1.服务器端pom.xml
4.0.0 cn.edu.tju nettyechoserver1.0-SNAPSHOT org.apache.maven.plugins maven-compiler-plugin8 io.netty netty-all4.0.0.Final com.google.protobuf protobuf-java3.0.0
2.服务器端ChannelHandler
package cn.edu.tju.handler; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandler; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelPipeline; import java.util.Date; public class ServerChannelHandler extends ChannelInboundHandlerAdapter { private int counter=0; @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { String msg="message from server: "+new Date().toLocaleString(); ByteBuf message=Unpooled.copiedBuffer(msg.getBytes()); //ctx.writeAndFlush(message); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String str=(String)msg; String sequence=String.valueOf(++counter)+" : "; System.out.println(sequence+str); } @Override public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause){ System.out.println(cause.getMessage()); } }
3.服务器端主类:
package cn.edu.tju; import cn.edu.tju.handler.ServerChannelHandler; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; 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.nio.NioServerSocketChannel; import io.netty.handler.codec.LinebasedframeDecoder; import io.netty.handler.codec.string.StringDecoder; public class Start { private static int port=8899; public static void main(String[] args) { EventLoopGroup bossGroup=new NioEventLoopGroup(); EventLoopGroup workerGroup=new NioEventLoopGroup(); try{ ServerBootstrap serverBootstrap=new ServerBootstrap(); serverBootstrap.group(bossGroup,workerGroup). channel(NioServerSocketChannel.class). childHandler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { //以下三个ChannelHandler的顺序不能改变 ch.pipeline().addLast(new LinebasedframeDecoder(1024)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new ServerChannelHandler()); } }); ChannelFuture channelFuture=serverBootstrap.bind(port).sync(); channelFuture.channel().closeFuture().sync(); }catch (Exception ex){ System.out.println(ex.getMessage()); }finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }
4.客户端pom.xml
4.0.0 cn.edu.tju nettyechoclient1.0-SNAPSHOT org.apache.maven.plugins maven-compiler-plugin8 io.netty netty-all4.0.0.Final com.google.protobuf protobuf-java3.0.0
5.客户端ChannelHandler
package cn.edu.tju.handler; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import java.util.Date; public class ClientChannelHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx,Object msg){ String str=(String)msg; System.out.println(str); } @Override public void channelActive(ChannelHandlerContext ctx){ for(int i=0;i<10000;i++){ String msg=new Date().toLocaleString()+"rn"; ByteBuf message=null; message= Unpooled.buffer(msg.length()); message.writeBytes(msg.getBytes()); ctx.writeAndFlush(message); } } @Override public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause){ System.out.println(cause.getMessage()); } }
6.客户端主类:
package cn.edu.tju; import cn.edu.tju.handler.ClientChannelHandler; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.LinebasedframeDecoder; import io.netty.handler.codec.string.StringDecoder; public class Start { private static int port=8899; private static String host="127.0.0.1"; public static void main(String[] args) { NioEventLoopGroup group=new NioEventLoopGroup(); try{ Bootstrap bootstrap=new Bootstrap(); bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel ch) throws Exception { //以下3个ChannelHandler顺序不能改变 ch.pipeline().addLast(new LinebasedframeDecoder(1024)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new ClientChannelHandler()); } }); ChannelFuture f=bootstrap.connect(host,port).sync(); f.channel().closeFuture().sync(); }catch (Exception ex){ System.out.println(ex.getMessage()); }finally { group.shutdownGracefully(); } } }
7.运行服务器端和客户端,可以看到服务器端收到的消息并未发生粘包
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)