网络编程复习(七):Netty解决拆包粘包问题--分隔符方式

在Netty中实现了2种拆包方式:分隔符方式,定长方式,这里介绍分隔符方式。实现起来很简单:

server:

package 网络编程_netty2;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
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.string.StringDecoder;

public class Server {
	private int port;
	
	public Server(int port){
		this.port = port;
	}
	public void run()throws Exception{
		EventLoopGroup boss = new NioEventLoopGroup();
		EventLoopGroup workers = new NioEventLoopGroup();
		ServerBootstrap bootstrap = new ServerBootstrap();
		bootstrap.group(boss, workers)
				 .channel(NioServerSocketChannel.class)
				 .childHandler(new ChannelInitializer<SocketChannel>() {
					@Override
					public void initChannel(SocketChannel ch) throws Exception {
						ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes());
						ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf));
						ch.pipeline().addLast(new StringDecoder());
						ch.pipeline().addLast(new ServerHandler());
					}
				});
		ChannelFuture f = bootstrap.bind(port).sync();
		f.channel().closeFuture().sync();
		workers.shutdownGracefully();
		boss.shutdownGracefully();
	}
	public static void main(String[] args) throws Exception {
		new Server(8888).run();
	}
}
serverHandler:

package 网络编程_netty2;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ServerHandler extends ChannelHandlerAdapter{

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		String request = (String) msg;
		System.out.println(request);
		ctx.writeAndFlush(Unpooled.copiedBuffer("你好,服务端已经接收到你的请求$_".getBytes()));
	}

	
}

client

package 网络编程_netty2;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
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.string.StringDecoder;

public class client {
	public static void main(String[] args) throws InterruptedException {
		EventLoopGroup worker = new NioEventLoopGroup();
		Bootstrap bootstrap = new Bootstrap();
		bootstrap.group(worker)
				 .channel(NioSocketChannel.class)
				 .handler(new ChannelInitializer<SocketChannel>() {
					@Override
					public void initChannel(SocketChannel ch) throws Exception {
						ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes());
						ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf));
						ch.pipeline().addLast(new StringDecoder());
						ch.pipeline().addLast(new ClientHandler());
					}
				});
		ChannelFuture f = bootstrap.connect("127.0.0.1", 8888).sync();
		f.channel().writeAndFlush(Unpooled.wrappedBuffer("aaa$_".getBytes()));
		f.channel().writeAndFlush(Unpooled.wrappedBuffer("bbbb$_".getBytes()));
		f.channel().writeAndFlush(Unpooled.wrappedBuffer("ccccccc$_".getBytes()));
		f.channel().closeFuture().sync();
		worker.shutdownGracefully();
	}
	
}
clientHandler:

package 网络编程_netty2;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ClientHandler extends ChannelHandlerAdapter{

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		String response = (String) msg;
		System.out.println(response);
	}

	
}
这里使用"$_"为分隔符。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值