分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

02_Netty实现的Echo服务器和客户端

发布时间:2023-09-06 02:31责任编辑:沈小雨关键词:暂无标签

【Echo服务端】

【EchoServer】

public class EchoServer { ???private final int port; ???public EchoServer(int port) { ???????this.port = port; ???} ???public static void main(String[] args) throws Exception { ???????new EchoServer(9999).start(); ???} ???public void start() throws Exception { ???????NioEventLoopGroup group = new NioEventLoopGroup(); ???????try { ???????????ServerBootstrap b = new ServerBootstrap(); ??//创建ServerBootstrap ???????????b.group(group) ???????????????????.channel(NioServerSocketChannel.class) ??//指定NIO的传输Channel ???????????????????.localAddress(new InetSocketAddress(port)) ???????????????????.childHandler(new ChannelInitializer<SocketChannel>() { ???????????????????????@Override ???????????????????????protected void initChannel(SocketChannel channel) throws Exception { ???????????????????????????channel.pipeline().addLast(new EchoServerHandler()); ?//添加EchoServerHandler到Channel的ChannelPipeline ???????????????????????} ???????????????????}); ???????????ChannelFuture future = b.bind().sync(); ?//绑定服务器,sync等待服务器关闭 ???????????System.out.println(EchoServer.class.getName() + " started and listen on " + future.channel().localAddress()); ???????????future.channel().closeFuture().sync(); ???????} finally { ???????????group.shutdownGracefully().sync(); ???????} ???}}

【EchoServerHandler】

public class EchoServerHandler extends ChannelInboundHandlerAdapter { ???@Override ???public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ???????ByteBuf ?in = (ByteBuf)msg; ???????System.out.println("Server received:"+in.toString(CharsetUtil.UTF_8)); ???????ctx.write(in); ???} ???@Override ???public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ???????ctx.writeAndFlush(Unpooled.EMPTY_BUFFER) ???????????????.addListener(ChannelFutureListener.CLOSE); ???} ???@Override ???public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ???????cause.printStackTrace(); ???????ctx.close(); ???} ???@Override ???public void channelActive(ChannelHandlerContext ctx) throws Exception { ???????System.out.println("Server is Active......"); ???}}

[ 说明 ] 

Echo的Handler实现了服务器的业务,决定了连接创建以后和收到信息后该如何处理。

ChannelInboundHandler的实现方法作用channelRead() ????????//每次收到信息都会回调channelReadComplete() //channelRead执行结束时回调exceptionCaught() ????//执行异常情况下会被回调。

【Echo客户端】

【EchoClient】

public class EchoClient { ???private final String host; ???private final int port; ???public EchoClient(String host, int port) { ???????this.host = host; ???????this.port = port; ???} ???public static void main(String[] args) throws Exception{ ???????new EchoClient("127.0.0.1",9999).start(); ???} ???public void start() throws Exception{ ???????EventLoopGroup group = new NioEventLoopGroup(); ???????try{ ???????????Bootstrap b = new Bootstrap(); ???????????b.group(group) ???????????????????.channel(NioSocketChannel.class) ???????????????????.remoteAddress(new InetSocketAddress(host,port)) ???????????????????.handler(new ChannelInitializer<SocketChannel>() { ???????????????????????@Override ???????????????????????protected void initChannel(SocketChannel channel) throws Exception { ???????????????????????????channel.pipeline().addLast( ???????????????????????????????????new EchoClientHandler() ???????????????????????????); ???????????????????????} ???????????????????}); ???????????ChannelFuture future = b.connect().sync(); ???????????future.channel().closeFuture().sync(); ???????}finally { ???????????group.shutdownGracefully(); ???????} ???}}

【EchoClientHandler】

public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> { ???@Override ???public void channelActive(ChannelHandlerContext ctx) throws Exception { ???????System.out.println("client is active......"); ???????ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8)); ???} ???@Override ???protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) throws Exception { ???????System.out.println("client received:"+ in.toString(CharsetUtil.UTF_8)); ???} ???@Override ???public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ???????cause.printStackTrace(); ???????ctx.close(); ???}}

【运行结果】

[ 服务端 ]

[ 客户端 ]

02_Netty实现的Echo服务器和客户端

原文地址:https://www.cnblogs.com/HigginCui/p/10324585.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved