Main Contents

Slow TcpClient Connection (sockets)

Schotime @ May 27, 2008

.NET

Recently I have been experimenting with Sockets and trying to communicate with a windows service both with a console app and a website.

After managing to get some lines of communication going between the client and server applications, I couldn’t help but notice that it was taking a little bit longer than it probably should have. I decided to have a closer look.

Using some basic timestamping in the console/website application the whole process was taking just over 1 second. This was to connect, send and then receive a response. Further investigation revealed only 1 statement was causing the time to blow out. Below is the statement.

TcpClient socketForServer = new TcpClient("localhost", 200);

Now in all the demos and tutorials this is how it showed to connect to the server. Pretty straightforward and simple. Or so I thought. Whilst having a look through the methods associated with the TcpClient, there was one that stood out: Connect(). Hmmm…this also took a host name and a port. I decided to give it a try.

TcpClient socketForServer = new TcpClient();
socketForServer.Connect("localhost", 200);

I bet you’re wondering right now. What is the difference?? At this point in time I’m not quite sure but I’m determined to figure it out because after making that change the time to execute the previous two statements was about 1 millisecond or so. Much better.

One thing that did seem odd was if the TcpClient failed to make a connection, the response time was almost exactly the same (~1sec).

If anyone knows the reason, please leave a comment. Until then, two lines will have to do.


book mark Slow TcpClient Connection (sockets) in del.icio.us submit Slow TcpClient Connection (sockets) to digg.com


4 Comments

  1. Mike Koss June 27, 2008 @ 8:22 am

    I found your post trying to debug a local Python web server I’m running. Requests were also taking about 1 second each, when they should be nearly instantaneous.

    One change I made, connect to “127.0.0.1″ insead of “localhost” -> now everything is fast again!

    Is Vista doing a DNS call (and timing out), before connecting to the local machine???

  2. Schotime June 27, 2008 @ 1:05 pm

    Mike,

    This is also related to the slow response from firefox when running on localhost with vista.

    The reason for it is because of the ipv6 protocol. In the hosts file of your pc under vista there is two entries for localhost. One for ipv6 and one for ipv4. I have now just commented out the ipv6 one with a # and everything is as fast as it should be now (<4ms).

    Its obviously taking 1 second to decide which protocol to use or gets confused. Something….whatever. haha. Least it works now.

    Hope this helps everyone.
    Thanks for the comments Mike.

  3. Pedro Duarte October 3, 2009 @ 1:03 am

    I had the same problem with Windows Vista and 7, and with this solution it works very fast.

  4. Jason Smith May 22, 2010 @ 12:50 am

    Using the TcpClient() constructor with no parameters works only with ipV4 which is why you are noticing this difference.

    See here: http://msdn.microsoft.com/en-us/library/aa329755(v=VS.71).aspx

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Feed
24,359 spam comments
blocked by
Akismet