Mina.Net实现的UDP协议消息收发Demo

别等时光非礼了梦想. 提交于 2020-03-31 16:37:11
using Mina.Filter.Codec;
using Mina.Filter.Codec.TextLine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Mina.Core.Session;
using System.Threading;

namespace MinaDemo
{
    class UDPMessageDemo
    {
        static void Main(string[] args)
        {
            var port = 1024;
            //服务端
            var acceptor = new Mina.Transport.Socket.AsyncDatagramAcceptor();
            acceptor.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8)));
            acceptor.MessageReceived += (ss, ee) =>//接收到消息后
            {
                Console.WriteLine("服务端收到" + ee.Session.RemoteEndPoint + "的消息:" + ee.Message + ",并广播:I love you all");

                acceptor.Broadcast("I love you all");
                //只有服务端接收到客户端发来消息时,才会把客户端存放到acceptor.ManagedSessions中,才能广播到。超过1分钟后,如果没有收到 Session,就会自动掉线的。
            };
            acceptor.SessionIdle += (ss, ee) =>
            {

            };
            acceptor.SessionCreated += (ss, ee) => { ee.Session.Config.SetIdleTime(IdleStatus.BothIdle, 10); };
            acceptor.Bind(new IPEndPoint(IPAddress.Loopback, port));


            //客户端
            var connector = new Mina.Transport.Socket.AsyncDatagramConnector();
            connector.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8)));
            
            var session = connector.Connect(new IPEndPoint(IPAddress.Loopback, port)).Await().Session;
            connector.MessageReceived += (ss, ee) =>
            {
                Console.WriteLine("客户端1收到:" + ee.Message);
            };


            //客户端2
            var connector2 = new Mina.Transport.Socket.AsyncDatagramConnector();
            connector2.FilterChain.AddLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Encoding.UTF8)));
            var session2 = connector2.Connect(new IPEndPoint(IPAddress.Loopback, port)).Await().Session;
            connector2.MessageReceived += (ss, ee) =>
            {
                Console.WriteLine("客户端2收到:" + ee.Message);
            };

            //让客户端1发送并连接
            session.Write("i am 1");
            Console.WriteLine("客户端1发送:i am 1");


            int count = 0;
            //发送
            while (true)
            {
                Thread.Sleep(1000);
                var line = Console.ReadLine();
                count++;
                Console.WriteLine("输入exit退出:" + count);

                if (line == "exit")
                {
                    session.Close(true);
                    acceptor.Unbind();
                    acceptor.Dispose();
                    break;
                }
                session2.Write("I Am Client2 " + line);
                Console.WriteLine("客户端2发送:" + "I Am Client2 " + line);
            }
        }


    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!