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 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             //设置主线程继续
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 
posted @ 2006-08-10 16:18  吴东雷  阅读(1140)  评论(2编辑  收藏  举报