- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
看下面的代码:
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 类 http://netty.io/wiki/user-guide-for-4.x.html添加了行
ch.writeAndFlush("hi");
在 while 循环之前。服务器上的输出不会读取该行。我不明白为什么会这样,对我来说,ch.writeandflush 方法本身似乎在循环之外不起作用。
如果我不应该在循环外使用 ch.writeandlfush,有没有更好的方法在启动时向服务器发送消息?
解决方法:所有的writeandflush语句都必须以“\r\n”结尾,才能flush。有了它们就不会冲洗。我不明白这是为什么,但无论如何。
代码解决:ch.writeAndFlush("hi\r\n");
最佳答案
您应该检查 writeAndFlush(...) 返回的 ChannelFuture 告诉您的内容。我敢打赌你会得到某种异常(exception)。
关于java - Netty WriteAndFlush 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28934777/
看下面的代码: public final class SecureChatClient { static final String HOST = System.getProperty("host",
我的 tcp 客户端请求 netty 服务器,netty 服务器使用 writeAndFlush() 返回 393718 字节。但客户端只收到 262142 字节。我使用“tcpdump -A”来抓取
我正在使用 Netty 4.1.16 创建服务器。我建立了一条管道: socketChannel.pipeline() //Line Based Frame Decoder will
我正在使用 ChannelInboundHandlerAdapter 类并 writeAndFlush 来自 channelRead() 的每条消息; public void channelRead(
我们有一个在 Ubuntu v10 上运行的基于 Netty (4.0.15) 的 Websocket 服务器,在弹性测试期间我们这样做: 杀死 -9 服务器 从客户端发送一些数据 预计客户端上的wr
引用包io.netty.example.proxy中的netty示例,http和https都可以正常工作,即使https消息是乱码。 我想使用netty来执行从代理服务器获取的https消息。 pub
我是 netty 新手,并试图了解 writeAndFlush 的 channel future 如何工作。考虑在 netty 客户端上运行以下代码: final ChannelFuture writ
我正在使用 Netty4 框架开发一个 IM 服务器。同时,我使用名为channel.writeAndFlush() 的方法向客户端发送消息。但是,当手机端的客户端socket异常关闭时,比如关闭网络
我正在使用 java 代码将 JSON 数据发送到 flash,因为我使用的是 netty 服务器 4.0.23。通过这种方式,我经常使用两种不同的数据发送给同一个客户端。我使用了channel.wr
我是一名优秀的程序员,十分优秀!