Skip to content

4.SslByteMessageClient‐Server

ReferenceType edited this page Dec 18, 2023 · 3 revisions

SSLByteMessageTcpClient/Server

It is build open SSLClient/Server, where only difference is messages are sent with 4 byte lenght header.

For detailed info, please refer SSLClient/Server

  • Byte fragmentation occurs when message size exceeds max MTU. Which is around 1500 bytes and can be lower than that.
  • Each receiver unit on server and client side has statefull message extractor where full messages are extracted from fragmented bytes by using lenght info.
  • Messages are guaranted to reach destionation atomically without fragmentation.

Example

//var scert = new X509Certificate2("server.pfx", "greenpass");
//var cert = new X509Certificate2("client.pfx", "greenpass");
var scert = CertificateGenerator.GenerateSelfSignedCertificate();
var cert = CertificateGenerator.GenerateSelfSignedCertificate();

var server = new SslByteMessageServer(20008, scert);
server.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };

server.OnBytesReceived += ServerBytesReceived;
// since certificate is self-signed it will give chain errors, here we bypass it
server.RemoteCertificateValidationCallback += (_, _, _, _) => { return true; };
server.StartServer();

var client = new SslByteMessageClient(cert);
client.OnBytesReceived += ClientBytesReceived;
client.RemoteCertificateValidationCallback += (a, b, c, d) => true;

client.Connect("127.0.0.1", 20008);
client.SendAsync(UTF8Encoding.UTF8.GetBytes("Hello I'm a client!"));

// Callback handlers
void ServerBytesReceived(Guid clientId, byte[] bytes, int offset, int count)
{
    Console.WriteLine(UTF8Encoding.UTF8.GetString(bytes, offset, count));
    server.SendBytesToClient(clientId, UTF8Encoding.UTF8.GetBytes("Hello I'm the server"));
}

void ClientBytesReceived(byte[] bytes, int offset, int count)
{
    Console.WriteLine(UTF8Encoding.UTF8.GetString(bytes, offset, count));
}
Clone this wiki locally