Reputation: 1
Just using .net sockets as an example, I use:
TcpListener listener = new TcpListener(ip, port);
while(true)
{
TcpClient client = socket.AcceptTcpClient();
DoSomethingWithClient(client);
}
But the other way seems to be something like (based on http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.beginaccepttcpclient.aspx):
public static ManualResetEvent tcpClientConnected = new ManualResetEvent(false);
public static void DoBeginAcceptTcpClient(TcpListener listener)
{
tcpClientConnected.Reset();
listener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), listener);
tcpClientConnected.WaitOne();
}
public static void DoAcceptTcpClientCallback(IAsyncResult ar)
{
TcpListener listener = (TcpListener) ar.AsyncState;
TcpClient client = listener.EndAcceptTcpClient(ar);
DoSomethingWithClient(client);
tcpClientConnected.Set();
}
IMHO the async style requires 3 times as much code, and looks like goto spaghetti - its difficult to read, and forces you to separate out related code. So why would you use the async way? Presumably it must have some advantage?
Upvotes: 0
Views: 672
Reputation: 457422
As others have mentioned, asynchronous code enables you to use fewer threads, and thus scale better (for servers) and remain responsive (for UI clients).
Regarding TCP/IP sockets in particular, there's another reason to use asynchronous methods: you can write and read at the same time. This is necessary in the general case to prevent deadlocks; all large-scale general-purpose servers use asynchronous sockets exclusively.
The new async
/await
in VS2012 enables a much more natural way of writing asynchronous code. However, due to the read and write streams being independent, actual asynchronous socket programming is still rather complex. I have an intro to async
on my blog with some good followup resources at the end.
I believe you're just using sockets as an example of something that can be synchronous or asynchronous, but if you're actually interested in using sockets at this level you may also find my TCP/IP .NET Sockets FAQ helpful.
Upvotes: 1
Reputation: 12330
For large numbers of simultaneous clients, using asynchronous vs synchronous methods allows an app to use less threads while doing the same job.
E.g. I just ran an app that creates/manages 2000 outgoing connections using about 30 threads at first (less as threads are returned to the thread pool).
However it is usually slightly trickier to program this way, and can be a bit of a slippery slope as you'll have to reduce all sources of blocked threads to maximise the advantages.
The new async
/await
syntax reduces the problem of code spaghetti and lets you write regular "unbroken" methods that incorporate asynchronous calls.
If you can't use C# 5 and the async
/await
syntax there are other ways like J. Richter's AsyncEnumerator.
Upvotes: 0
Reputation: 354864
It has the advantage of not blocking the caller's thread, which is especially important when you're creating interactive applications that should remain responsive even when performing work.
Since C# 5 there is async
/await
to make this kind of code much easier to write and read.
Upvotes: 5