Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if connection is open

I have a problem when i'm trying to avoid using a Thread.sleep(400) my code is like this:

System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
clientSocket = Connect(IP, Port);
Thread.Sleep(400);

NetworkStream networkStream = clientSocket.GetStream();
Send(networkStream, "My Data To send");
networkStream.Flush();

and my send() method:

public static void Send(NetworkStream networkStream, string Data)
{
    int range = 1000;
    int datalength = 0;
    foreach (string data in Enumerable.Range(0, Data.Length / range).Select(i => Data.Substring(i * range, range)))
    {
        byte[] outStream = System.Text.Encoding.ASCII.GetBytes(data);
        networkStream.Write(outStream, 0, outStream.Length);
        datalength = datalength + range;
        Thread.Sleep(50);
    }
    byte[] LastoutStream = System.Text.Encoding.ASCII.GetBytes(Data.Substring(datalength, Data.Length - datalength) + "$EOS$\r\n");
    networkStream.Write(LastoutStream, 0, LastoutStream.Length);
}

the Connect method:

 protected static System.Net.Sockets.TcpClient Connect(string Ip, int Onport)
    {
        //start connection
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        try
        {
            clientSocket.Connect(Ip, Onport);
        }
        catch
        {
            clientSocket.Connect("LocalHost", Onport);
        }
        return clientSocket;
    }

is there a way to check if the stream is ready to be used?

like image 597
Rene Nielsen Avatar asked Nov 01 '22 04:11

Rene Nielsen


1 Answers

Though your code is working I like to point out these regarding the stream

GetStream() returns exception only on 2 cases and these exceptions are (Source):

  1. InvalidOperationException - The TcpClient is not connected to a remote host.

  2. ObjectDisposedException - The TcpClient has been closed.

So your stream should be available if you meet this two conditions

Its always good to write exception logic in your code for the same reasons like the other party have bugs in their code.

In your own connect() method you should find out the exception and notify the called function that connect was unsuccessful or throw the exception back to caller function and have a try catch to handle it. So in successful case you will always get your stream.

try{
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
clientSocket = Connect(IP, Port);
//Thread.Sleep(400);

NetworkStream networkStream = clientSocket.GetStream();
Send(networkStream, "My Data To send");
networkStream.Flush();
}catch(Exception E)
{
 //Log
 //Its always best to catch the actual exception than general exception
 //Handle gracefully
}

Connect Method or you can leave out the exception to fall back to caller

 protected static System.Net.Sockets.TcpClient Connect(string Ip, int Onport)
    {
        //start connection
        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        try
        {
            clientSocket.Connect(Ip, Onport);
        }
        catch
        {
            //clientSocket.Connect("LocalHost", Onport);
            throw;
        }
        return clientSocket;
    }
like image 172
Dexters Avatar answered Nov 15 '22 04:11

Dexters