• 大小: 42.52MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-06-22
  • 语言: 其他
  • 标签: dotnetty  

资源简介

https://blog.csdn.net/qq_34719168/article/details/87893803 博文项目源代码

资源截图

代码片段和文件信息

using CommonLib;
using DotNetty.Buffers;
using DotNetty.Handlers.Timeout;
using DotNetty.Transport.Bootstrapping;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels.Sockets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Client
{
    class Program
    {
        static void Main(string[] args) => RunClientAsync().Wait();

        static async Task RunClientAsync()
        {
            var group = new MultithreadEventLoopGroup();
            try
            {
                var bootstrap = new Bootstrap();
                bootstrap
                    .Group(group)
                    .Channel()
                    .Option(ChannelOption.TcpNodelay true)
                    .Handler(new ActionChannelInitializer(c =>
                    {
                        IChannelPipeline pipeline = c.Pipeline;

                        //配置编码解码器
                        pipeline.AddLast(new CommonClientEncoder());
                        pipeline.AddLast(new CommonClientDecoder());

                        // IdleStateHandler 心跳
                        //客户端为写IDLE
                        pipeline.AddLast(new IdleStateHandler(0 100 0));//第一个参数为读,第二个为写,第三个为读写全部

                        pipeline.AddLast(new HelloClientHandler());
                    }));

                IChannel clientChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(“127.0.0.1“) 3399));
                Console.ReadLine();
                await clientChannel.CloseAsync();
            }
            finally
            {
                await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100) TimeSpan.FromSeconds(1));
            }
        }
    }

    class HelloClientHandler : ChannelHandlerAdapter
    {
        public override void UserEventTriggered(IChannelHandlerContext context object evt)
        {
            Console.WriteLine(“客户端循环心跳监测发送: “ + DateTime.Now);
            if (evt is IdleStateEvent eventState)
            {
                if (eventState.State == IdleState.WriterIdle)
                {
                    context.WriteAndFlushAsync($“biubiu:{DateTime.Now}“);
                }
            }
        }

        public override void ChannelActive(IChannelHandlerContext context)
        {
            Console.WriteLine(“我是客户端.“);
            Console.WriteLine($“连接至服务端{context}.“);

            //编码器发送至服务端
            string message = “客户端1“;
            context.WriteAndFlushAsync(message);
        }

        public override void ChannelRead(IChannelHandlerContext context object message)
        {
            if (message is byte[] o)
            {
                Console.WriteLine($“解码器方式,从服务端接收:{Encoding.UTF8.GetString(o)}:{DateTime.Now}“);
            }
        }

        public override void ChannelReadCompl

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        174  2019-02-23 16:07  Lession2\.vs\Lession2\DesignTimeBuild\.dtbcache

    ..A..H.    395776  2019-02-23 17:26  Lession2\.vs\Lession2\v15\.suo

     文件          0  2019-02-23 13:22  Lession2\.vs\Lession2\v15\Server\sqlite3\db.lock

     文件       4096  2019-02-23 13:22  Lession2\.vs\Lession2\v15\Server\sqlite3\storage.ide

     文件      32768  2019-02-23 17:20  Lession2\.vs\Lession2\v15\Server\sqlite3\storage.ide-shm

     文件    3662712  2019-02-23 17:24  Lession2\.vs\Lession2\v15\Server\sqlite3\storage.ide-wal

     文件        838  2019-02-23 11:33  Lession2\Client\App.config

     文件       9216  2019-02-23 17:24  Lession2\Client\bin\Debug\Client.exe

     文件      18334  2019-02-23 12:16  Lession2\Client\bin\Debug\Client.exe.config

     文件      22016  2019-02-23 17:24  Lession2\Client\bin\Debug\Client.pdb

     文件       5120  2019-02-23 16:23  Lession2\Client\bin\Debug\CommonLib.dll

     文件        712  2019-02-23 16:08  Lession2\Client\bin\Debug\CommonLib.dll.config

     文件      17920  2019-02-23 16:23  Lession2\Client\bin\Debug\CommonLib.pdb

     文件     175184  2018-10-08 10:15  Lession2\Client\bin\Debug\DotNetty.Buffers.dll

     文件     130620  2018-10-08 16:55  Lession2\Client\bin\Debug\DotNetty.Buffers.xml

     文件     140392  2018-10-08 10:14  Lession2\Client\bin\Debug\DotNetty.Codecs.dll

     文件      38209  2018-10-08 16:55  Lession2\Client\bin\Debug\DotNetty.Codecs.xml

     文件     153168  2018-10-08 10:14  Lession2\Client\bin\Debug\DotNetty.Common.dll

     文件     146861  2018-10-08 16:55  Lession2\Client\bin\Debug\DotNetty.Common.xml

     文件      52296  2018-10-08 10:14  Lession2\Client\bin\Debug\DotNetty.Handlers.dll

     文件      32387  2018-10-08 16:55  Lession2\Client\bin\Debug\DotNetty.Handlers.xml

     文件     180816  2018-10-08 10:14  Lession2\Client\bin\Debug\DotNetty.Transport.dll

     文件     198320  2018-10-08 16:55  Lession2\Client\bin\Debug\DotNetty.Transport.xml

     文件      35320  2016-11-14 20:41  Lession2\Client\bin\Debug\Microsoft.Extensions.DependencyInjection.Abstractions.dll

     文件      53520  2016-11-14 18:50  Lession2\Client\bin\Debug\Microsoft.Extensions.DependencyInjection.Abstractions.xml

     文件      44032  2017-02-17 17:01  Lession2\Client\bin\Debug\Microsoft.Extensions.Logging.Abstractions.dll

     文件      33625  2017-02-17 13:48  Lession2\Client\bin\Debug\Microsoft.Extensions.Logging.Abstractions.xml

     文件      18432  2017-02-17 17:01  Lession2\Client\bin\Debug\Microsoft.Extensions.Logging.dll

     文件       1652  2017-02-17 13:48  Lession2\Client\bin\Debug\Microsoft.Extensions.Logging.xml

     文件      21216  2018-11-08 16:24  Lession2\Client\bin\Debug\Microsoft.Win32.Primitives.dll

............此处省略5152个文件信息

评论

共有 条评论