我用netty编写了一个应用程序,客户端使用channelActive方法将字符串消息发送到服务器,但是服务器未收到该消息。我不知道原因。
服务器代码:
public static void main(String[] args) throws InterruptedException {
NioEventLoopGroup boss = new NioEventLoopGroup();
NioEventLoopGroup worker = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss,worker);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new NettyServerHandler());
}
});
bootstrap.option(ChannelOption.SO_BACKLOG,1024);
bootstrap.childOption(ChannelOption.SO_KEEPALIVE,true);
ChannelFuture sync = bootstrap.bind(8080).sync();
System.out.println("server start");
sync.channel().closeFuture().sync();
System.out.println("server end");
} finally {
worker.shutdownGracefully();
boss.shutdownGracefully();
}
}
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("channelRead");
super.channelRead(ctx, msg);
System.out.println(((ByteBuf) msg).toString(Charset.defaultCharset()));
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelActive");
super.channelActive(ctx);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("exceptionCaught");
super.exceptionCaught(ctx, cause);
cause.printStackTrace();
ctx.close();
}
}
我在服务器处理程序的方法channelRead中打印来自客户端的消息。
以下是客户端代码:
public static void main(String[] args) throws InterruptedException {
NioEventLoopGroup worker = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(worker);
bootstrap.channel(NioSocketChannel.class);
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new NettyClientHandler());
}
});
ChannelFuture future = bootstrap.connect("localhost", 8080).sync();
future.channel().closeFuture().sync();
}finally {
worker.shutdownGracefully();
}
}
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("channelActive");
super.channelActive(ctx);
ctx.writeAndFlush("message from client");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("channelRead");
super.channelRead(ctx, msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("exceptionCaught");
super.exceptionCaught(ctx, cause);
cause.printStackTrace();
ctx.close();
}
}
我从通道处于活动状态的客户端发送了消息,但是服务器未收到消息,字符串“来自客户端的消息”
答案 0 :(得分:0)
那是因为调用String
时使用了writeAndFlush(...)
。返回的ChannelFuture
应该告诉您不支持的消息类型。如果要使用String
,则需要将StringEncoder
放在ChannelPipeline
上,否则只需要编写ByteBuf
个实例。