Speeding up the TcpClient method

I am working on a set of programs that communicate over a stand alone network.

I have 1 client and 10 servers. The client trys to connect to the servers when the program starts.

The client uses

return new TcpClient(StudentIP, port);

to try to connect to the servers. StudentIP is a String that is "192.168.0.101" , port is an integer equal to 60000. The TcpClient hangs(blocks ) over 1 minute when trying to connect to a server that is not running. It does connect to a server that is running.

When I try

IPEndPoint ipLocalEndPoint = new IPEndPoint(IPAddress.Parse(StudentIP),port);

temp = new TcpClient(ipLocalEndPoint);

temp.Connect(ipLocalEndPoint);

return temp;

TcpClient does not hang when trying to connect to a server that is not running. It will not connect to a server that is running.

Is there any way to speed up TcpClient

Thanks,

Frank G. Haymes

frank.haymes@hill.af.mil



Answer this question

Speeding up the TcpClient method

  • Danny Tsai

    If this is just a personal project for yourself then thats not a problem, but if you intend to distribute the application, changing a registry entry like TCPInitialRTT is not very wise, it may have other un-desired effects to the user's PC and at the very least you should make it a very clear option and explain what it does before making any changes, as well as storing the user's previous value and offering a way to restore it.

    don't mean to nag but I recommend an different method that won't involve a system-wide change to a user's PC.

    Chris


  • jazg

    The network is a stand alone network. It has one client and 10 servers.

    The computers are running custom programs. They will not need be running any other network apps.

    The change was only made to the client.

    Frank G. Haymes


  • bfox

    Frank Haymes wrote:
    When I try

    IPEndPoint ipLocalEndPoint = new IPEndPoint(IPAddress.Parse(StudentIP),port);

    temp = new TcpClient(ipLocalEndPoint);

    temp.Connect(ipLocalEndPoint);

    return temp;

    TcpClient does not hang when trying to connect to a server that is not running. It will not connect to a server that is running.

    I think you'll find that the new TcpClient statement is throwing a SocketsException (you could wrap it in a try/catch block to be sure). The TcpClient (IPEndPoint) variant of the constructor is used to associate the TcpClient with a local end point. For example because you have two (or more) IP connections on your system and you want to explicitly specify which local end point the client should use.

    Change that line to temp = new TcpClient(); and I suspect you'll find that the code behaves exactly the same as your first experiment (i.e., it hangs on missing servers and connects to working servers).

    The long delay when connecting to a server that isn't running is because the TcpClient will wait for the connect attempt to fail and will retry the connect attempt several times before it decides to give up and assume the server isn't running. You can control the number of retries via the registry but this is a bad thing to do - it affects all connections over the interface you modify and can lead to you failing to connect to systems that are running. http://technet2.microsoft.com/WindowsServer/en/Library/895c4bfa-1feb-4c16-a0c0-69e342b542261033.mspx

    As has already been suggested, using BeginConnect to asynchronously connect to all ten servers is your best bet. It will still take a minute or so for each connect to fail, but now all ten servers have their one minute running at the same time so the net elapsed time is more or less one minute total not one minute per server.


  • nickc

    Thank Frank.

    I ended up added a registery entry for TCPInitialRTT. I have it set to 40h and it is now taking about 1 sec to timeout.

    I had the TcpClient (IPEndPoint) in a Try Catch block and was getting an execption.

    I am using

    TcpClient(StudentIP, port).

    With the registery change it is working better now.

    Frank G. Haymes


  • Packiyanath

    Have you tried using TcpClient.BeginConnect That way your application can continue while the client attempts to connect in the background.

    Chris


  • Speeding up the TcpClient method