Netty WriteAndFlush method doesnt work(Netty WriteAndFlush 方法不起作用)
问题描述
看下面的代码:
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 + "
");
}
// 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 类 http://netty 修改的.io/wiki/user-guide-for-4.x.html 添加的行
That code was modified from netty's SecureChatClient Class http://netty.io/wiki/user-guide-for-4.x.html with the added in line
ch.writeAndFlush("hi");
在while循环之前.服务器上的输出不会读取该行.我不明白为什么会这样,对我来说,ch.writeandflush 方法本身似乎在循环之外不起作用.
before the while loop. The output on the server does not read that line. I can't understand why that is, and to me it almost seems like the ch.writeandflush method itself doesn't work outside of a loop.
如果我不应该在循环之外使用 ch.writeandlfush,有没有更好的方法在启动时向服务器发送消息?
If im not supposed to use ch.writeandlfush outside of a loop, is there any better way to send a message to a server on startup?
解决方案:所有 writeandflush 语句必须以 "结尾才能刷新.没有,他们不冲洗.我不明白为什么会这样.
Solution: all writeandflush statements must end with " " in order to flush. With out that they dont flush. I don't understand why that is but whatever.
代码解决方案:ch.writeAndFlush("hi ");
Code solution: ch.writeAndFlush("hi ");
推荐答案
解决方案:所有 writeandflush 语句必须以 "结尾才能刷新.没有,他们不冲洗.我不明白为什么会这样.
Solution: all writeandflush statements must end with " " in order to flush. With out that they dont flush. I don't understand why that is but whatever.
代码解决方案:ch.writeAndFlush("hi ");
Code solution: ch.writeAndFlush("hi ");
这篇关于Netty WriteAndFlush 方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Netty WriteAndFlush 方法不起作用
基础教程推荐
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01