The mission: Implement a program that will listen to multiple client requests asynchronously (on TCP) and transfer them to a server (also on TCP) to which only one connection is allowed. The program will also receive answers from the server and transfer each answer to the relevant client.

The question: What is the best way to implement this using c#?

From reading on the Internet I understood that I can use an asynchronous socket, and handle everything based on events. No need to use a thread pool. Am I correct?

This is the best example I found: https://gist.github.com/leandrosilva/656054

But from it I only figured out how to receive client requests and deliver them to the server. I still don't understand how to receive an answer from the server and deliver it to a relevant client using events of asynchronous socket?

Will appreciate your insights and help.

PS: I'm on a closed network, and have no VS on the internet computer, that is why I can't paste my code here. But currently my code looks a lot like in the example above (https://gist.github.com/leandrosilva/656054)

  • you may want to install the nuget package AltarNet3 which does all the event handling for you. Then you can scrap most of your code.. – Mattias Åslund May 15 '16 at 9:56

ignoring the language-specific bits, in general the way to do this is to remember which client made the request to the server. Now you can wrap the client request in another structure containing a token (client address works) so that you can work out where to send the response when the server returns it to you, or you can build a map or clients to server requests and work out in some other way which client made the request when the server responds (this may be easier depending on the type of data you receive, for example, you might be stamping a counter on every message to prevent duplicates).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.