Client Connect - lishu/Jock.Net.TcpJson GitHub Wiki

Client Connect

TcpJsonClient has two way connection to server

Connection in Construct method

If you use TcpJsonClient(IPEndPoint) construct method create client, The connection will start inside construct method, This means if server can not connect that time, it will throw a SocketException Immediately. You need use try block handle this.

Connection in Core Thread

If you use TcpJsonClient() (No parameters) construct method create client, you need start this by call Start(IPEndPoint, TcpJsonConnectionDeniedEventHandler), The connection will start inside core thread, if server denied connection, your can handle it in callback event handler, just like below:

var retryCount = 0;
var client = new TcpJsonClient();
client.Start(
    new System.Net.IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 8013),
    e=> {
        if(retryCount < 3)
        {
            retryCount++;
            // Report something

            // wait time and retry
            System.Threading.Thread.Sleep(3000);
            e.Retry = true;
        }
        else
        {
            // Report something

            // no code need here, no set Retry = true, just Stop
        }
    });