Netty WriteAndFlush 方法不起作用
Netty WriteAndFlush method doesnt work
看下面的代码:
public final class SecureChatClient {
static final String HOST = System.getProperty("host", "127.0.0.1");
static final int PORT = Integer.parseInt(System.getProperty("port", "8992"));
public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new NetworkInitializer(sslCtx));
// Start the connection attempt.
Channel ch = b.connect(HOST, PORT).sync().channel();
// Read commands from the stdin.
ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//does not work
ch.writeAndFlush("hi");
while (true) {
String line = in.readLine();
if (line == null) {
break;
//(goes to if writefuture !=null)
}
// Sends the received line to the server.
lastWriteFuture = ch.writeAndFlush(line + "\r\n");
}
// Wait until all messages are flushed before closing the channel.
if (lastWriteFuture != null) {
lastWriteFuture.sync();
}
} finally {
// The connection is closed automatically on shutdown.
group.shutdownGracefully();
}
}}
该代码是根据 netty 的 SecureChatClient Class http://netty.io/wiki/user-guide-for-4.x.html 修改的,添加了行
ch.writeAndFlush("hi");
在 while 循环之前。服务器上的输出不会读取该行。我不明白为什么会这样,对我来说似乎 ch.writeandflush 方法本身在循环之外不起作用。
如果我不应该在循环外使用 ch.writeandlfush,有没有更好的方法在启动时向服务器发送消息?
解决方法:所有的writeandflush语句必须以“\r\n”结尾才能刷新。有了它们就不会冲洗。我不明白这是为什么,但无论如何。
代码解:ch.writeAndFlush("hi\r\n");
您应该检查 writeAndFlush(...) 返回的 ChannelFuture 告诉您的内容。我敢打赌你会得到某种例外。
解决方法:所有的writeandflush语句必须以“\r\n”结尾才能刷新。有了它们就不会冲洗。我不明白为什么会这样。
代码解:ch.writeAndFlush("hi\r\n");
看下面的代码:
public final class SecureChatClient {
static final String HOST = System.getProperty("host", "127.0.0.1");
static final int PORT = Integer.parseInt(System.getProperty("port", "8992"));
public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new NetworkInitializer(sslCtx));
// Start the connection attempt.
Channel ch = b.connect(HOST, PORT).sync().channel();
// Read commands from the stdin.
ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//does not work
ch.writeAndFlush("hi");
while (true) {
String line = in.readLine();
if (line == null) {
break;
//(goes to if writefuture !=null)
}
// Sends the received line to the server.
lastWriteFuture = ch.writeAndFlush(line + "\r\n");
}
// Wait until all messages are flushed before closing the channel.
if (lastWriteFuture != null) {
lastWriteFuture.sync();
}
} finally {
// The connection is closed automatically on shutdown.
group.shutdownGracefully();
}
}}
该代码是根据 netty 的 SecureChatClient Class http://netty.io/wiki/user-guide-for-4.x.html 修改的,添加了行
ch.writeAndFlush("hi");
在 while 循环之前。服务器上的输出不会读取该行。我不明白为什么会这样,对我来说似乎 ch.writeandflush 方法本身在循环之外不起作用。
如果我不应该在循环外使用 ch.writeandlfush,有没有更好的方法在启动时向服务器发送消息?
解决方法:所有的writeandflush语句必须以“\r\n”结尾才能刷新。有了它们就不会冲洗。我不明白这是为什么,但无论如何。
代码解:ch.writeAndFlush("hi\r\n");
您应该检查 writeAndFlush(...) 返回的 ChannelFuture 告诉您的内容。我敢打赌你会得到某种例外。
解决方法:所有的writeandflush语句必须以“\r\n”结尾才能刷新。有了它们就不会冲洗。我不明白为什么会这样。
代码解:ch.writeAndFlush("hi\r\n");