.NET Socket通信

server 端
csharp 复制代码
Socket serverSocket = null;
        Socket ClientSocket = null;
        List<Socket> listSocket=new List<Socket>();

        private void btnlisten_Click(object sender, EventArgs e)
        {
            serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse(txtIP.Text);
            IPEndPoint endpoint = new IPEndPoint(ip, int.Parse(txtPort.Text));
            serverSocket.Bind(endpoint);
            //同时监控几个客户端
            serverSocket.Listen(10);
            Task.Run((Action)ListentConnectSocket);//等价于 Action action=ListentConnectSocket;Task.Run(action);
            //方法2
            //Thread thd = new Thread(ListentConnectSocket);
            //thd.IsBackground = true;
            //thd.Start();
        }
       
        void ListentConnectSocket()
        {
            // 死循环
            while (true)
            {              
                try
                {
                    ClientSocket = serverSocket.Accept(); //放在UI线程,会阻塞,所以放在另外一个线程
                   
                    string sendMsg = "恭喜您,服务器连接成功!";
                    ClientSocket.Send(Encoding.Default.GetBytes(sendMsg));
                    //客户端IP、端口号
                    string ClientInfo = ClientSocket.RemoteEndPoint.ToString();
                    //方法1
                    //Action<string> action = delegate (string msg)
                    //{
                    //    lsReceive.Items.Add(msg);
                    //};
                    //lsReceive.Invoke(action, ClientInfo);
                    //方法2 匿名方法
                    //lsReceive.Invoke((Action<string>)delegate(string msg) {
                    //    lsReceive.Items.Add(msg);
                    //},ClientInfo);
                    // 方法3 lamda
                    lsReceive.Invoke(new Action<string>((msg) =>
                    {
                        listSocket.Add(ClientSocket);
                        lsReceive.Items.Add($"{msg} 上线啦");
                    }), ClientInfo);
                }
                catch (Exception ex)
                {

                    lsReceive.Invoke(new Action<string>((msg) =>
                    {
                        lsReceive.Items.Add(msg);
                    }), $"发送信息失败;{ex.Message}");
                    break;
                }
               

                //开启接收信息
                //方法1
                //Thread thr = new Thread(ReceiveClientMsg);
                //thr.IsBackground = true;
                //thr.Start(ClientSocket);
                //方法2
                Action<Socket> action = new Action<Socket>(ReceiveClientMsg);
                Task.Run(()=>action(ClientSocket));
              
            }
        }
        private void ReceiveClientMsg(object clientSocket)
        {
            Socket client = clientSocket as Socket;
            while (true)
            {
                byte[] recBuffer = new byte[1024];
                int length = -1;
                try
                {
                    //Receive 是阻塞方法
                    length = client.Receive(recBuffer);
                }
                catch (Exception ex)
                {
                    //客户端关闭应用程序
                    string str = client.RemoteEndPoint.ToString();
                    this.Invoke(new Action(() =>
                    {
                        lsReceive.Items.Add($"接收客户端:{str} 异常; {ex.Message}");
                    }));
                    break;
                }
                if (length == 0)
                {
                    string str = client.RemoteEndPoint.ToString();
                    this.Invoke(new Action(() =>
                    {
                        lsReceive.Items.Add($"{str}:下线了!");
                    }));
                    break;
                }
                else
                {
                    string msg = Encoding.Default.GetString(recBuffer, 0, length);
                    string msgStr = $"{DateTime.Now}【 接收客户端: {client.RemoteEndPoint.ToString()}】--- {msg}";
                    this.Invoke(new Action(() =>
                    {
                        lsReceive.Items.Add(msgStr + Environment.NewLine);
                    }));
                }
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {

            Task.Run(new Action(()=> {
                foreach (Socket item in listSocket)
                {
                    item.Send(Encoding.Default.GetBytes(txtSend.Text));
                }
              
            }));
            
        }

客户端

csharp 复制代码
  // 创建 Socket对象
        Socket clientSocket = null;
        private void btnConnect_Click(object sender, EventArgs e)
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text));
            try
            {
                lstReceive.Items.Add("与服务器连接中。。。。");
                clientSocket.Connect(endPoint);
                //方法1
                Action action = ReceiveMsg;
                Task.Run(action);//开启线程
                if (clientSocket.Connected)
                {
                    lstReceive.Items.Add("服务器连接成功。。。。");
                }
                //方法2
                //Thread thd = new Thread(ReceiveMsg);
                //thd.IsBackground = true;
                //thd.Start();
            }
            catch (Exception ex)
            {
                lstReceive.Items.Add($"连接失败:{ ex.ToString()}");
            }
        }
        /// <summary>
        /// 接收消息
        /// </summary>
        private void ReceiveMsg()
        {
            while (true)
            {
                //声明接受最大字节数
                byte[] buffer = new byte[1024];
                int length = -1;
                try
                {
                    length = clientSocket.Receive(buffer);
                }
                catch (Exception ex)
                {
                    lstReceive.Invoke(new Action(() =>
                    {
                        lstReceive.Items.Add($"接收数据失败: {ex.Message}");
                    }));
                    break;
                }              
                if (length > 0)
                {
                    // 方法1
                    //byte[] temp = new byte[length];
                    //Array.Copy(buffer, 0, temp, 0, length);
                    //lstReceive.Invoke(new Action(() =>
                    //{
                    //    lstReceive.Items.Add(Encoding.UTF8.GetString(temp));
                    //}));
                    // 方法2
                    string msg = Encoding.Default.GetString(buffer,0,length);
                    lstReceive.Invoke(new Action(()=> {
                        lstReceive.Items.Add(msg);
                    }));
                }
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] temp = Encoding.Default.GetBytes(textBox3.Text);
            try
            {
                clientSocket.Send(temp);
            }
            catch (Exception ex)
            {
                lstReceive.Items.Add($" 消息发送失败:{ex.Message}");
            }
           
        }
