C # sockets handling multiple clients

I have the following code that I want to implement as my server. As far as I understand, this is asynchronous. and should allow connections from multiple clients ...

public void Start() { TcpListener listener = new TcpListener(IPAddress.Any, 10250); listener.Start(); Console.WriteLine("Listening..."); while (true) { IAsyncResult res = listener.BeginAcceptTcpClient(HandleAsyncConnection, listener); connectionWaitHandle.WaitOne(); } } private void HandleAsyncConnection(IAsyncResult res) { TcpListener listener = (TcpListener)res.AsyncState; TcpClient client = listener.EndAcceptTcpClient(res); connectionWaitHandle.Set(); StringBuilder sb = new StringBuilder(); var data = new byte[client.ReceiveBufferSize]; using (NetworkStream ns = client.GetStream()) { // Test reply Byte[] replyData = System.Text.Encoding.ASCII.GetBytes(DateTime.Now.ToString()); ns.Write(replyData, 0, replyData.Length); ns.Flush(); ns.Close(); } client.Close(); } 

I have a test application that just runs requests to my server. As you can see in the code, the server simply responds with its date / time. The test application sends, say, 20 requests, which are just test strings. For each of these requests, it opens a socket, sends data to my server, and then closes the socket again.

This works great with one test application. However, if I open two test applications, the second cannot connect to the server. I thought because I am processing the async request. and because my test application opens, it closes the socket before each call with which I could process requests from several clients?

+7
source share
2 answers

Edit

When using> =. Net4.5 is better off using new network methods, which then allow the adoption of async and await . Thus, it would be better to follow the example presented in this post as a starting point.

Original post

The following code demonstrates how to accept multiple clients asynchronously without spinning off a new thread to the connection.

 private TcpListener listener; public void Start() { listener = new TcpListener(IPAddress.Any, 10250); listener.Start(); Console.WriteLine("Listening..."); StartAccept(); } private void StartAccept() { listener.BeginAcceptTcpClient(HandleAsyncConnection, listener); } private void HandleAsyncConnection(IAsyncResult res) { StartAccept(); //listen for new connections again TcpClient client = listener.EndAcceptTcpClient(res); //proceed } 

You can use this template for most asynchronous operations.

+18
source

As you did, there is no benefit in using AcceptTcpClient . Just loop and create a new thread for each received connection:

 public void Start() { TcpListener listener = new TcpListener(IPAddress.Any, 10250); listener.Start(); Console.WriteLine("Listening..."); while (canRun) { var client = listener.AcceptTcpClient(); new Thread(ClientThread).Start(client); } } private void ClientThread(IAsyncResult res) { TcpClient client = (TcpClient)res.AsyncState; StringBuilder sb = new StringBuilder(); var data = new byte[client.ReceiveBufferSize]; using (NetworkStream ns = client.GetStream()) { // Test reply Byte[] replyData = System.Text.Encoding.ASCII.GetBytes(DateTime.Now.ToString()); ns.Write(replyData, 0, replyData.Length); ns.Flush(); ns.Close(); } client.Close(); } 

The golden rule when using asynchronous methods is NOT to block the operation. Blocking will defeat the purpose of using asynchronous operations.

 public void Start() { TcpListener listener = new TcpListener(IPAddress.Any, 10250); listener.Start(); Console.WriteLine("Listening..."); listener.BeginAcceptTcpClient(OnAccept, listener); } private void OnAccept(IAsyncResult res) { TcpListener listener = (TcpListener)res.AsyncState; TcpClient client = listener.EndAcceptTcpClient(res); StringBuilder sb = new StringBuilder(); var data = new byte[client.ReceiveBufferSize]; using (NetworkStream ns = client.GetStream()) { // Test reply Byte[] replyData = System.Text.Encoding.ASCII.GetBytes(DateTime.Now.ToString()); ns.Write(replyData, 0, replyData.Length); ns.Flush(); ns.Close(); } client.Close(); } 
+1
source

All Articles