Skip to content

Asynchronous network library for .NET

License

Notifications You must be signed in to change notification settings

lulzzz/AsyncNet

Repository files navigation

AsyncNet

Asynchronous network library for .NET

Purpose

The primary purpose of this library is to provide easy to use interface for TCP and UDP networking in C#

Getting started

This repository contains multiple projects that fall into different category. See below.

AsyncNet.Tcp

Installation

NuGet

Features:

  • Easy to use TCP server
  • Easy to use TCP client
  • SSL support
  • Custom protocol deframing / defragmentation support

Basic Usage

TCP Server

var server = new AsyncNetTcpServer(7788);
server.ServerStarted += (s, e) => Console.WriteLine($"Server started on port: " +
    $"{e.TcpServerStartedData.ServerPort}");
server.ConnectionEstablished += (s, e) =>
{
    var peer = e.ConnectionEstablishedData.RemoteTcpPeer;
    Console.WriteLine($"New connection from [{peer.IPEndPoint}]");

    var hello = "Hello from server!";
    var bytes = System.Text.Encoding.UTF8.GetBytes(hello);
    peer.Post(bytes);
};
server.FrameArrived += (s, e) => Console.WriteLine($"Server received: " +
    $"{System.Text.Encoding.UTF8.GetString(e.TcpFrameArrivedData.FrameData)}");
await server.StartAsync(CancellationToken.None);

TCP Client

var client = new AsyncNetTcpClient("127.0.0.1", 7788);
client.ConnectionEstablished += (s, e) =>
{
    var peer = e.ConnectionEstablishedData.RemoteTcpPeer;
    Console.WriteLine($"New connection from [{peer.IPEndPoint}]");

    var hello = "Hello from client!";
    var bytes = System.Text.Encoding.UTF8.GetBytes(hello);
    peer.Post(bytes);
};
client.FrameArrived += (s, e) => Console.WriteLine($"Client received: " +
    $"{System.Text.Encoding.UTF8.GetString(e.TcpFrameArrivedData.FrameData)}");
await client.StartAsync(CancellationToken.None);

AsyncNet.Udp

Installation

NuGet

Features:

  • Easy to use UDP server
  • Easy to use UDP client

Basic Usage

UDP Server

var server = new AsyncNetUdpServer(7788);
server.ServerStarted += (s, e) => Console.WriteLine($"Server started on port: {e.UdpServerStartedData.ServerPort}");
server.UdpPacketArrived += (s, e) =>
{
    Console.WriteLine($"Server received: " +
        $"{System.Text.Encoding.UTF8.GetString(e.UdpPacketArrivedData.PacketData)} " +
        "from " +
        $"[{e.UdpPacketArrivedData.RemoteEndPoint}]");

    var response = "Response!";
    var bytes = System.Text.Encoding.UTF8.GetBytes(response);
    server.Post(bytes, e.UdpPacketArrivedData.RemoteEndPoint);
};
await server.StartAsync(CancellationToken.None);

UDP Client

var client = new AsyncNetUdpClient("127.0.0.1", 7788);
client.ClientReady += (s, e) =>
{
    var hello = "Hello!";
    var bytes = System.Text.Encoding.UTF8.GetBytes(hello);

    e.UdpClientReadyData.Client.Post(bytes);
};
client.UdpPacketArrived += (s, e) =>
{
    Console.WriteLine($"Client received: " +
        $"{System.Text.Encoding.UTF8.GetString(e.UdpPacketArrivedData.PacketData)} " +
        "from " +
        $"[{e.UdpPacketArrivedData.RemoteEndPoint}]");
};
await client.StartAsync(CancellationToken.None);

About

Asynchronous network library for .NET

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages