zoukankan      html  css  js  c++  java
  • C# Socket通信改进记录

    1. Socket

    1. 使用原始Socket,Send和Recv方法 进行发送和消息获取。(另起后台线程 不停获取和发送)
      1.   
         public void RecvMsg()
                {
                    //receive message
                    bool isListen = true;
        
                    while (isListen)
                    {
                        string recStr = "";
                        byte[] recBytes = new byte[4096];
                        int bytes = clientSocket.Receive(recBytes, recBytes.Length, 0);
                        recStr += Encoding.UTF8.GetString(recBytes, 0, bytes);
        
                        if (!String.IsNullOrWhiteSpace(recStr))
                        {
                            if (recStr.EndsWith(Environment.NewLine))
                            {
                                recStr = recStr.Substring(0, recStr.Length - Environment.NewLine.Length);
                            }
                            AppendContet(recStr);
                        }
                    }
        
                }
    2. 使用Socket BeginSend 和BeginReceive 进行异步发送和消息获取。
      1.   
        private void button1_Click(object sender, EventArgs e)
                {
                    //send message
                    string sendStr = txtMsg.Text;
                    byte[] sendBytes = Encoding.UTF8.GetBytes(sendStr);
                    clientSocket.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, new AsyncCallback(Send_Complete), clientSocket);
                }
        
        
                private void Send_Complete(IAsyncResult e)
                {
                    if (e.IsCompleted)
                    {
                        AppendDebugMsg("异步发送已完成");
                    }
                    else
                    {
                        AppendDebugMsg("异步发送失败");
                    }
                    var socket = e.AsyncState as Socket;
                    if (socket == null)
                    {
                        AppendDebugMsg("发送异步Socket为空");
                    }
                }
    3. 使用 SocketAsyncEventArgs 进行高效率 异步发送和消息获取。
      1.   
         public void SendAync(byte[] sendBytes)
                {
                    SocketAsyncEventArgs sendSocketArgs = GlobalConfig.SendPool.Pop();
                    sendSocketArgs.UserToken = this;
                    sendSocketArgs.SetBuffer(sendBytes, 0, sendBytes.Length);
        
                    this.ClientSocket.SendAsync(sendSocketArgs);
                }

    2. Socket 与 WebSocket进行相互通信

    1. 前端Html5  WebSocket搭建
    2. Socket 接收和发送方法改进 支持 WebSocket。

    其他:

    1. java对于 Socket的支持 ,及 ReadLine 造成的IO阻塞。
    2. 异常捕获,及废弃Socket处理
  • 相关阅读:
    D. Beautiful Array
    C. Magic Ship Educational Codeforces Round 60 (Rated for Div. 2)
    CCPC-Wannafly Winter Camp Day3 小清新数论(莫比乌斯反演反演加杜教筛)
    杜教筛
    Algorithms Weekly 3
    Algorithms Weekly 2
    Algorithms Weekly 1
    KNN算法实现数字识别
    2019总结
    2019 Google Kickstart Round H
  • 原文地址:https://www.cnblogs.com/shikyoh/p/4588403.html
Copyright © 2011-2022 走看看