how do i get TcpListener to accept multiple connections and work with each one individually?

You can factor out most of your code into a separate thread: static void Main(string[] args) { TcpListener listener = new TcpListener(IPAddress.Any , 8000); TcpClient client; listener.Start(); while (true) // Add your exit flag here { client = listener.AcceptTcpClient(); ThreadPool.QueueUserWorkItem(ThreadProc, client); } } private static void ThreadProc(object obj) { var client = (TcpClient)obj; // Do … Read more

Proper way to stop TcpListener

These are two quick fixes you can use, given the code and what I presume is your design: 1. Thread.Abort() If you have started this TcpListener thread from another, you can simply call Abort() on the thread, which will cause a ThreadAbortException within the blocking call and walk up the stack. 2. TcpListener.Pending() The second … Read more

What is the async/await equivalent of a ThreadPool server?

I’d let the Framework manage the threading and wouldn’t create any extra threads, unless profiling tests suggest I might need to. Especially, if the calls inside HandleConnectionAsync are mostly IO-bound. Anyway, if you like to release the calling thread (the dispatcher) at the beginning of HandleConnectionAsync, there’s a very easy solution. You can jump on … Read more