user140053
user140053

Reputation:

Design of asynchronous socket classes in C#

I've done an small asynchronous tcp server/client in C#...

... And I've been just thinking :

  1. C# API implements select and epoll, a classic but easy way to do async. Why does Microsoft introduce the BeginConnect/BeginSend family, which -in my opinion- have a more complicated design (and adds lines of code too).

  2. So, using the BeginXXX() "trend", I noticed that the System.Threading import is required (for the events). Does it mean that threads are involved too ?

Upvotes: 3

Views: 293

Answers (1)

usr
usr

Reputation: 171178

select and poll have two problems:

  1. They are generally used in a single-threaded way. They do not scale for this reason.
  2. They require all IO to be dispatched through a central place that does the polling.

It is much nicer to be able to just specify callback that magically will be called on completion. This scales automatically and there is no central place to dispatch needed. Async IO in .NET is quite free of hassles. It just works (efficiently).

Async IO on Windows is threadless. While an IO is running not a single thread is busy serving it. All async IO in .NET uses truly async IO supported by the OS. This means either overlapped IO or completion ports.

Look into async/await which also can be used with sockets. They provide the easiest way to use async IO that I know of. That includes all languages and platforms. select and poll aren't even in the same league judged by ease of use.

Upvotes: 4

Related Questions