乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Socket異步編程-之服務(wù)器端. - 業(yè)精于勤荒于嬉,形成于思------->毀于隨 - 博客園

       xnet 2006-08-12
       1 using System;
       2 using System.Text;
       3 using System.Threading;
       4 using System.Net.Sockets;
       5 using System.Net;
       6 namespace asyncSocketServer {
       7 
       8     class SocketListener {
       9         System.Threading.ManualResetEvent allDone=new ManualResetEvent(false);
      10         [STAThread]
      11         static void Main(string[] args) {
      12             SocketListener server=new SocketListener();
      13             server.StartListening();
      14         }
      15 
      16         public void StartListening() {
      17             IPHostEntry ipHostInfo=Dns.Resolve(Dns.GetHostName());
      18             IPEndPoint localEP=new IPEndPoint(ipHostInfo.AddressList[0],11000);
      19             Console.WriteLine("Local address and pot:{0}",localEP.ToString());
      20             Socket listener=new Socket(AddressFamily.InterNetwork,
      21                 SocketType.Stream,ProtocolType.Tcp);
      22             try {
      23                 listener.Bind(localEP);
      24                 listener.Listen(10);
      25                 while (true) {
      26                     allDone.Reset();
      27                     Console.WriteLine("Waiting for a connection");
      28                     listener.BeginAccept(new AsyncCallback(acceptCallback),listener);
      29                     allDone.WaitOne();            //阻塞主線程
      30                 }
      31             }
      32             catch(Exception ex) {
      33                 Console.WriteLine(ex.ToString());
      34             }
      35             Console.WriteLine("Closing the Listener.");
      36         }
      37 
      38         public void acceptCallback(IAsyncResult ar) {
      39             Socket listener=(Socket)ar.AsyncState;
      40             Socket handler=listener.EndAccept(ar);
      41             //設(shè)置主線程繼續(xù)
      42             allDone.Set();
      43             StateObject state=new StateObject();
      44             state.workSocket=handler;
      45             handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
      46                 new AsyncCallback(readCallback),state);
      47         }
      48 
      49         public void readCallback(IAsyncResult ar) {
      50             StateObject state=(StateObject)ar.AsyncState;
      51             Socket handler=state.workSocket;
      52             int read=handler.EndReceive(ar);
      53             if (read>0) {
      54                 state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,read));
      55                 handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
      56                     new AsyncCallback(readCallback),state);
      57             }
      58             else {
      59                 if (state.sb.Length>1) {
      60                     string content=state.sb.ToString();
      61                     Console.WriteLine("Read {0} bytes from socket.\n Data:{1}",
      62                         content.Length,content);
      63                 }
      64                 handler.Close();
      65             }
      66         }
      67     }
      68
      69     public class StateObject {
      70         public Socket workSocket=null;
      71         public const int BufferSize=1024;
      72         public byte[] buffer=new byte[BufferSize];
      73         public StringBuilder sb=new StringBuilder();
      74     }
      75 }
      76 

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

        0條評(píng)論

        發(fā)表

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

        類似文章 更多