Java 内蒂:发送消息有问题
所以我最近和netty(5.0.0.Alpha2)合作了一点,我非常喜欢它!但不幸的是,我无法让发送/接收消息工作。奇怪的是,连接和断开就像一种魅力。服务器接收客户机已连接/断开连接且通道已添加/删除的消息。只是消息不能正常工作 我确实尝试过很多其他的方法(例如没有编码器),但它从来都不起作用。。也许这里有人知道?我真的很感激 提前谢谢!您可以在下面找到所有使用的源代码: CoreClient.javaJava 内蒂:发送消息有问题,java,netty,channel,Java,Netty,Channel,所以我最近和netty(5.0.0.Alpha2)合作了一点,我非常喜欢它!但不幸的是,我无法让发送/接收消息工作。奇怪的是,连接和断开就像一种魅力。服务器接收客户机已连接/断开连接且通道已添加/删除的消息。只是消息不能正常工作 我确实尝试过很多其他的方法(例如没有编码器),但它从来都不起作用。。也许这里有人知道?我真的很感激 提前谢谢!您可以在下面找到所有使用的源代码: CoreClient.java package me.creepsterlgc.coreclient; import io
package me.creepsterlgc.coreclient;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class CoreClient extends Thread {
private String host;
private int port;
private Channel channel;
public CoreClient(String host, int port) {
this.host = host;
this.port = port;
}
public void run() {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("handler", new CoreClientHandler());
}
});
ChannelFuture f = bootstrap.connect(host, port);
channel = f.sync().channel();
ChannelFuture cf = null;
try {
cf = channel.writeAndFlush("Testing..").sync();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!cf.isSuccess()) {
System.out.println("Send failed: " + cf.cause());
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally {
}
}
public void send(String message) {
ChannelFuture cf = null;
try {
cf = channel.writeAndFlush(message).sync();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
channel.flush();
if (!cf.isSuccess()) {
System.out.println("Send failed: " + cf.cause());
}
}
public void shutdown() {
}
}
CoreServer.java
package me.creepsterlgc.coreserver;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class CoreServer extends Thread {
private int port;
public CoreServer(int port) {
this.port = port;
}
public void run() {
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(boss, worker)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("handler", new CoreServerHandler());
}
});
ChannelFuture f = bootstrap.bind(port).sync();
f.channel().closeFuture().sync();
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
}
你应该替换
try {
cf = channel.write("Testing..").sync();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
channel.flush();
与:
你应该替换
try {
cf = channel.write("Testing..").sync();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
channel.flush();
与:
你正在使用的netty版本是什么?嗨,Sudheera,我正在使用以下版本:5.0.0。Alpha2你正在使用的netty版本是什么?嗨,Sudheera,我正在使用以下版本:5.0.0。Alpha2Hi Norman,谢谢你的回复!我替换了channel.flush();使用直接的.writeAndFlush();但似乎仍然没有消息发送。在两次访问客户后,我收到以下日志:嗨,诺曼,谢谢你的回复!我替换了channel.flush();使用直接的.writeAndFlush();但似乎仍然没有消息发送。在对客户端执行两次扫描后,我收到以下日志:
try {
cf = channel.write("Testing..").sync();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
channel.flush();
try {
cf = channel.writeAndFlush("Testing..").sync();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}