server 指定客户端发送信息
csharp 复制代码
   Socket serverSocket = null;
   Socket ClientSocket = null;
   //List<Socket> listSocket=new List<Socket>();
   //存储所有监控的客户端
   Dictionary<EndPoint,Socket> clientSocketDic=new Dictionary<EndPoint, Socket> ();
   private void btnlisten_Click(object sender, EventArgs e)
   {
       serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
       IPAddress ip = IPAddress.Parse(txtIP.Text);
       IPEndPoint endpoint = new IPEndPoint(ip, int.Parse(txtPort.Text));
       serverSocket.Bind(endpoint);
       //同时监控几个客户端
       serverSocket.Listen(10);
       Task.Run((Action)ListentConnectSocket);//等价于 Action action=ListentConnectSocket;Task.Run(action);
       //方法2
       //Thread thd = new Thread(ListentConnectSocket);
       //thd.IsBackground = true;
       //thd.Start();
   }
  
   void ListentConnectSocket()
   {
       // 死循环
       while (true)
       {              
           try
           {
               ClientSocket = serverSocket.Accept(); //放在UI线程,会阻塞,所以放在另外一个线程
              
               string sendMsg = "恭喜您,服务器连接成功!";
               ClientSocket.Send(Encoding.Default.GetBytes(sendMsg));
               //客户端IP、端口号
               string ClientInfo = ClientSocket.RemoteEndPoint.ToString();
               //方法1
               //Action<string> action = delegate (string msg)
               //{
               //    lsReceive.Items.Add(msg);
               //};
               //lsReceive.Invoke(action, ClientInfo);
               //方法2 匿名方法
               //lsReceive.Invoke((Action<string>)delegate(string msg) {
               //    lsReceive.Items.Add(msg);
               //},ClientInfo);
               // 方法3 lamda
               lsReceive.Invoke(new Action<string>((msg) =>
               {
                   clientSocketDic.Add(ClientSocket.RemoteEndPoint,ClientSocket);
                   comboBox1.Items.Add(ClientSocket.RemoteEndPoint);
                   lsReceive.Items.Add($"{msg} 上线啦");
               }), ClientInfo);
           }
           catch (Exception ex)
           {

               lsReceive.Invoke(new Action<string>((msg) =>
               {
                   lsReceive.Items.Add(msg);
               }), $"发送信息失败;{ex.Message}");
               break;
           }
          

           //开启接收信息
           //方法1
           //Thread thr = new Thread(ReceiveClientMsg);
           //thr.IsBackground = true;
           //thr.Start(ClientSocket);
           //方法2
           Action<Socket> action = new Action<Socket>(ReceiveClientMsg);
           Task.Run(()=>action(ClientSocket));
         
       }
   }
   private void ReceiveClientMsg(object clientSocket)
   {
       Socket client = clientSocket as Socket;
       while (true)
       {
           byte[] recBuffer = new byte[1024];
           int length = -1;
           try
           {
               //Receive 是阻塞方法
               length = client.Receive(recBuffer);
           }
           catch (Exception ex)
           {
               //客户端关闭应用程序
               string str = client.RemoteEndPoint.ToString();
               this.Invoke(new Action(() =>
               {
                   lsReceive.Items.Add($"接收客户端:{str} 异常; {ex.Message}");
               }));

               break;
           }
           if (length == 0)
           {
               string str = client.RemoteEndPoint.ToString();
               this.Invoke(new Action(() =>
               {
                   lsReceive.Items.Add($"{str}:下线了!");
               }));
               break;
           }
           else
           {
               string msg = Encoding.Default.GetString(recBuffer, 0, length);
               string msgStr = $"{DateTime.Now}【 接收客户端: {client.RemoteEndPoint.ToString()}】--- {msg}";
               this.Invoke(new Action(() =>
               {
                   lsReceive.Items.Add(msgStr + Environment.NewLine);
               }));
           }
       }
   }

   private void btnSend_Click(object sender, EventArgs e)
   {
      
           if (comboBox1.Text != string.Empty) 
           {
               string[] str=comboBox1.Text.Split(':');
               IPEndPoint endpoint=new IPEndPoint(IPAddress.Parse(str[0]), int.Parse(str[1]));
               Socket cltsocket = clientSocketDic[endpoint];
               Task.Run(new Action(() => {                                  
                   cltsocket.Send(Encoding.Default.GetBytes(txtSend.Text));
               }));
           }

             
       
   }



