TCP Guide

The messages consist of text only (encoded in UTF-8) and end with a line break, so if you want to send bytes or a message with line breaks, you'll have to implement escape characters or something like base64 encoding on both ends.

Once connected, serverside and clientside connections work exactly the same, so everything regarding TcpConnection applies to both. Servers use TcpServer to listen for clients.

The necessary classes are contained in the namespace uwap.TCP, so you should probably add using uwap.TCP; to the top of your code.

In the following examples, port 12345 will be used. Keep in mind that usage of ports 1-1024 will require elevated rights on most systems.

Installation

For now, there is no binary you can just install on your server, however, that's in the works.

You can download the source code from GitHub and add a reference to it from your project.

Soon, there will also be a NuGet package so you can simply install and update that.

Creating a server

To create a new server object:
TcpServer server = new(12345);
You can use new(12345, false) if you can't support IPv4.

Assigning a method (below) to the ConnectionReceived event:
server.ConnectionReceived += Server_ConnectionReceived;

Starting the server:
server.Start();

Method for ConnectionReceived:
void Server_ConnectionReceived(TcpConnection connection) { ... }
In this method, you'll get TcpConnection objects to save and work with.

Connecting a client to a server

Connect to a server (uwap.org:12345 here) and get the connection object from it:
TcpConnection connection = TcpConnection.Connect("uwap.org", 12345);
If you are going to have a lot of connections to servers, you should use the .NET thread pool like this:TcpConnection.Connect("uwap.org", 12345, true)

That's it, now you have a TcpConnection object to work with.

Using a TcpConnection

First, you'll want to assign the two events (methods below):
connection.MessageReceived += Connection_MessageReceived;
connection.ConnectionLost += Connection_ConnectionLost;

Methods to handle the events:
void Connection_MessageReceived(TcpConnection connection, string message) { ... }
void Connection_ConnectionLost(TcpConnection connection) { ... }

Sending a message:
connection.Send("Awesome message");

Disconnecting and disposing the connection:
connection.Disconnect();
If the connection has been lost, the connection object will be disposed automatically.

That'll be all

If you have any more questions, problems or suggestions for this project, please contact me at flo@uwap.org!