【发布时间】:2014-01-09 22:58:55
【问题描述】:
我目前正在使用 netty 开发一个客户端-服务器应用程序,一些客户端在收到消息之前不会做任何事情。我已经阅读了 api,但找不到这样做的方法。我的意思是我可以尝试在 main 上使用“in.readline()”,这样它就不会结束,但感觉不对。也可能有无限循环,但我认为这也不是正确的方法。
这里的问题是:有没有办法为传入消息绑定套接字,就像服务器以 main 方法结尾一样?
public void run(){
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(new ChatClientInitializer());
Channel channel = bootstrap.connect(host,port).sync().channel();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Inserte su nombre");
String nombre = in.readLine();
MyClientChannel canal = new MyClientChannel(channel,nombre);
canal.write("SM",nombre);
in.readLine();
看到最后我必须写“in.readline()”,这样程序就不会结束,处理程序仍然可以接收传入的消息
【问题讨论】:
-
请尝试重新表述问题并提交示例代码。恐怕你的问题不是很清楚。
-
@42 完成,谢谢 :)