使用Netty框架实现一个简单的CS结构的网络程序处理tcp半包问题粘包、拆包运行结果是什么
以下是一个简单的使用Netty框架处理tcp半包问题的示例代码:
服务端代码:
public class Server {
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024, 0, 4, 0, 4));
ch.pipeline().addLast(new ServerHandler());
}
});
ChannelFuture f = bootstrap.bind(8888).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
try {
int length = buf.readInt();
byte[] content = new byte[length];
buf.readBytes(content);
String message = new String(content, CharsetUtil.UTF_8);
System.out.println("Received message: " + message);
ctx.writeAndFlush(Unpooled.copiedBuffer(("Server received: " + message).getBytes()));
} finally {
buf.release();
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
客户端代码:
public class Client {
public static void main(String[] args) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LengthFieldPrepender(4));
ch.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture f = bootstrap.connect("localhost", 8888).sync();
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String message = scanner.nextLine();
ByteBuf buf = Unpooled.copiedBuffer(message.getBytes());
f.channel().writeAndFlush(buf);
}
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
}
class ClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
try {
int length = buf.readInt();
byte[] content = new byte[length];
buf.readBytes(content);
String message = new String(content, CharsetUtil.UTF_8);
System.out.println("Received message: " + message);
} finally {
buf.release();
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
运行结果:
启动服务端后,客户端连接上来后可以输入任意字符串,服务端会将收到的字符串打印出来,并回复一个消息。例如:
Client: hello
Server: Received message: hello
Client: world
Server: Received message: world
``
原文地址: https://www.cveoy.top/t/topic/cFzd 著作权归作者所有。请勿转载和采集!