Why would a Sockets.Connect take 10+ seconds?

Started by
1 comment, last by JonStelly 19 years, 9 months ago
Hello, I'm writing a simple test application to test the scalability of several server-side network techniques (mainly I want to see how many users my Stratego server is going to be able to support concurrently). I'm coding in C# and using the TcpListener and TcpClient classes. I'm doing my stress tests with two laptops connected together with a router (there's nothing else on the network and there is no internet access). I have a client test program that creates a new connection to the server every 500 milliseconds, and sends 100 bytes of data to the server on each currently open connection every 1 second. This runs on one laptop and the server runs on the other. Currently the server is a very simple one-listening-thread-per-socket implementation. Whenever a client sends it a string, it sends the string back in all upper case. This works great when both the client and the server are running on the same machine (I can get up to about 700 connections before quality really goes downhill). However, when I run the client on the other laptop, it takes the TcpClient.Connect() call 10+ seconds to complete. Now in an actual game, this wouldn't really be a problem, but for my test application it kind of is. Does anyone know why it takes so long for the Connect to complete? The ping from one machine to the other is less than a millisecond, and the two computers are connected with gigabit ethernet cables. The solution, it seems, is to do multiple connects simultaneously from the client tester program, but I'm still puzzled as to what is going on here. Both computers are on a router, but I don't think NAT is occurring since they are only talking to each other. My first thought was that the .Net TCP connection classes do some kind of automated NAT punch-through to start the connection, but I don't really know what I'm talking about and I don't think that makes any sense in this case. Comments?

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

Advertisement
My guess is that the C# implementation bundles a series of calls like gethostbyname()/bind()/connect() into one. gethostbyname() involves a DNS lookup even though your target machine is nearby.

Plan A: Try profiling 'Dns.GetHostByName()' under the same conditions.

Plan B: You can try to accelerate things by editing the '{WINDOWS}/system32/drivers/etc/hosts' file to add your other machine in there, but this assumes the target machine has a fixed IP address. It's a 'hack' but one that will tell you if this is DNS-related or not.

-cb
TcpClient.Connect() can accept a hostname and port number. You can try the method above, or try changing to the method that accepts an IPEndPoint. I'd guess it's a DNS related problem too, I've used TcpClient before and the Connect() method didn't take anywhere near 10 seconds.

This topic is closed to new replies.

Advertisement