Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,

I have written a bespoke "latency checker". There is a client and server. Client is Console App and service is a Service. I am restricted to .Net 3.5.

So this all works locally fine, but on the clients main server it throws an error:
C#
while (!client.Connected && retry-- > 0)
Null ref exception
at TcpClient.Get_Connected (points to the line above)

I don't get it. I only just instantiated it. The most it might be is closed.

here is the code for this method.
C#
private int GetPort(long taskLength, bool isReport = false)
{
    IPEndPoint endPoint = new IPEndPoint(_address, _port);
    TcpClient client = new TcpClient();
    int port = 0;


    int retry = retries;

    while (!client.Connected && retry-- > 0)
    {
        try
        {
            client.Connect(endPoint);

            using (var stream = client.GetStream())
            using (StreamReader reader = new StreamReader(stream))
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.AutoFlush = true;
                if (isReport)
                {
                    writer.WriteLine($"report:{taskLength}");
                }
                else
                {
                    writer.WriteLine($"port request:{taskLength}");
                }
                string portString = reader.ReadLine();
                if (!string.IsNullOrEmpty(portString) && int.TryParse(portString, out port))
                    return port;
                throw new BadResponseException("Port format incorrect");
            }
        }
        catch (BadResponseException)
        {
            //retry
        }
        catch (SocketException)
        {
            //retry
        }
        finally
        {
            client.Close();
        }
    }
    return port;
}
For the first call: taskLength = 1 and isReport = false.


Any ideas on what could've caused this would help ^_^

Thanks

What I have tried:

I tested the client and server locally and the server is running both locally to it. This is the first test before I put the client on another machine.

The Server has 1 port held open for requests from clients. It opens a port async, sets it to receive a specific number of bytes and replies back saying what the port number is.

I only have 1 client running atm which is single threaded.
Posted
Updated 3-Nov-17 6:17am

1 solution

I guess that it only occurs when there are multiple retries. Then your TcpClient might be no longer valid due to the Close() call:
The Close method marks the instance as disposed and requests that the associated Socket close the TCP connection.
Note the "disposed".

There is no need to perform the Connected check at all because it would be always false in your code after closing. But if the Close() call makes the object invalid, you might then get the same error at the next Connect() call. The only solution would be then using a new instance (optionally limited to the case that the Connect() call does not throw an exception; when that throws an exception, there is no need to close).
 
Share this answer
 
Comments
Andy Lanng 3-Nov-17 13:44pm    
It's always something simple I just don't see >_<
Ta that worked ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900