TCPLib.Client 2.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package TCPLib.Client --version 2.1.1                
NuGet\Install-Package TCPLib.Client -Version 2.1.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TCPLib.Client" Version="2.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TCPLib.Client --version 2.1.1                
#r "nuget: TCPLib.Client, 2.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TCPLib.Client as a Cake Addin
#addin nuget:?package=TCPLib.Client&version=2.1.1

// Install TCPLib.Client as a Cake Tool
#tool nuget:?package=TCPLib.Client&version=2.1.1                

<h1 align="center">TCPLib</h1>

<p align="center"> <a href="https://github.com/Kacianoki/TCPLib/actions/workflows/Tests.yml"> <img src="https://github.com/Kacianoki/TCPLib/actions/workflows/Tests.yml/badge.svg?branch=master"> </a> <img src="https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FKacianoki%2FTCPLib&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false"></img> <a href="https://github.com/Kacianoki/TCPLib/pulse" alt="Activity"> <img src="https://img.shields.io/github/commit-activity/m/Kacianoki/TCPLib" /> </a> </p>

TCPLib.Server TCPLib.Client TCPLib.Shared
NuGet NuGet NuGet NuGet NuGet NuGet

What is this?!

TCPLib - It's a low-level library for exchanging packets with a remote computer (🤓). TCPLib works on the TCP protocol and supports AES and RSA encryption. For packet exchange, TCPLib uses Protocol Buffers.

How do I use this thing? :0

Since this is a low-level library, using it can be quite tricky : (

Implementing the Server Side

First, you need to implement two interfaces: TCPLib.Server.SaveFiles.IBanListSaver and TCPLib.Server.SaveFiles.ISettingsSaver.

Your implementation might look like this:

ISettingsSaver

using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using TCPLib.Server.SaveFiles;
using System.IO;

namespace ExampleServer
{
    public class SettingsSaver : ISettingsSaver
    {
        public void Save(TCPLib.Server.SaveFiles.Settings settings)
        {
            var serializer = new SerializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance)
                .Build();
            File.WriteAllText("Settings.yml", serializer.Serialize(settings));
        }
        public TCPLib.Server.SaveFiles.Settings Load()
        {
            if (!File.Exists("Settings.yml"))
                new TCPLib.Server.SaveFiles.Settings().Save();
            var deserializer = new DeserializerBuilder().Build();
            return deserializer.Deserialize<TCPLib.Server.SaveFiles.Settings>(File.ReadAllText("Settings.yml"));
        }
    }
}

IBanListSaver

using System;
using System.IO;
using Newtonsoft.Json;
using TCPLib.Server.SaveFiles;

namespace ExampleServer
{
    public class BanSaver : IBanListSaver
    {
        public void Save(Ban[] bans)
        {
            File.WriteAllText("banlist.json", JsonConvert.SerializeObject(bans));
        }
        public Ban[] Load()
        {
            if (!File.Exists("banlist.json")) Save(Array.Empty<Ban>());
            return JsonConvert.DeserializeObject<Ban[]>(File.ReadAllText("banlist.json"));
        }
    }
}

Once you've implemented these two interfaces, you can start the server! But you'll need to code a bit more : (

To create the server, you can write this:

using System;
using System.Threading.Tasks;
using TCPLib.Server;

namespace ExampleServer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            TCPLib.Server.Server server = new Server(new BanSaver(), new SettingsSaver());

            server.Stopped += OnStopped; // If you type the standard "stop" command in the console,
            // the server won't terminate the process, so we subscribe to this event.

            server.Start();
            server.ConsoleRead(); // We want to send commands to the server :0
        }

        static Task OnStopped()
        {
            Environment.Exit(0);

            return Task.CompletedTask;
        }
    }
}

Wow! We managed to start the SERVER 🎉

But it doesn't work with the client yet, so let's fix that!

using System;
using System.Text;
using System.Threading.Tasks;
using TCPLib.Server;
using TCPLib.Server.Net;

namespace ExampleServer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            TCPLib.Server.Server server = new Server(new BanSaver(), new SettingsSaver());

            server.Stopped += OnStopped;

            TCPLib.Server.Net.Client.SuccessfulConnection += OnConnected;

            server.Start();
            server.ConsoleRead();
        }

        private static async Task OnConnected(TCPLib.Classes.ResponseCode code, Client client)
        {
            while (true)
            {
                var message = await client.ReceiveSourceAsync(); // Here we get the raw byte array of the packet (this is basically a hack)
                TCPLib.Server.Console.Info(UTF8Encoding.UTF8.GetString(message.Data));
            }
        }

        static Task OnStopped()
        {
            Environment.Exit(0);

            return Task.CompletedTask;
        }
    }
}

It would be more appropriate to use ReceiveAsync() here, but for that, you would need to write a Protobuf schema, compile it, and implement the TCPLib.Net.IProtobufSerializable interface, which we don't need right now, so we made a hack instead 😎.

Implementing the Client Side

Now we need to write a client for the server! Let's start with the simplest part:

using TCPLib.Client;
using System.Net;
using System.Threading.Tasks;

namespace ExampleClient
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            TCPLib.Client.Client client = new Client();

            var server = await client.Connect(IPAddress.Parse("127.0.0.1"), 2024); // 127.0.0.1 - local IP
        }
    }
}

To start sending messages to the server, we need to create a message class:

using System.Text;
using TCPLib.Net;

namespace ExampleClient 
{
    internal class Message : IProtobufSerializable<Message>
    {
        public string Data;

        public Message FromBytes(byte[] bytes)
        {
            return new Message() { Data = Encoding.UTF8.GetString(bytes) };
        }

        public byte[] ToByteArray()
        {
            return Encoding.UTF8.GetBytes(Data);
        }

        public Message() { } // A parameterless constructor is required
    }
}

Once we have the message class, we can send objects of this class to the server:

using TCPLib.Client;
using System.Net;
using System.Threading.Tasks;
using System;

namespace ExampleClient
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            TCPLib.Client.Client client = new Client();

            var server = await client.Connect(IPAddress.Parse("127.0.0.1"), 2024);

            while (true)
            {
                string input = Console.ReadLine();

                await server.SendAsync(new Message() { Data = input });
            }
        }
    }
}

We wrote the server and the CLIENT! Hooray! 🥳 You can find code examples right here.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 85 10/20/2024
2.1.2 93 10/12/2024
2.1.1 96 9/8/2024
2.1.0 80 9/8/2024
2.0.0 105 9/8/2024
1.1.1 84 9/3/2024
1.1.0 102 9/3/2024 1.1.0 is deprecated because it has critical bugs.
1.0.0 115 8/29/2024