C # Socket Performance with .Net 4.5 Async vs [...] Async vs Begin [...]

Currently, from what I explored, there are 3 ways to work with a socket asynchronously:

.Net 4.5 Async Example: Using .Net 4.5 Async Function for Socket Programming (second publication)

[...] Async: http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx

To begin [...]: http://msdn.microsoft.com/en-us/library/5w7b7x5f(v=vs.110).aspx

I am very confused by all parameters .Net provides work with asynchronous sockets. Why should I use one or the other? What is the best choice for working with thousands of simultaneous connections?

+6
source share
4 answers

What is the best choice for working with thousands of simultaneous connections?
...
Curiosity about the beginning [...]. If I have an MMORPG server, where is one connection interacting with each other to update the position, animation, effects (the main MMORPG mechanism), in numbers that are "heavily loaded servers"? 200 ~ 300 simultaneous connections?

On the server side, you can also make good use of any asynchronous socket API, starting with Begin / End-style APM , based on EAP or TAP based Task events. This is because you will block fewer threads, as opposed to using synchronous APIs. Thus, more threads will be available to simultaneously serve other incoming requests to your server, which will increase its scalability.

Most likely, you will not see any performance benefits from using the TAP socket APIs compared to analogs of APM or EAP. However, the TAP API template is much easier to develop than APM or EAP. When used with async/await it creates shorter, more readable, and less error prone code. You get a natural stream of pseudo-linear code that is not possible with APM callbacks or EAP event handlers. If you cannot find a suitable Task socket API, you can always do it yourself from the Begin/End APM API using Task.FromAsync (or from the EAP API, check the " Task.FromAsync Template for Converting an Event to a Task" ).

When it comes to the client user interface application , scalability is not so important, but there is another advantage of the TAP template. With a little effort, this helps make your user interface responsive because you wonโ€™t block the user interface thread (which usually happens while waiting for the result of a synchronous call). This does not apply to the Socket Task API, it applies to any Task based API, such as Task.Delay() or Stream.ReadAsync() .

For some good reading material on asynchronous C # programming, check out the async/await wiki tag:

fooobar.com/questions/tagged / ...

+5
source

Methods using SocketAsyncEventArgs most closely match Windows core technology (I / O completion ports). They are essentially a pure metal cover designed for zero distribution and extract the highest performance through a less friendly API. This has the disadvantage of more closely related code as it does not implement any standard Stream API. Other asynchronous socket methods wrap this all up.

Methods using the Begin / End pair use what is called an asynchronous programmable model (APM). APM is the original asynchronous .NET model. It is very easy to write spaghetti code if you use it half-cocked, but it is functional and fairly easy to use when you have some experience with it. However, they should not be used a lot in modern .NET, because we have something much simpler and better:

Methods that return Task use the Task Based Asynchronous Template (TAP). Tasks are a clean upgrade to APM: they are more flexible, easier to compose, and typically have equal or better performance. Combined with the integrated async / await language, you can write code that works great and is much easier to understand and maintain.

tl; dr use Task methods if you do not have the requirement for extreme performance. Then use the SocketAsyncEventArgs methods. Do not use APM methods.

+9
source

If you have a chance to use .NET 4.5 and async / await, I fully recommend it.

Basically, these are the ways to do multithreading in .NET:

  • Thread
  • ThreadPool.QueueWorkItem.
  • XXXAsync method and XXXCompleted event.
  • BeginXXX and EndXXX methods.
  • Parallel task library.
  • asynchronous / await

The first is raw threads, which you should avoid, because creating threads is an expensive operation. The rest are just different ways of using ThreadPool, which is the tool responsible for collecting a collection of threads that can be used to schedule your tasks, which gives better performance than the first option.

Using different syntaxes, but for me the most clear is async / await. I recently created a WebSocket using sockets and asyn / await and the performance is pretty good . Technically, async / await does not give you a performance boost, but clarity in the code will allow you to optimize the approach of your application, and this can give a good performance boost over messy code based on continuations.

+1
source

First, you can check out this article on MSDN about the differences between the various asynchronous programming mechanisms in .NET.

Begin[โ€ฆ] was the first implementation of the async socket using APM (asynchronous programming model). A callback is required as one of the arguments. Although this is somewhat dated compared to newer methods, it works great if you don't mind having callbacks and the messy code that they can create. In addition, some additional overhead is associated with this due to the state object, and on heavily loaded servers this can be a problem.

[โ€ฆ]Async uses a new event-based model, as well as a lighter implementation that will help deal with problems with high Begin[โ€ฆ] traffic Begin[โ€ฆ] . This method works well, but can also lead to confusing code if you are not careful. Oh yes, there is an error that you can read here here , although you probably wonโ€™t like it if you donโ€™t create a very successful piece of software.

Asynchronous task-based programming (TPL) is the latest mechanism, and with the help of the async / await keywords it can have most (if not all) the effectiveness associated with [โ€ฆ]Async , making it much easier to understand the code. In addition, using the Task is much easier to wait until several operations are completed at a time. It is important to note that, although there are several proprietary .NET functions that implement TPL and return Task , there are still no operations for Socket. There are examples of how to do this on the Internet, but this requires a bit of extra work.

+1
source

Source: https://habr.com/ru/post/970719/


All Articles