首先引入maven依赖,我这里用的5.0的
<dependencies>
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>5.0.0.Alpha2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.marshalling/jboss-marshalling -->
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling</artifactId>
<version>1.3.19.GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.marshalling/jboss-marshalling-serial -->
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling-serial</artifactId>
<version>1.3.18.GA</version>
<scope>test</scope>
</dependency>
</dependencies>
创建NettyClient客户端 class ClientHandler extends ChannelHandlerAdapter { /** * 当通道被调用,执行该方法 */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 接收数据 String value = (String) msg; System.out.println("client msg:" + value); } } public class NettyClient { public static void main(String[] args) throws InterruptedException { System.out.println("客户端已经启动...."); // 创建负责接收客户端连接 NioEventLoopGroup pGroup = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); b.group(pGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel sc) throws Exception { ByteBuf buf = Unpooled.copiedBuffer("_c".getBytes());//拆包 sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf)); /*sc.pipeline().addLast(new FixedLengthFrameDecoder(10)); sc.pipeline().addLast(new StringDecoder());按长度分 sc.pipeline().addLast(new ClientHandler());*/ } }); ChannelFuture cf = b.connect("127.0.0.1", 8080).sync(); // Thread.sleep(1000); //cf.channel().writeAndFlush(Unpooled.wrappedBuffer("".getBytes())); cf.channel().writeAndFlush(Unpooled.wrappedBuffer("caocao_c".getBytes())); cf.channel().writeAndFlush(Unpooled.wrappedBuffer("caocao_c".getBytes())); //Scanner s=new Scanner() // 等待客户端端口号关闭 cf.channel().closeFuture().sync(); pGroup.shutdownGracefully(); }
首先创建NettyServer服务类 class ServerHandler extends ChannelHandlerAdapter { /** * 当通道被调用,执行方法(拿到数据) */ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String value = (String) msg; System.out.println("服务器端收到客户端msg:" + value); // // 回復客戶端 //ctx.writeAndFlush("好啊"); } } public class NettyServer { public static void main(String[] args) { try { System.out.println("服务器端启动..."); // 1.创建两个线程池,一个负责接收客户端,一个进行传输 NioEventLoopGroup pGroup = new NioEventLoopGroup(); NioEventLoopGroup cGroup = new NioEventLoopGroup(); // 2.创建辅助类 ServerBootstrap b = new ServerBootstrap(); b.group(pGroup, cGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024) // 3.设置缓冲区与发送区大小 .option(ChannelOption.SO_SNDBUF, 32 * 1024).option(ChannelOption.SO_RCVBUF, 32 * 1024) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel sc) throws Exception { //sc.pipeline().addLast(new FixedLengthFrameDecoder(10)); ByteBuf buf =Unpooled.copiedBuffer("_c".getBytes()); sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf)); // 设置string类型 sc.pipeline().addLast(new StringDecoder()); sc.pipeline().addLast(new ServerHandler()); } }); // 启动 ChannelFuture cf = b.bind(8080).sync(); // 关闭 cf.channel().closeFuture().sync(); pGroup.shutdownGracefully(); cGroup.shutdownGracefully(); } catch (Exception e) { // TODO: handle exception } }