【发布时间】:2014-06-01 10:26:31
【问题描述】:
我想写一个基于 netty 的客户端。它应该有方法 public String send(String msg); 应该从服务器或将来返回响应 - 没关系。它也应该是多线程的。像这样:
public class Client {
public static void main(String[] args) throws InterruptedException {
Client client = new Client();
}
private Channel channel;
public Client() throws InterruptedException {
EventLoopGroup loopGroup = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(loopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringDecoder()).
addLast(new StringEncoder()).
addLast(new ClientHandler());
}
});
channel = b.connect("localhost", 9091).sync().channel();
}
public String sendMessage(String msg) {
channel.writeAndFlush(msg);
return ??????????;
}
}
在调用 writeAndFlush(); 后,我不知道如何从服务器检索响应;我该怎么办?
我也使用 Netty 4.0.18.Final
【问题讨论】:
标签: java client-server netty