All Projects → ygoe → AsyncTcpClient

ygoe / AsyncTcpClient

Licence: other
An asynchronous variant of TcpClient and TcpListener for .NET Standard.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to AsyncTcpClient

tcp server client
A thin and simple C++ TCP client server
Stars: ✭ 124 (-0.8%)
Mutual labels:  tcp, tcp-server, tcp-client
Simplenet
An easy-to-use, event-driven, asynchronous network application framework compiled with Java 11.
Stars: ✭ 164 (+31.2%)
Mutual labels:  tcp, tcp-server, tcp-client
Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (-46.4%)
Mutual labels:  tcp, tcp-server, tcp-client
Packetsender
Network utility for sending / receiving TCP, UDP, SSL
Stars: ✭ 1,349 (+979.2%)
Mutual labels:  tcp, tcp-server, tcp-client
Oksocket
An blocking socket client for Android applications.
Stars: ✭ 2,359 (+1787.2%)
Mutual labels:  tcp, tcp-server, tcp-client
Easytcp
Simple framework for TCP clients and servers. Focused on performance and usability.
Stars: ✭ 60 (-52%)
Mutual labels:  tcp, tcp-server, tcp-client
React Native Tcp Socket
React Native TCP socket API for Android, iOS & macOS with client SSL/TLS support
Stars: ✭ 112 (-10.4%)
Mutual labels:  tcp, tcp-server, tcp-client
tcp-net
Build tcp applications in a stable and elegant way
Stars: ✭ 42 (-66.4%)
Mutual labels:  tcp, tcp-server, tcp-client
QTcpSocket
A simple Qt client-server TCP architecture to transfer data between peers
Stars: ✭ 62 (-50.4%)
Mutual labels:  tcp, tcp-server, tcp-client
Tacopie
C++ TCP Library - NO LONGER MAINTAINED
Stars: ✭ 359 (+187.2%)
Mutual labels:  tcp, tcp-server, tcp-client
Simpletcp
Simple wrapper for TCP client and server in C# with SSL support
Stars: ✭ 99 (-20.8%)
Mutual labels:  tcp, tcp-server, tcp-client
Simpleunitytcp
🖧 Simple Unity Project to show how TCP communication are builded in C# without multi-threading or Unity network (Unet) involved.
Stars: ✭ 22 (-82.4%)
Mutual labels:  tcp, tcp-server, tcp-client
ctsTraffic
ctsTraffic is a highly scalable client/server networking tool giving detailed performance and reliability analytics
Stars: ✭ 125 (+0%)
Mutual labels:  tcp, tcp-server, tcp-client
Simpletcp
A minimal non-blocking TCP server written for Python 3.
Stars: ✭ 162 (+29.6%)
Mutual labels:  tcp, tcp-server, tcp-client
network
exomia/network is a wrapper library around System.Socket for easy and fast TCP/UDP client & server communication.
Stars: ✭ 18 (-85.6%)
Mutual labels:  tcp, tcp-server, tcp-client
EasyFileTransfer
An easy way to transfer file with any size on network with tcp protocol.
Stars: ✭ 30 (-76%)
Mutual labels:  tcp, tcp-server, tcp-client
SuperSimpleTcp
Simple wrapper for TCP client and server in C# with SSL support
Stars: ✭ 263 (+110.4%)
Mutual labels:  tcp, tcp-server, tcp-client
Hprose Nodejs
Hprose is a cross-language RPC. This project is Hprose 2.0 for Node.js
Stars: ✭ 297 (+137.6%)
Mutual labels:  tcp, tcp-server, tcp-client
Bizsocket
异步socket,对一些业务场景做了支持
Stars: ✭ 469 (+275.2%)
Mutual labels:  tcp, tcp-server, tcp-client
Tinytcpserver
A small tcp server working under Mono or .NET (4.0) and provides hooks for handling data exchange with clients (works under mono and .net). Behaviour/protocol/reaction could be specified via custom C# script.
Stars: ✭ 14 (-88.8%)
Mutual labels:  tcp, tcp-server, tcp-client

AsyncTcpClient & AsyncTcpListener

An asynchronous variant of TcpClient and TcpListener for .NET Standard.

NuGet

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.

Features

  • Awaitable (async) client connection
  • Awaitable (async) listener
  • Automatic reconnecting of the client on connection loss
  • Client: React on new connection
  • Client: React on closed connection
  • Client: React on received data
  • Client: Received data queue buffer, fetch as much data as you need when it’s available
  • Just three small class files to add to your project (AsyncTcpClient, ByteBuffer, optional: AsyncTcpListener)

