小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C#下用select方法實(shí)現(xiàn)socket服務(wù)端

 正則室 2018-01-02

select是一種比較古老但一直被證明性能很好的socket模式,它可以讓你以消息驅(qū)動(dòng)的模式書寫socket程序。網(wǎng)上C++的例子很多,但C#的例子極少。

上代碼:

[csharp] view plain copy
  1. namespace Server  
  2. {  
  3.     class Program  
  4.     {  
  5.         // Thread signal.  
  6.     public static ManualResetEvent allDone = new ManualResetEvent(false);  
  7.   
  8.     private static Socket handler = null;  
  9.     private static ArrayList g_CliSocketArr = new ArrayList();  
  10.     private static Object thisLock = new Object();  
  11.     public Program()  
  12.     {  
  13.     }  
  14.   
  15.     public static void StartListening() {  
  16.         // Data buffer for incoming data.  
  17.         byte[] bytes = new Byte[1024];  
  18.   
  19.           
  20.         IPAddress ipAddress = IPAddress.Parse("0.0.0.0");//0.0.0.0表示監(jiān)聽本機(jī)的所有IP  
  21.         IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);  
  22.   
  23.         // Create a TCP/IP socket.  
  24.         Socket listener = new Socket(AddressFamily.InterNetwork,  
  25.             SocketType.Stream, ProtocolType.Tcp );  
  26.           
  27.         // Bind the socket to the local endpoint and listen for incoming connections.  
  28.         try {  
  29.             listener.Bind(localEndPoint);  
  30.             listener.Listen(100);  
  31.   
  32.   
  33.             // Start an asynchronous socket to listen for connections.  
  34.             Console.WriteLine("Waiting for a connection...");  
  35.   
  36.             Thread worker = new Thread(new ThreadStart(WorkerThread));//創(chuàng)建一個(gè)線程用于處理請求  
  37.             worker.Start();  
  38.             while (true)  
  39.             {  
  40.                 Socket sClient = listener.Accept();  
  41.                 Console.WriteLine("There is a new connection.");  
  42.                 g_CliSocketArr.Add(sClient);  
  43.                   
  44.             }  
  45.   
  46.         } catch (Exception e) {  
  47.             Console.WriteLine(e.ToString());  
  48.         }  
  49.   
  50.         Console.WriteLine("\nPress ENTER to continue...");  
  51.         Console.Read();  
  52.           
  53.     }  
  54.         public static void WorkerThread()  
  55.         {  
  56.             Socket socket1 = null;  
  57.             ArrayList readList = new ArrayList();  
  58.       //      readList.Add(socket0);  
  59.             while (true)  
  60.             {  
  61.                 lock (thisLock)  
  62.                 {  
  63.                     readList.Clear();  
  64.                     for (int i = 0; i < g_CliSocketArr.Count; i++)  
  65.                     {  
  66.                         readList.Add(g_CliSocketArr[i]);  
  67.                     }  
  68.                 }  
  69.                 if (readList.Count <= 0)  
  70.                 {  
  71.                     Thread.Sleep(100);  
  72.                     continue;  
  73.                 }  
  74.                 try  
  75.                 {  
  76.                     Socket.Select(readList, nullnull, 500);  
  77.                     for (int i = 0; i < readList.Count; i++)  
  78.                     {  
  79.                         socket1 = (Socket)readList[i];  
  80.                         Console.WriteLine("There is a new message from client.");  
  81.                         byte[] buffer = new byte[1024];  
  82.                       
  83.                             int recLen = socket1.Receive(buffer);  
  84.                             if(recLen > 0)  
  85.                             {  
  86.                             //    recLen = socket1.Receive(buffer);  
  87.                             }  
  88.                             else  
  89.                             {//如果返回0,表示客戶端已經(jīng)斷開連接,須將此socket關(guān)閉然后從連接池中清除  
  90.                                 Console.WriteLine("Rece 0 length.");  
  91.                                 for (int ii = 0; ii < g_CliSocketArr.Count; ii++)  
  92.                                 {  
  93.                                     Socket s = (Socket)g_CliSocketArr[ii];  
  94.                                     if (s == socket1)  
  95.                                         g_CliSocketArr.RemoveAt(ii);  
  96.                                 }  
  97.                                 socket1.Shutdown(SocketShutdown.Both);  
  98.                                 socket1.Close();  
  99.                                 break;  
  100.                             }  
  101.                             socket1.Send(buffer,recLen, SocketFlags.None);  
  102.                      
  103.                     }  
  104.                 }  
  105.                 catch (SocketException e)  
  106.                 {  
  107.                     Console.WriteLine("{0} Error code: {1}.", e.Message, e.ErrorCode);  
  108.   
  109.                     for (int ii = 0; ii < g_CliSocketArr.Count; ii++)  
  110.                     {  
  111.                         Socket s = (Socket)g_CliSocketArr[ii];  
  112.                         if (s == socket1)  
  113.                             g_CliSocketArr.RemoveAt(ii);  
  114.                     }  
  115.                     socket1.Shutdown(SocketShutdown.Both);  
  116.                     socket1.Close();  
  117.                 }  
  118.             }  
  119.               
  120.         }  
  121.   
  122.         static void Main(string[] args)  
  123.         {  
  124.             StartListening();  
  125.         }  
  126.    
  127.     }  
  128.   
  129. }  

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請遵守用戶 評(píng)論公約

    類似文章 更多