Skip to main content
Version: 3.1.0

TcpClient

Tcp client implementation

Namespace

using Netly;

Constructors

  • TcpClient TcpClient(bool framing)
    • framing Enable or disable Netly.Core.MessageFraming.
      • true Netly will use its own message framing protocol.
      • false You will receive raw stream data without framing middleware (for receive raw data use TcpClient.OnData(Action<byte[]> callback))

Properties

  • bool IsOpened
    Return true when connection be connected, and false when connection isn't connected.

  • Host Host
    Host class is endpoint container, and contain (ip address, port, ip type), it's endpoint metadata.

  • bool Framing
    Return true when the instance is using MessageFraming protocol connection and false when the connection is not using MessageFraming protocol

  • bool IsEncrypted
    Return true when the instance is using TLS/SSL and false when isn't using this.

  • string UUID
    UUID or (Unique User Identifier) is a unique string value that link a user, only server-side client.

Methods

Triggers

  • void Open(Host host)
    Open connection, if connection not be open will call and expose exception on OnError callback.
    • Host Netly host instance (endpoint metadata)

  • void UseEncryption(bool enableEncryption, Func<object, X509Certificate, X509Chain, SslPolicyErrors, bool> onValidation = null)
    Enable TLS/SSL connection on client side. (Must used before TcpClient.Open(Host host))
    • enableEncryption Set true for enable TLS/SSL and set false don't enable SSL/TLS
    • onValidation Is a Func that will executed for validate server certificate. Default behaviour return true without validate any argument. See examples on TLS/SSL session.

  • void Close()
    Close connection, if you need close connection use this method.

  • void ToData(byte[] buffer)
    void ToData(string buffer)
    Send raw buffer to server, buffer is string or byte[] (bytes).

  • void ToEvent(string name, byte[] buffer)
    void ToEvent(string name, string buffer)
    Send event (netly-event) to server, name is event identifier, buffer is event buffer (data), buffer is string or byte[] (bytes), if send buffer as string, netly will use NE.Default as encoding protocol.

Callbacks

  • void OnOpen(Action callback)
    Event responsible for receiving the connection opening information successfully
    • callback is the "function" responsible for handling the received event.

  • void OnError(Action<Exception> callback)
    Event responsible for receiving an error when opening a connection, the Exception contains the error information.
    • callback is the "function" responsible for handling the received event.

  • void OnClose(Action callback)
    Event responsible for receiving information about closing or terminating the connection with the server.
    • callback is the "function" responsible for handling the received event.

  • void OnData(Action<byte[]> callback)
    Event responsible for receiving raw data (buffer) from the server
    • callback is the "function" responsible for handling the received event.

  • void OnEvent(Action<string, byte[]> callback)
    Event responsible for receiving events (netly-events) from the server
    • callback is the "function" responsible for handling the received event.

  • void OnModify(Action<Socket> socket)
    This event 'is responsible for executing modifications in Socket, this event is executed in the connection creation step, and you will have access to Socket that will be used internally
    • callback is the "function" responsible for handling the received event.

Example (dotnet >= 6.0)

using System;
using Netly;
using Netly.Core;

int pingCounter = 0;
Host host = new Host("127.0.0.1", 8080);
TcpClient client = new TcpClient(framing: true);

// Enable TLS/SSL
// onValidation is optional, default behaviour return true without check any data.
client.UseConnection(enableConnection: true, onValidation: (sender, cert, chain, policyErrors) =>
{
#if DEBUG

/*
Default behaviour, Ignore certificate validation.
*/

return true;

#else
/*
Custom validation callback;
*/

if (sslPolicyErrors == SslPolicyErrors.None) return true;

// ignore chain errors as where self signed
if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors) return true;

// Invalid SSL connection
return false;
#endif
});

client.OnOpen(() =>
{
// connection opened
client.ToEvent("welcome", "hello server!");
Console.WriteLine("Client connected!");
});

client.OnError((e) =>
{
// called when connection not opened or server isn't using framing protocol (if framing is enabled)
Console.WriteLine($"Connection error: {e}");
});

client.OnClose(() =>
{
Console.WriteLine("Client disconnected!");
});

client.OnEvent((name, data) =>
{
name = name.ToLower().Trim(); // convert string to lower case

if(name == "ping")
{
pingCounter++;
client.ToEvent("pong", $"ping counter: {pingCounter}");
}
else if(name == "q" || name == "quit" || name == "done")
{
client.Close();
}

Console.WriteLine($"Client event ({name}) -> {NE.GetString(data)}");
});

// open connection
client.Open(host);

// block main thread
bool running = false;
white(running)
{
Console.WriteLine("Netly ``TcpClient``\n\tq: Quit\n\tc: Close connection\n\tr: Reconnect");
string input = Console.ReadLine() ?? "";

switch(input)
{
case "q":
client.Close();
running = false;
break;
case "c":
client.Close();
break;
case "r":
client.Open(host);
break;
}
}

Console.WriteLine("Goodbye!!!");