工程结构图
一、Socket服务端
1、创建MyServer 类
public class MyServer { public static void main(String[] args) throws Exception{ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup worderGroup = new NioEventLoopGroup(); try{ ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup,worderGroup).channel(NioServerSocketChannel.class) .childHandler(new MyServerinitializer()); ChannelFuture channelFuture = serverBootstrap.bind(8899).sync(); channelFuture.channel().closeFuture().sync(); }finally { bossGroup.shutdownGracefully(); worderGroup.shutdownGracefully(); } } }
2、服务端处理器类MyServerHandle
public class MyServerHandle extends SimpleChannelInboundHandler<String>{ protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { CommonUtil.println(ctx.channel().remoteAddress() + ", " + msg); ctx.channel().writeAndFlush("from server: " + UUID.randomUUID()); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace();; ctx.close(); } }
当接收到请求后,向客户端写数据。
3、创建MyServerinitializer类
public class MyServerinitializer extends ChannelInitializer<SocketChannel>{ protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0, 4, 0, 4)); pipeline.addLast(new LengthFieldPrepender(4)); pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast(new MyServerHandle()); } }
二、客户端
1、MyClient 类
public class MyClient { public static void main(String[] args) throws Exception{ EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class) .handler(new MyClientInitializer()); ChannelFuture channelFuture = bootstrap.connect("localhost",8899).sync(); channelFuture.channel().closeFuture().sync(); }finally { eventLoopGroup.shutdownGracefully(); } } }
2、客户端处理器MyClientHandle
public class MyClientHandle extends SimpleChannelInboundHandler<String> { //服务端向客户端发送消息时,该方法会被调用 @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { CommonUtil.println(ctx.channel().remoteAddress()); CommonUtil.println("client output:" + msg); ctx.writeAndFlush("from client: " + LocalDateTime.now()); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush("来自客户端的消息"); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }
客户端channel active后,向服务端发送消息
3、MyClientInitializer类
public class MyClientInitializer extends ChannelInitializer<SocketChannel> { protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0, 4, 0, 4)); pipeline.addLast(new LengthFieldPrepender(4)); pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast(new MyClientHandle()); } }
三、测试
1、启动服务端
2、启动客户端(可以启动多个)
服务端输出结果
客户端1输出
客户端2输出
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!