Sign in to follow this  
Telamon

Why would a Sockets.Connect take 10+ seconds?

Recommended Posts

Telamon    157
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?

Share this post


Link to post
Share on other sites
cbenoi1    484
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

Share this post


Link to post
Share on other sites
JonStelly    127
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this