java - 我用netty编写了一个应用程序,从客户端向服务器发送消息,但服务器无法接收到消息
问题描述
我用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();
}
}
我从通道活动的客户端发送消息,但服务器没有收到消息,字符串“来自客户端的消息”
解决方案
那是因为您String
在调用时使用了 a writeAndFlush(...)
。返回的ChannelFuture
应该告诉您消息类型不受支持。如果你想使用 aString
你需要把StringEncoder
你的ChannelPipeline
,否则你只需要编写ByteBuf
实例。
推荐阅读
- android - 如何使用 RXJava 调用的响应结果作为另一个 RXJava 函数中的 if 语句的条件?
- .net-core - 加载所需库 hostpolicy.dll 时出错
- php - Mpdf 删除标题和正文内容之间的空格
- r - 您如何准备纵向数据以进行各种规格的生存分析?
- excel - 使用 VBScript 从 TXT 格式文件填充现有的 XLSX
- javascript - 如何使用javascript提取属性值
- java - 确定我的代码是否包含硬编码的 SQL 查询?
- karate - 空手道 + Appium,命令 driver.startRecordingScreen() 不起作用
- java - 计算一个点是否在多边形内
- php - Laravel Wherehas 在枢轴关系中不起作用并且不返回任何数据