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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Netty4服务端和客户端实现

發(fā)布時(shí)間:2025/3/19 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Netty4服务端和客户端实现 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

目標(biāo)

代碼UML類圖

服務(wù)端

客戶端

Netty4實(shí)現(xiàn)服務(wù)端

Netty4實(shí)現(xiàn)客戶端

測試

小結(jié)


目標(biāo)

用netty4實(shí)現(xiàn)一個(gè)服務(wù)端和客戶端,兩者之間可以進(jìn)行測試通信

代碼UML類圖

服務(wù)端

客戶端

Netty4實(shí)現(xiàn)服務(wù)端

服務(wù)類

package com.mym.netty.server;import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder;/*** netty服務(wù)端*/ public class NettyServer {public static void main(String[] args) {startServer();}public static void startServer(){//1.定義server啟動(dòng)類ServerBootstrap serverBootstrap = new ServerBootstrap();//2.定義工作組:boss分發(fā)請求給各個(gè)worker:boss負(fù)責(zé)監(jiān)聽端口請求,worker負(fù)責(zé)處理請求(讀寫)EventLoopGroup boss = new NioEventLoopGroup();EventLoopGroup worker = new NioEventLoopGroup();//3.定義工作組serverBootstrap.group(boss,worker);//4.設(shè)置通道channelserverBootstrap.channel(NioServerSocketChannel.class);//A//serverBootstrap.channelFactory(new ReflectiveChannelFactory(NioServerSocketChannel.class));//舊版本的寫法,但是此過程在A中有同樣過程//5.添加handler,管道中的處理器,通過ChannelInitializer來構(gòu)造serverBootstrap.childHandler(new ChannelInitializer<Channel>() {@Overrideprotected void initChannel(Channel channel) throws Exception {//此方法每次客戶端連接都會(huì)調(diào)用,是為通道初始化的方法//獲得通道channel中的管道鏈(執(zhí)行鏈、handler鏈)ChannelPipeline pipeline = channel.pipeline();pipeline.addLast(new StringDecoder());pipeline.addLast("serverHandler1",new ServerHandler());pipeline.addLast("serverHandler2",new ServerHandler2());pipeline.addLast(new StringEncoder());System.out.println("success to initHandler!");}});//6.設(shè)置參數(shù)//設(shè)置參數(shù),TCP參數(shù)serverBootstrap.option(ChannelOption.SO_BACKLOG, 2048); //連接緩沖池的大小serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);//維持鏈接的活躍,清除死鏈接serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);//關(guān)閉延遲發(fā)送//7.綁定ip和porttry {ChannelFuture channelFuture = serverBootstrap.bind("0.0.0.0", 9099).sync();//Future模式的channel對象//7.5.監(jiān)聽關(guān)閉channelFuture.channel().closeFuture().sync(); //等待服務(wù)關(guān)閉,關(guān)閉后應(yīng)該釋放資源} catch (InterruptedException e) {System.out.println("server start got exception!");e.printStackTrace();}finally {//8.優(yōu)雅的關(guān)閉資源boss.shutdownGracefully();worker.shutdownGracefully();}} }

Handler1

package com.mym.netty.server;import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter;public class ServerHandler extends ChannelInboundHandlerAdapter {/** ChannelInboundHandlerAdapter:ChannelInboundHandlerAdapter是ChannelInboundHandler的一個(gè)簡單實(shí)現(xiàn),默認(rèn)情況下不會(huì)做任何處理,* 只是簡單的將操作通過fire*方法傳遞到ChannelPipeline中的下一個(gè)ChannelHandler中讓鏈中的下一個(gè)ChannelHandler去處理。** SimpleChannelInboundHandler:SimpleChannelInboundHandler支持泛型的消息處理,默認(rèn)情況下消息處理完將會(huì)被自動(dòng)釋放,無法提供* fire*方法傳遞給ChannelPipeline中的下一個(gè)ChannelHandler,如果想要傳遞給下一個(gè)ChannelHandler需要調(diào)用ReferenceCountUtil#retain方法。* */@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {System.out.println("ServerHandler receive msg:"+msg.toString());//寫消息:先得到channel,在寫如通道然后flush刷新通道把消息發(fā)出去。ctx.channel().writeAndFlush("this is ServerHandler reply msg happend at !"+System.currentTimeMillis());//把消息往下一個(gè)Handler傳ctx.fireChannelRead(msg);} }

Handler2

package com.mym.netty.server;import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter;public class ServerHandler2 extends ChannelInboundHandlerAdapter {@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {System.out.println("ServerHandler2 receive msg:"+msg.toString());ctx.channel().writeAndFlush("this is ServerHandler2 reply msg happend at !"+System.currentTimeMillis());} }

Netty4實(shí)現(xiàn)客戶端

客戶端服務(wù)類

package com.mym.netty.client;import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder;import java.io.BufferedReader; import java.io.InputStreamReader;/*** netty客戶端*/ public class NettySingleClient {public static void main(String[] args) {startClient();}public static void startClient(){//1.定義服務(wù)類Bootstrap clientBootstap = new Bootstrap();//2.定義執(zhí)行線程組EventLoopGroup worker = new NioEventLoopGroup();//3.設(shè)置線程池clientBootstap.group(worker);//4.設(shè)置通道clientBootstap.channel(NioSocketChannel.class);//5.添加HandlerclientBootstap.handler(new ChannelInitializer<Channel>() {@Overrideprotected void initChannel(Channel channel) throws Exception {System.out.println("client channel init!");ChannelPipeline pipeline = channel.pipeline();pipeline.addLast("StringDecoder",new StringDecoder());pipeline.addLast("StringEncoder",new StringEncoder());pipeline.addLast("ClientHandler",new ClientHandler());}});//6.建立連接ChannelFuture channelFuture = clientBootstap.connect("0.0.0.0",9099);try {//7.測試輸入BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));while(true){System.out.println("請輸入:");String msg = bufferedReader.readLine();channelFuture.channel().writeAndFlush(msg);}} catch (Exception e) {e.printStackTrace();}finally {//8.關(guān)閉連接worker.shutdownGracefully();}} }

客戶端的handler

package com.mym.netty.client;import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter;public class ClientHandler extends ChannelInboundHandlerAdapter {@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {System.out.println("client receive msg:"+msg.toString());} }

測試

啟動(dòng)服務(wù)端和客戶端后,客戶端發(fā)送nihao!服務(wù)端回應(yīng),然后客戶端發(fā)送hello,服務(wù)端回應(yīng)。?
服務(wù)端輸出

success to initHandler! ServerHandler receive msg:nihao! ServerHandler2 receive msg:nihao! ServerHandler receive msg:hello ServerHandler2 receive msg:hello

客戶端輸出

client channel init! 請輸入: nihao! 請輸入: client receive msg:this is ServerHandler reply msg happend at !1531893027697this is ServerHandler2 reply msg happend at !1531893027698 hello 請輸入: client receive msg:this is ServerHandler reply msg happend at !1531893045446this is ServerHandler2 reply msg happend at !1531893045447

小結(jié)

需要注意的是,服務(wù)端和客戶端除了啟動(dòng)類和socket channel不一樣以外,其他幾乎一致的操作。

總結(jié)

以上是生活随笔為你收集整理的Netty4服务端和客户端实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 大陆农村乡下av | 午夜精品国产精品大乳美女 | 伊人国产在线视频 | 韩国三级hd中文字幕的背景音乐 | 午夜av网站 | 国产国语videosex另类 | 中文字幕日韩一级 | 国产调教 | 国产一区在线播放 | 久久嫩| av免费片| 黄色一级免费大片 | 天天躁日日躁aaaxxⅹ | 911成人网| 理论片91 | 春闺艳妇(h)高h产乳 | 偷拍亚洲综合 | 99免费精品| 日本不卡一区在线观看 | 91视频地址 | 夫妻性生活黄色片 | 黄色的网站在线观看 | 自拍偷拍欧美亚洲 | 日韩六十路 | 体内射精一区二区 | 特黄老太婆aa毛毛片 | a级在线免费观看 | 免费视频网站在线观看入口 | 第一章豪妇荡乳黄淑珍 | 色小说在线 | 性囗交免费视频观看 | 国产无码精品在线播放 | 一区二区 中文字幕 | 全黄一级片 | 成年人视频网站 | 久久伊人超碰 | 亚洲在线网站 | 亚洲高清视频免费观看 | 性色av一区二区三区红粉影视 | 亚洲夜色 | 免费在线观看成人av | 日本不卡一区二区三区视频 | 精品一区二区日韩 | 天堂中文在线看 | 国产性爱精品视频 | 天天夜夜人人 | 香蕉国产精品 | 美女被c出水 | 在线看av的网址 | 免费a视频在线观看 | sese在线视频| 北条麻妃一区二区三区 | 特级精品毛片免费观看 | av日韩一区二区 | 人体写真 福利视频 | 国产激情无码一区二区 | 97操碰| 日韩国产精品一区 | 国产精品你懂得 | 免费观看的av网站 | 久久久久亚洲AV成人网人人小说 | 日韩福利视频网 | av直接看 | 奇米综合网 | 雪花飘电影在线观看免费高清 | 国产福利一区二区三区 | 日本黄区免费视频观看 | 久久影视大全 | 成人一级网站 | 毛片aa| 美女福利视频导航 | 成人公开视频 | 青草一区 | 精品国产乱码久久久久久牛牛 | 狂野欧美性猛交xxxx巴西 | 国产超碰91| 少女国产免费观看 | 日韩视频在线观看一区二区三区 | 日韩在线视频网站 | 99艹| 色天天综合网 | 乱老熟女一区二区三区 | 国产91精品一区二区 | av在线影音 | 91九色网| 日本在线资源 | 男女视频免费网站 | 婷婷久久久久久 | 亚洲自拍偷拍av | 手机看片1024欧美 | 性少妇videosexfreexxx片 | 涩涩网站在线观看 | 国产高清免费 | 国产又色又爽又黄又免费 | 成人午夜影视在线观看 | 不卡的毛片 | 色播日韩 | 好看的中文字幕电影 | 激情四月 |