netty簡單筆記

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

Server

package com.netty;import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
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.string.StringDecoder;public class Server {public static void main(String[] args) throws Exception {//1.第一個線程是用于接收client連接的EventLoopGroup bossGroup = new NioEventLoopGroup();//2.第二個線程是用于實際的業務處理操作的EventLoopGroup workGroup = new NioEventLoopGroup();ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel sc) throws Exception {ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes());sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf));   //分隔符sc.pipeline().addLast(new StringDecoder());//發送string類型sc.pipeline().addLast(new ServerHandler());}}).option(ChannelOption.SO_KEEPALIVE, true);ChannelFuture f = b.bind(1234).sync();f.channel().closeFuture().sync();bossGroup.shutdownGracefully();workGroup.shutdownGracefully();}
}

?

?

ServerHandler

package com.netty;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.ReferenceCountUtil;public class ServerHandler extends ChannelHandlerAdapter {@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {try {/*ByteBuf buf = (ByteBuf) msg;byte[] data = new byte[buf.readableBytes()];buf.readBytes(data);String request = new String(data,"utf-8");System.out.println(request);*/System.out.println("--------->"+msg);String response = "Hi Client";ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));} finally {ReferenceCountUtil.release(msg);}}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {// TODO Auto-generated method stubsuper.exceptionCaught(ctx, cause);}
}

?

?

Client

package com.netty;import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
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.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;public class Client {public static void main(String[] args) throws Exception {EventLoopGroup group = new NioEventLoopGroup();Bootstrap b = new Bootstrap();b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel sc) throws Exception {sc.pipeline().addLast(new ClientHandler());}});ChannelFuture f = b.connect("127.0.0.1", 1234).sync();f.channel().write(Unpooled.copiedBuffer("hello word$_".getBytes()));f.channel().write(Unpooled.copiedBuffer("hello word$_".getBytes()));f.channel().write(Unpooled.copiedBuffer("hello word$_".getBytes()));f.channel().flush();f.channel().closeFuture().sync();group.shutdownGracefully();}
}

?

?

ClientHandler

