日期:2014-05-17 浏览次数:21057 次
public static ManualResetEvent allDone = new ManualResetEvent(false);
Socket listener = new Socket(AddressFamily.InterNetwork,
44 SocketType.Stream, ProtocolType.Tcp);
45
46 // Bind the socket to the local endpoint and listen for incoming connections.
47 try
48 {
49 listener.Bind(localEndPoint);
50 listener.Listen(100);
51 while (true)
52 {
53 // Set the event to nonsignaled state.
54 allDone.Reset();
55
56 // Start an asynchronous socket to listen for connections.
57 Console.WriteLine("Waiting for a connection");
58 listener.BeginAccept(
59 new AsyncCallback(AcceptCallback),
60 listener);
61
62 // Wait until a connection is made before continuing.
63 allDone.WaitOne();
64 }
65 }
66 catch (Exception e)
67 {
68 Console.WriteLine(e.ToString());
69 }