11.11. Create a Multithreaded TCP Server That Supports Asynchronous Communications

Problem

You need to handle multiple network requests concurrently or perform a network data transfer as a background task while your program continues with other processing.

Solution

Use the AcceptTcpClient method of the System.Net.Sockets.TcpListener class to accept connections. Every time a new client connects, start a new thread to handle the connection. Alternatively, use the TcpListener.BeginAcceptTcpClient to accept a new client connection on a thread-pool thread using the asynchronous execution pattern (discussed in recipe 4-2).

To start a background task to handle the asynchronous sending of data, you can use the BeginWrite method of the System.Net.Sockets.NetworkStream ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.