What’s so hard about TcpClient?

“The TcpClient class provides simple methods for connecting, sending, and receiving stream data over a network in synchronous blocking mode.” Reading from the network stream is a blocking operation. Once you decided that you need received data, your code will block until data was actually received. Your application will even deadlock if no data will ever be sent for some reason. While you can use the DataAvailable property to test that, you still need to poll it regularly, introducing additional load and delays.

While you’re doing something else and not wait for received data, you will also not notice when the connection was closed remotely. Detecting this requires calling the Read method.

Add to this exception handling with special error codes for normal situations and things are getting complicated.

AsyncTcpClient and AsyncTcpListener take all that complexity out of the process, with the bonus of automatic client reconnecting and server-side client connection management. You just handle a new connection and/or received data, the rest is taken care of by these classes.

Client side

Usage

Derived class with overridden methods:

var client = new DemoTcpClient
{
    IPAddress = IPAddress.IPv6Loopback,
    Port = 12345,
    //AutoReconnect = true
};
await client.RunAsync();

AsyncTcpClient with callbacks:

var client = new AsyncTcpClient
{
    IPAddress = IPAddress.IPv6Loopback,
    Port = 12345,
    //AutoReconnect = true,
    ConnectedCallback = async (client, isReconnected) =>
    {
        // Custom connection logic
    },
    ReceivedCallback = async (client, count) =>
    {
        // Custom connection logic
    }
};
await client.RunAsync();

Close the client connection:

// This can be called from within a callback
client.Disconnect();

// This should only be called outside of RunAsync
client.Dispose();

OnConnected

Virtual method to override in derived classes:

protected virtual Task OnConnectedAsync(bool isReconnected)

Callback method:

public Func<AsyncTcpClient, bool, Task> ConnectedCallback { get; set; }

Called when the client has connected to the remote host. This method can implement the communication logic to execute when the connection was established. The connection will not be closed before this method completes.

OnClosed

Virtual method to override in derived classes:

protected virtual void OnClosed(bool remote)

Callback method:

public Action<AsyncTcpClient, bool> ClosedCallback { get; set; }

Called when the connection was closed.

Parameter remote: true, if the connection was closed by the remote host; false, if the connection was closed locally.

OnReceived

Virtual method to override in derived classes:

protected virtual Task OnReceivedAsync(int count)

Callback method:

public Func<AsyncTcpClient, int, Task> ReceivedCallback { get; set; }

Called when data was received from the remote host. This method can implement the communication logic to execute every time data was received. New data will not be received before this method completes.

Parameter count: The number of bytes that were received. The actual data is available through the ByteBuffer.

Server side

Usage

Generic class with type argument for derived client class:

var server = new AsyncTcpListener<DemoTcpServerClient>
{
    IPAddress = IPAddress.IPv6Any,
    Port = 12345
};
await server.RunAsync();

Non-generic AsyncTcpListener with callbacks:

var server = new AsyncTcpListener
{
    IPAddress = IPAddress.IPv6Any,
    Port = 12345,
    ClientConnectedCallback = tcpClient =>
        new AsyncTcpClient
        {
            ServerTcpClient = tcpClient,
            RemoteEndPoint = tcpClient.Client.RemoteEndPoint,
            ConnectedCallback = async (serverClient, isReconnected) =>
            {
                // Custom server logic
            },
            ReceivedCallback = async (serverClient, count) =>
            {
                // Custom server logic
            }
        }.RunAsync()
};
await server.RunAsync();

Stop the listener:

server.Stop(true);

Stop the listener but keep client connections open:

server.Stop(false);

OnClientConnected

Virtual method to override in derived classes:

protected virtual Task OnClientConnected(TcpClient tcpClient)

Callback method:

public Func<TcpClient, Task> ClientConnectedCallback { get; set; }

Called when a pending connection request was accepted. When this method completes, the client connection will be closed.

Parameter tcpClient: The TcpClient that represents the accepted connection.

Generic type parameter:

public class AsyncTcpListener<TClient>
    : AsyncTcpListener
    where TClient : AsyncTcpClient, new()

Instantiates a new AsyncTcpClient instance of the type TClient that runs the accepted connection. This implementation does not call the OnClientConnected callback method.

License

Permissive license

Copyright (c) 2018, Yves Goergen, https://unclassified.software

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].