AsyncTcpClient & AsyncTcpListener
An asynchronous variant of TcpClient and TcpListener for .NET Standard. Allows implementing TCP clients or servers with just a few callback methods.
Building asynchronous solutions with TcpClient and TcpListener is complicated and it is easy to introduce bugs or miss critical features. These classes provide an easy solution for this task. Writing asynchronous TCP/IP clients and servers with these classes only requires the implementation of very basic callbacks. Alternatively, you can implement your connection logic in derived classes. In any case, you just have to read the received data from a buffer and send data back. All socket and stream interaction is hidden from you.
This also includes an asynchronous byte buffer that keeps all received bytes as they come in. Applications can dequeue as many bytes as they need without discarding what else was received. Dequeueing returns up to as many bytes as are available when called synchronously, or exactly the requested number of bytes when called asynchronously. The async method can be cancelled. This ensures that the code will never block irrecoverably.
A complete example of both implementation styles is provided in the application in this repository. Start reading at Program.cs.
There’s a public Git repository of AsyncTcpClient on GitHub. Changes to the code are documented there.
Copying and distribution of this file, with or without modification, are permitted provided the copyright notice and this notice are preserved. This file is offered as-is, without any warranty. (GNU All-Permissive licence)
- Created on 2018-08-03, updated on 2018-08-04.
- Ca. 600 lines of code, estimated development costs: 600 - 2 400 €