package com.netty;import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;public class ClientHandler extends ChannelHandlerAdapter {@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf buf = (ByteBuf) msg;byte[] data = new byte[buf.readableBytes()];buf.readBytes(data);String request = new String(data,"utf-8");System.out.println(request);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {// TODO Auto-generated method stubsuper.exceptionCaught(ctx, cause);}
}

?

?

?

?

?

?

?

轉載于:https://my.oschina.net/u/2562032/blog/891889

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/258880.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/258880.shtml
英文地址,請注明出處:http://en.pswp.cn/news/258880.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c語言與python通信_python和c++通信示例

先貼一個大牛寫的python與C的通信的經典文章&#xff1a;如何實現 C/C 與 Python 的通信&#xff1f; 里面講到了不少方法來實現C和python之間的通信&#xff0c;我看了之后深有感觸&#xff0c;但里面的例程序大多都是int或者string這樣容易轉換的&#xff0c;但如果是list呢&…

halcon/c++接口基礎 之 控制參數

HALCON/C可以處理各種不同類型的字母數字混合的控制參數&#xff0c;如下&#xff1a; 離散數字&#xff08;long&#xff09;浮點數字&#xff08;double&#xff09;字符串&#xff08;char*&#xff09; 控制參數的一個特殊形式是句柄&#xff0c;提供了途徑去訪問復雜的數…

C#使用多態求方形面積周長和圓的面積周長

class class1{public static void Main(string[] args){//使用多態求矩形面積與周長和圓的面積與周長Shape cl new Circle(5);double clarea cl.GetArea();double clpar cl.GetPerimeter();Console.WriteLine("這個圓的面積是{0},周長是{1}", Math.Round(clarea, …

Java編程的邏輯 (84) - 反射

?本系列文章經補充和完善&#xff0c;已修訂整理成書《Java編程的邏輯》&#xff0c;由機械工業出版社華章分社出版&#xff0c;于2018年1月上市熱銷&#xff0c;讀者好評如潮&#xff01;各大網店和書店有售&#xff0c;歡迎購買&#xff0c;京東自營鏈接&#xff1a;http://…

C# 與 VC Dll 傳輸信息

考慮&#xff1a; 使用string類型傳送&#xff1b; 在VC Dll中解析字符&#xff1b; 使用 string 類型將解析的類型傳送到C#程序中&#xff1b; 建立VC解析的函數&#xff0c;提高代碼可重用性轉載于:https://www.cnblogs.com/ein-key5205/p/3597612.html

linux下python_linux下python安裝

Python2.5的安裝方法&#xff1a; 1&#xff0e;下載源代碼 http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tar.bz2 2&#xff0e; 安裝 $ tar –jxvf Python-2.5.2.tar.bz2 $ cd Python-2.5.2 $ ./configure $ make $ make install 3. 測試 在命令行下輸入python&…

灰度圖像的8位平面分解

所謂灰度圖像&#xff0c;即指8位256顏色的圖像。將圖像的每一位分別取出來&#xff0c;我們就可以將一幅圖像分解開來&#xff0c;形成8幅圖像。下面我們分別介紹使用matlab分解圖像與使用halcon/c分解圖像的方法。 matlab8位分解 clc; clear all; A imread(lena.tif); % 顯…

Win10 UAP 綁定

Compiled DataBinding in Windows Universal Applications (UAP) http://nicksnettravels.builttoroam.com/post/2015/04/26/Compiled-DataBinding-in-Windows-Universal-Applications-(UAP).aspx 讀寫剪貼板 http://www.cnphp6.com/archives/80079 Learn how the Reversi samp…

HDUOJ----4501小明系列故事——買年貨(三維背包)

小明系列故事——買年貨 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 2146 Accepted Submission(s): 953 Problem Description春節將至&#xff0c;小明要去超市購置年貨&#xff0c;于是小明去了自己經常去…

css 橫線_atom.css正式發布,從此跟CSS框架說拜拜。

atom.css 大家好&#xff0c;我寫了一個css庫atom.css&#xff0c;蠻好用的&#xff0c;所以忍不住分享給大家。(https://github.com/MatrixAge/atom.css)起因寫HTML幾年了&#xff0c;再到如今的JSX&#xff0c;最大的感受不是枯燥&#xff0c;而是眼花。寫樣式的時候&#xf…

halcon模板匹配學習(一) Matching 初印象

什么是模板匹配呢&#xff1f;簡單而言&#xff0c;就是在圖像中尋找目標圖像&#xff08;模板&#xff09;&#xff0c;或者說&#xff0c;就是在圖像中尋找與模板圖像相似部分的一種圖像處理技術。依賴于選擇的方法不同&#xff0c;模板匹配可以處理各種情形下的變換&#xf…

第五章 面向方面編程___AOP入門

上一篇講了 AOP 和 OOP 的區別&#xff0c;這一次我們開始入門 AOP 。實現面向方面編程的技術&#xff0c;主要分為兩大類&#xff1a; 一是 采用動態代理技術&#xff0c;利用截取消息的方式&#xff0c;對該消息進行裝飾&#xff0c;以取代原有對象行為的執行&#xff1b; 二…

java將xml中的標簽名稱轉為小寫_深入學習Java Web(七): JSTL標簽庫

本文轉自與博客園一杯涼茶的博客.在之前我們學過在JSP頁面上為了不使用腳本&#xff0c;所以我們有了JSP內置的行為、行為只能提供一小部分的功能&#xff0c;大多數的時候還是會用java腳本&#xff0c;接著就使用了EL表達式&#xff0c;基本上EL表達式看似能滿足我們的要求&am…

python中*args和**args的不同

上一段代碼&#xff0c;大家感受一下 def test_param(*args): print(args) def test_param2(**args): print(args) test_param(test1,test2) >>>(test1,test2) test_param2(p1test1,p2test2) >>>{p1:test1, p2:test2} python提供了兩種特別的方法來定義函數的…

halcon模板匹配學習(二) 準備模板

如下&#xff0c;我們將介紹匹配的第一個操作&#xff1a;準備模板 初始時刻&#xff0c;我們準備好參考圖像&#xff0c;并對其做一定的處理&#xff0c;然后我們需要從參考圖像中導出模板&#xff0c;也就是將參考圖像裁剪成所謂的模板圖像。獲取模板圖像可以通過設置ROI來完…

揭秘繼承技術之虛函數

虛函數 調用虛函數時函數行為將根據對象所屬類的不同而變化。 父類指針或引用指向子類對象時&#xff0c;可訪問子類重寫方法&#xff08; virtual函數&#xff09;但無法訪問在父類中沒有定義的子類方法和數據成員。 #include <iostream>using namespace std;class Supe…

java中GET方式提交和POST方式提交

java中GET方式提交的示例&#xff1a; /*** 獲取關注列表;* return*/SuppressWarnings("unchecked")public static ArrayList<String> getUserList() {StringBuffer bufferRes new StringBuffer();ArrayList<String> users null;try {URL realUrl new…

基于HALCON的模板匹配方法總結

很早就想總結一下前段時間學習HALCON的心得&#xff0c;但由于其他的事情總是抽不出時間。去年有過一段時間的集中學習&#xff0c;做了許多的練習和實驗&#xff0c;并對基于HDevelop的形狀匹配算法的參數優化進行了研究&#xff0c;寫了一篇《基于HDevelop的形狀匹配算法參數…

js 數組移除_2020前端面試--常見的js面試題

&#xff08;答案持續更新...&#xff09; 1.簡述同步和異步的區別js是一門單線程語言&#xff0c;所謂"單線程"&#xff0c;就是指一次只能完成一件任務。如果有多個任務&#xff0c;就必須排隊&#xff0c;前面一個任務完成&#xff0c;再執行后面一個任務&#xf…

spring-自動加載配置文件\使用屬性文件注入

在上一篇jsf環境搭建的基礎上 , 加入spring框架 , 先看下目錄結構 src/main/resources 這個source folder 放置web項目所需的主要配置,打包時,會自動打包到WEB-INF下 首先看下pom.xml,需要引入一些依賴項: 1 <project xmlns"http://maven.apache.org/POM/4.0.0" x…