Socket异步编程-之服务器端.

 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 connectiondot.gif " );
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 Listenerdot.gif. " );
36          }
37 
38           public   void  acceptCallback(IAsyncResult ar) {
39              Socket listener = (Socket)ar.AsyncState;
40              Socket handler = listener.EndAccept(ar);
41               // 设置主线程继续
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 

转载于:https://www.cnblogs.com/fxwdl/archive/2006/08/10/473467.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值