你是不是對非同步Socket 很迷惑? 看完本文的一小類 你就知道大體該做什麼,怎麼做了….

來源:互聯網
上載者:User

直接上代碼,看完你就懂了。

public class Socketer     {         /// <summary> /// Server /// </summary> /// <param name="serverport">ServerPort</param>         public Socketer(int serverport)         {             Initilize_Bind(serverport);             Socket_host.Listen(1);             Socket_host.BeginAccept(ar =>             {                     //這裡要注意我這裡上面只監聽了一個串連,而下面這句也只是簡答地吧原來的Socket給覆蓋了,如果你要改成多用戶端的話需要增加一個列表,                      // 並且對列表中的Socket對象分別進行相應的操作                 Socket_host = Socket_host.EndAccept(ar);                 Recive();             }, null);         }            //IPAddress _HostIP;  //public IPAddress HostIP { get { return _HostIP; } }          /// <summary> /// Client /// </summary> /// <param name="clientport">ClientPort</param> /// <param name="serverport">ServerPort</param>         public Socketer(int clientport, int serverport)         {             Initilize_Bind(clientport);             Socket_host.BeginConnect(new IPEndPoint(IPAddress.Loopback, serverport), ar =>                 {                     Socket_host.EndConnect(ar);                     Recive();                   }, null);         }          private void Initilize_Bind(int hostport)         {             Socket_host = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);             //_HostIP = ;             Socket_host.Bind(new IPEndPoint(IPAddress.Any, hostport));         }            public Socket Socket_host;         //public List<Socket> Clients = new List<Socket>();           private byte[] Buffer_Recive = new byte[256];          private virtual void Recive()         {             if (Socket_host.Connected)             {                 Array.Clear(Buffer_Recive, 0, Buffer_Recive.Length);                 Socket_host.BeginReceive(Buffer_Recive, 0, Buffer_Recive.Length, SocketFlags.None, ar =>                     {                        int getsize= Socket_host.EndReceive(ar);                        if (GetMsg != null)                        {                            //GetMsg.Invoke(Encoding.UTF8.GetString(Buffer_Recive, 0, getsize)); //GetMsg.Method.Invoke(null, new  object[]{ Encoding.UTF8.GetString(Buffer_Recive, 0, getsize)});                            GetMsg(Encoding.UTF8.GetString(Buffer_Recive, 0, getsize));                            //GetMsg.BeginInvoke(,null,null);                        }                        Recive();                     }, null);             }         }          public event GetResult GetMsg;           public virtual void Send(string msg)         {             if (Socket_host.Connected)             {                 byte[] sendbuffer = Encoding.UTF8.GetBytes(msg);                 Socket_host.BeginSend(sendbuffer, 0, sendbuffer.Length, SocketFlags.None, ar =>                     {                         Socket_host.EndSend(ar);                     }, null);             }         }      }       public delegate void GetResult(string msg);//小菜

另說一下,不知道為什麼,寫文章時,代碼塊 刪除不掉,煩死人了

伺服器端和用戶端使用同一段代碼建立,是不是很簡單,很easy?

Socketer Server = new Socketer(9999);
Socketer Client = new Socketer(9998, 9999);
Server.GetMsg += new GetResult(Server_GetMsg);
Client.GetMsg += new GetResult(Client_GetMsg);

然後你就send吧...都不需要Recive了...

哎,編輯的時候還不能局部選中文字,這編輯器太爛了....估計就只有速度還可以這個優勢了...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.