NetCore 使用 SimpleTCP 实现双工通信

十年河东,十年河西,莫欺少你穷

学无止境,精益求精

1、新建 netcore 控制台应用程序并引入包

 2、服务端

using SimpleTCP;
using System;
using System.Net;
using System.Text;

namespace TcpServe
{
    class Program
    {
        static void Main(string[] args)
        {
            var server = new SimpleTcpServer();
            //设置编码格式,默认是UTF8
            server.StringEncoder = System.Text.ASCIIEncoding.UTF8;
            server.Delimiter = Encoding.ASCII.GetBytes("\r")[0];
            server.Start(IPAddress.Parse("127.0.0.1"), 8080);
            server.ClientConnected += Server_ClientConnected;
            server.ClientDisconnected += Server_ClientDisconnected;
            server.DataReceived += Server_DataReceived;

            Console.ReadKey();

        }

        private static void Server_DataReceived(object sender, Message e)
        {

            Console.WriteLine("收到来自客户端的信息[" + e.TcpClient.Client.RemoteEndPoint.ToString() + "]" + e.MessageString);

            e.Reply(""+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        private static void Server_ClientDisconnected(object sender, System.Net.Sockets.TcpClient e)
        {
            Console.WriteLine("客户端断开信息[" + e.Client.RemoteEndPoint.ToString() + "]");
        }

        private static void Server_ClientConnected(object sender, System.Net.Sockets.TcpClient e)
        {

            Console.WriteLine("客户端连接信息[" + e.Client.RemoteEndPoint.ToString() + "]");
        }
    }
}
View Code

3:客户端

using SimpleTCP;
using System;
using System.Text;
using System.Threading.Tasks;

namespace TcpService
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化
            var client = new SimpleTcpClient();
           

            //设置编码格式,默认是UTF8
            client.StringEncoder = System.Text.ASCIIEncoding.UTF8;
            //设置分隔符,默认是0x13
            client.Delimiter = Encoding.ASCII.GetBytes("\r")[0];

            //收到数据的事件,可以在这里实现自己的协议
            client.DataReceived += (sender, msg) =>
            {

                //字符串消息
                Console.WriteLine("收到来自服务器的消息:"+ msg.MessageString);
            };

            bool exit = false;
            bool connected = false;
            Task.Factory.StartNew(() =>
            {
                while (!exit)
                {
                    try
                    {
                        if (connected)
                        {
                            //发送心跳
                            client.Write(""+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                            Task.Delay(2000).Wait();
                        }
                        else
                        {
                            //断线重连
                            client.Connect("127.0.0.1", 8080);
                            connected = true;
                            Task.Delay(1000).Wait();
                        }
                    }
                    catch (Exception)
                    {
                        connected = false;
                        client.Disconnect();
                    }
                }
            }, TaskCreationOptions.LongRunning);

            Console.ReadKey(); 

        }
    }
}
View Code

4、运行结果

 开启了多个客户端

posted @ 2024-10-09 17:03  天才卧龙  阅读(20)  评论(0编辑  收藏  举报