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()) {
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?
Remotec
source share