Nuget 引用

client 端

Install-Package DotNetty.Codecs  -Version 0.6.0
Install-Package DotNetty.Handlers -Version 0.6.0
Install-Package DotNetty.Transport -Version 0.6.0

Server端

Install-Package DotNetty.Codecs  -Version 0.6.0
Install-Package DotNetty.Transport -Version 0.6.0

服务端代码

        static void Main(string[] args){Server server = new Server();server.Listen(9999).Wait();Console.WriteLine("服务启动");Console.ReadLine();}
    /// <summary>/// 服务/// </summary>public class Server{public async Task Listen(int port){var boos = new MultithreadEventLoopGroup(1);var work = new MultithreadEventLoopGroup();var bootstrap = new ServerBootstrap();bootstrap.Group(boos, work).Channel<TcpServerSocketChannel>().Option(ChannelOption.SoBacklog, 100).ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel=> {IChannelPipeline pipeline = channel.Pipeline;pipeline.AddLast(new ServerMessageHandler());}));IChannel boundChannel = await bootstrap.BindAsync(port);}}
    public class ServerMessageHandler : ChannelHandlerAdapter{public override void ChannelRead(IChannelHandlerContext context, object message){var buffer = message as IByteBuffer;if (buffer != null){string receiveData = buffer.ToString(Encoding.UTF8);Console.WriteLine("服务端获取到:" + receiveData);byte[] messageBytes = Encoding.UTF8.GetBytes(receiveData);IByteBuffer byteBuffer = Unpooled.Buffer(256);byteBuffer.WriteBytes(messageBytes);context.WriteAndFlushAsync(byteBuffer);}}public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();public override void ExceptionCaught(IChannelHandlerContext context, Exception exception){context.CloseAsync();}}

客户端代码

        static void Main(string[] args){Clinet clinet = new Clinet();clinet.Connect("127.0.0.1", 9999);Console.WriteLine("客户端开始连接!");string content = "";while (!(content = Console.ReadLine()).Contains("Exit", StringComparison.InvariantCultureIgnoreCase)){clinet.SendMessage(content);}Console.ReadLine();}
    /// <summary>/// 客户端/// </summary>public class Clinet{public static IChannel Channel { set; get; }public static IEventLoopGroup Group { set; get; }public static ClientMessageHandler clientMessageHandler = new ClientMessageHandler();public async void Connect(string host, int port){Group = new MultithreadEventLoopGroup();var bootstrap = new Bootstrap();bootstrap.Group(Group).Channel<TcpSocketChannel>().Option(ChannelOption.TcpNodelay, true).Handler(new ActionChannelInitializer<ISocketChannel>(channel =>{IChannelPipeline pipeline = channel.Pipeline;pipeline.AddLast(clientMessageHandler);}));Channel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(host), port));}public async void Close(){await Channel.CloseAsync();await Group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));}/// <summary>/// 发送消息/// </summary>/// <param name="message"></param>public bool SendMessage(string message){if (message == null){return false;}IByteBuffer buffer = Unpooled.Buffer(256);byte[] messageBytes = Encoding.UTF8.GetBytes(message);buffer.WriteBytes(messageBytes);Channel.WriteAndFlushAsync(buffer);return true;}}
    public class ClientMessageHandler : ChannelHandlerAdapter{public override void ChannelRead(IChannelHandlerContext context, object message){var byteBuffer = message as IByteBuffer;if (byteBuffer != null){var data = byteBuffer.ToString(Encoding.UTF8);Console.WriteLine("客户端收到:" + data);}}public override void ChannelReadComplete(IChannelHandlerContext context) => context.Flush();public override void ExceptionCaught(IChannelHandlerContext context, Exception exception){Console.WriteLine("Exception: " + exception);context.CloseAsync();}}

最后的效果

效果不错,第一次学习,就搞出来了。加油。

