Create a Multithreaded TCP Network Server

Problem

You want to create a TCP server that can simultaneously handle multiple TCP clients.

Solution

Every time a new client connects, start a new thread to handle the request.

Discussion

A single TCP endpoint (IP address and port) can serve multiple connections. In fact, the operating system takes care of most of the work for you. All you need to do is launch a worker object on the server that will handle the connection on a new thread.

For example, consider the basic TCP client and server classes shown in recipe Communicate Using TCP. You can convert the server into a multithreaded server that supports multiple simultaneous connections quite easily. First create a class that will interact with an individual ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.