Using Sockets Asynchronously

You should use the connection-oriented TCP protocol if you’re building synchronous clients and servers and you want to ensure that data is transmitted successfully between them. TCP comes at a price, though—sometimes you’ll simply want to use the datagram approach of UDP because of its inherently asynchronous nature, despite the lack of guarantees. Let’s look at some of the features of sockets under .NET that you can use to build reliable, asynchronous applications and get the best of both worlds.

The Poll and Select Methods

The TCP examples shown earlier created a thread for each client that connected to the server. Threads consume resources, and lots of threads consume lots of resources. Do you know how many concurrent ...

Get Microsoft® Visual J#™ .NET (Core Reference) 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.