日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Netty网络聊天室完整代码实现

發(fā)布時間:2025/1/21 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Netty网络聊天室完整代码实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Netty服務端:

package cn.zhangxueliang.netty.chat;import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.*; 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.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder;//聊天程序服務器端 public class ChatServer {private int port; //服務器端端口號public ChatServer(int port) {this.port = port;}public void run() throws Exception {EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<SocketChannel>() {@Overridepublic void initChannel(SocketChannel ch) {ChannelPipeline pipeline=ch.pipeline();//往pipeline鏈中添加一個解碼器pipeline.addLast("decoder",new StringDecoder());//往pipeline鏈中添加一個編碼器pipeline.addLast("encoder",new StringEncoder());//往pipeline鏈中添加自定義的handler(業(yè)務處理類)pipeline.addLast(new ChatServerHandler());}});System.out.println("Netty Chat Server啟動......");ChannelFuture f = b.bind(port).sync();f.channel().closeFuture().sync();} finally {workerGroup.shutdownGracefully();bossGroup.shutdownGracefully();System.out.println("Netty Chat Server關閉......");}}public static void main(String[] args) throws Exception {new ChatServer(9999).run();} }

Netty服務端自定義handler:

package cn.zhangxueliang.netty.chat;import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.group.ChannelGroup; import io.netty.channel.group.DefaultChannelGroup; import io.netty.util.concurrent.GlobalEventExecutor;import java.util.ArrayList; import java.util.List;//自定義一個服務器端業(yè)務處理類 public class ChatServerHandler extends SimpleChannelInboundHandler<String> {public static List<Channel> channels = new ArrayList<>();@Override //通道就緒public void channelActive(ChannelHandlerContext ctx) {Channel inChannel=ctx.channel();channels.add(inChannel);System.out.println("[Server]:"+inChannel.remoteAddress().toString().substring(1)+"上線");}@Override //通道未就緒public void channelInactive(ChannelHandlerContext ctx) {Channel inChannel=ctx.channel();channels.remove(inChannel);System.out.println("[Server]:"+inChannel.remoteAddress().toString().substring(1)+"離線");}@Override //讀取數(shù)據(jù)protected void channelRead0(ChannelHandlerContext ctx, String s) {Channel inChannel=ctx.channel();for(Channel channel:channels){if(channel!=inChannel){channel.writeAndFlush("["+inChannel.remoteAddress().toString().substring(1)+"]"+"說:"+s+"\n");}}}}

Netty客戶端:

package cn.zhangxueliang.netty.chat;import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; 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.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder;import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner;//聊天程序客戶端 public class ChatClient {private final String host; //服務器端IP地址private final int port; //服務器端端口號public ChatClient(String host, int port) {this.host = host;this.port = port;}public void run(){EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {@Overridepublic void initChannel(SocketChannel ch){ChannelPipeline pipeline=ch.pipeline();//往pipeline鏈中添加一個解碼器pipeline.addLast("decoder",new StringDecoder());//往pipeline鏈中添加一個編碼器pipeline.addLast("encoder",new StringEncoder());//往pipeline鏈中添加自定義的handler(業(yè)務處理類)pipeline.addLast(new ChatClientHandler());}});ChannelFuture cf=bootstrap.connect(host,port).sync();Channel channel=cf.channel();System.out.println("------"+channel.localAddress().toString().substring(1)+"------");Scanner scanner=new Scanner(System.in);while (scanner.hasNextLine()){String msg=scanner.nextLine();channel.writeAndFlush(msg+"\r\n");}} catch (Exception e) {e.printStackTrace();} finally {group.shutdownGracefully();}}public static void main(String[] args) throws Exception {new ChatClient("127.0.0.1",9999).run();} }

Netty客戶端handler:

package cn.zhangxueliang.netty.chat;import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler;//自定義一個客戶端業(yè)務處理類 public class ChatClientHandler extends SimpleChannelInboundHandler<String> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {System.out.println(s.trim());} }

?

總結

以上是生活随笔為你收集整理的Netty网络聊天室完整代码实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产欧美一区二区三区在线看蜜臀 | 国产精品久久久久久久久久久久 | 日批国产| 天天干天天要 | 国产操人| 久视频在线观看 | 精品国产一区二区三区日日嗨 | 国产免费自拍 | 男生和女生一起差差差很痛的视频 | 女同性做爰三级 | 国产精品va无码一区二区三区 | 伊人av网| 大尺度电影在线 | 精品人妻一区二区三区免费看 | 日韩视频一区二区三区四区 | 日韩欧美在线播放 | 国内成人自拍视频 | 一本色道久久88加勒比—综合 | 亚洲av无码国产综合专区 | 欧美亚洲国产日韩 | 夜夜高潮夜夜爽 | 天天操天天操天天操天天操 | 澳门黄色网 | 午夜性生活片 | 骚虎av | 欧美青青草 | 一级在线免费视频 | 亚洲av无码精品色午夜果冻不卡 | 亚洲免费不卡视频 | 无码乱人伦一区二区亚洲 | 永久免费精品影视网站 | 激情丁香 | 国产欧美视频在线观看 | 亚洲国产精品麻豆 | 精品久久成人 | 四色在线 | 国产精品suv一区二区69 | 亚洲乱亚洲乱妇 | 天堂一区二区三区四区 | 91爽爽 | 少妇一边呻吟一边说使劲视频 | 亚洲一区免费观看 | 亚洲成熟少妇视频在线观看 | 日韩成人av影院 | 少妇性高潮视频 | 黄色动漫免费在线观看 | 国产乱子伦精品视频 | 制服丝袜第一页在线观看 | www.成人网| 国产福利短视频 | 国产精品亚洲а∨天堂免在线 | 国产一区二区视频免费 | 日本一区二区高清视频 | 精品久久亚洲 | 青青草国产在线观看 | 自拍第1页| 成熟丰满熟妇高潮xxxxx视频 | 欧美自拍色图 | 久草综合在线观看 | 亚洲 欧洲 日韩 | 午夜日韩 | 亚洲最大网站 | 日本一本在线观看 | 国产精品日日摸天天碰 | 亚洲av无码一区二区三区dv | 国产精品1区 | 精品无码人妻一区 | 亚洲黄色在线看 | 深夜视频一区二区三区 | 老头糟蹋新婚少妇系列小说 | 亚洲成av人片在www色猫咪 | 日韩高清成人 | 国产精品探花一区二区三区 | 日产久久视频 | 神马午夜嘿嘿 | 天天干天天添 | 免费av小说 | 久久视频一区二区三区 | 毛片啪啪啪| 青青青草视频在线 | 人妖干美女 | 老司机综合网 | 亚洲精品无码成人 | 韩国三级一区 | 97超碰总站 | 亚洲综合在线第一页 | 国产中文字幕免费 | av在线不卡观看 | 国产极品视频 | 国产欧美一区二区三区在线看蜜臀 | 另类小说一区二区 | 免费久久网站 | 黑丝av在线 | 九九精品视频免费 | 精品欧美国产 | 医生强烈淫药h调教小说视频 | 成人91在线观看 | 国产毛片aaa| 超碰中文字幕在线 |