高性能 DotNetty Server Clinet Demo .NetCore相关推荐

  1. Nginx + FastCGI 程序(C/C++) 搭建高性能web service的Demo及部署发布

    1.介绍     Nginx - 高性能web server,这个不用多说了,大家都知道.     FastCGI程序 - 常驻型CGI程序,它是语言无关的.可伸缩架构的CGI开放扩展,其主要行为是将 ...

  2. 实战:Nginx + FastCGI 程序(C/C++) 搭建高性能web server

    1.介绍 Nginx - 高性能web server,这个不用多说了,大家都知道. FastCGI程序 - 常驻型CGI程序,它是语言无关的.可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程 ...

  3. 【入门篇】Nginx + FastCGI 程序(C/C++) 搭建高性能web service的Demo及部署发布

    http://blog.csdn.net/allenlinrui/article/details/19419721 分类: C/C++2014-02-18 17:58 3875人阅读 评论(0) 收藏 ...

  4. kestrel web服务器性能对比,Asp.Net Core 3.0 Kestrel服务器下 高性能 WebSocket Server

    最近研究.net core 的各种高性能类型,内存池之类的东西,基于kestrel 服务器的websocket ,写个例子练下手 把原生的Websocket用ArrayPool,Memory改造了下, ...

  5. nginx怎么控制socket链接转发到某台服务器_Nginx + FastCGI 程序(C/C++) 搭建高性能web service...

    1.介绍 Nginx - 高性能web server,这个不用多说了,大家都知道. FastCGI程序 - 常驻型CGI程序,它是语言无关的.可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程 ...

  6. 1分钟搭建极简mock server

    摘自博客园:https://www.cnblogs.com/mikasama/p/9838480.html 1.无聊的背景.起源: 如今的业务系统越来越复杂庞大,各个功能直接的调用也是多如牛毛,但如果 ...

  7. 基于微信小程序开发的仿微信demo

    (本文参考自github/liujians,地址:https://github.com/liujians/weApp) 作者声明: 基于微信小程序开发的仿微信demo  整合了ionic的样式库和we ...

  8. Linux下HTTP Server

    想在Linux下实现一个简单的web Server并不难.一个最简单的HTTP Server不过是一个高级的文件服务器,不断地接收客户端(浏览器)发送的HTTP请求,解析请求,处理请求,然后像客户端回 ...

  9. aws waf sql注入_适用于快速SQL Server Always On部署的AWS启动向导

    aws waf sql注入 SQL Server Always On is a high-availability and disaster recovery solution. We can use ...

最新文章

  1. Jvm原理剖析与调优之内存结构
  2. Java 12:开关表达式
  3. 你让,勋爵? 使用Jenkins声明性管道的Docker中的Docker
  4. 什么是真正的高清,你知道吗?
  5. 8篇SCI,总影响因子46.464!这位博士,毕业后带着女友一起援疆!
  6. 人机猜拳代码python_python实现人机猜拳小游戏
  7. linux如果时间等于22点,Linux下ntpdate时间同步
  8. 学习笔记(二) 消息队列
  9. 洛谷P2024 [NOI2001]食物链
  10. 解决办法:无法获得锁 /var/lib/dpkg/lock-frontend - open (11: 资源暂时不可用)
  11. android studio for android learning (二十六 )自定义控件理解与浅析(1)
  12. 【Java基础】使用NumberFormat 、DecimalFormat进行数字格式化
  13. diff git 代码实现_Git比对文件之间的差异
  14. MySQL 数据对比
  15. 中国石油天然气行业供需格局及消费需求前景调查报告2021年版
  16. PHP 之建行龙支付-被扫(商家扫码客户二维码),扫码枪使用
  17. 训练创新思维的方法:曼陀罗思考法
  18. web.xml中security-constraint安全认证标签说明
  19. get请求400神坑
  20. 中国股市:如果历史会重复?

热门文章

  1. 3D点云系列(一)点云介绍
  2. 十四条令PHP初学者头疼问题大总结(1)
  3. R语言特征提取与特征选择
  4. Thinkpad E440启动顺序设置
  5. java获取PDF页面长宽
  6. 连接别的电脑共享的打印机方法
  7. 京东商城组织架构调整:划分为前中后台 强调实现有质量增长
  8. Gmail相关一些工具
  9. Vue工程化项目创建-VueCli与Vite
  10. Ubuntu / Linux 各种操作指南