如何保证数据接受完整的方式
  1. 校验 双方协商 每次发信息,以什么符号结尾
  2. 包头+报文 双方协商 每次发信息,以什么符号、开始 、结尾
  3. 指令长度
数据获取的方式
  1. PC缓存区
  2. C# 操作获取数据
  3. 缓存区自移动

比较详细的概念:https://www.cnblogs.com/whuanle/p/10375526.html

相关推荐
一只小灿灿14 小时前
VB.NET在 Excel 二次开发中的全面应用
.net·excel
码农君莫笑14 小时前
信管通低代码信息管理系统应用平台
linux·数据库·windows·低代码·c#·.net·visual studio
liyongqiangcc21 小时前
了解 ASP.NET Core 中的中间件
.net
Kiros_Jiang1 天前
开源低代码平台-Microi吾码 打印引擎使用
javascript·开源·json·.net·pip
web147862107231 天前
C# .Net Web 路由相关配置
前端·c#·.net
数据的世界012 天前
.NET开发人员学习书籍推荐
学习·.net
paixiaoxin2 天前
CV-OCR经典论文解读|An Empirical Study of Scaling Law for OCR/OCR 缩放定律的实证研究
人工智能·深度学习·机器学习·生成对抗网络·计算机视觉·ocr·.net
1900432 天前
.NET重点
.net
m0_663234012 天前
在 .NET 5.0 运行 .NET 8.0 教程:使用 ASP.NET Core 创建 Web API
前端·asp.net·.net