While developing a client and server (both using blocking mode calls) I got into a few minor problems.
The client I'm creating should get one response for every time it does a request to the server. Once this "ping - pong" is done the client should disconnect and cleanup gracefully. This includes memory and the open socket.

A typical sequense goes like this.

1. Client sends request for server.
2. Server gives exactly one response.
...
3. Goto 1
...
4. Client is done and calls shutdown with parameter SD_BOTH and then closesocket.
5. Server will do a recv() call where it gets 0 as return value and assume the client is disconnecting.
6. Server calls shutdown with SD_BOTH param calls closesocket() and free's the memory used by the client.

So both the server and client runs the same sequense.
Now I shut down both programs and netstat shows this:

TCP cyber_server:4823 PII-400:6500 TIME_WAIT
TCP cyber_server:4824 PII-400:6500 TIME_WAIT
TCP cyber_server:4825 PII-400:6500 TIME_WAIT
TCP cyber_server:4826 PII-400:6500 TIME_WAIT
TCP cyber_server:4827 PII-400:6500 TIME_WAIT
TCP cyber_server:4828 PII-400:6500 TIME_WAIT
TCP cyber_server:4829 PII-400:6500 TIME_WAIT
TCP cyber_server:4830 PII-400:6500 TIME_WAIT
TCP cyber_server:4831 PII-400:6500 TIME_WAIT
TCP cyber_server:4832 PII-400:6500 TIME_WAIT
(This test was made by running the client in a endless loop)
The first column is the client and the server is second column which listens on port 6500.

So how can the socket stay open even though I closed it?

The other problem I'm having is a slow DNS lookup. On one of the machines I have it can take up to about 20-30 seconds to make a dns lookup even though it's on local network. The following code (written in C) is what I use to get the server ip:

// Try to use the host as a ip
sin.sin_addr.s_addr = inet_addr(szHost);
if (sin.sin_addr.s_addr == INADDR_NONE)
{
// Make a dns request
if ((lpHost = gethostbyname(szHost)) == NULL)
{
printf("DNS lookup of server failed\n");
return false;
}
sin.sin_addr.s_addr = *((unsigned long *) lpHost->h_addr);
}

It seems the process called System is taking all cpu time in the time the lookup happens. The odd thing is though that a few times in a row the lookup can be fast while most of the time it's slow. Is this a correct way to do DNS lookups?

All tests were made under Windows 2000.

Thanks in advance.

// CyberHeg
Posted on 2003-03-21 11:31:27 by CyberHeg
Originally posted by CyberHeg
So both the server and client runs the same sequense.
Now I shut down both programs and netstat shows this:

TCP cyber_server:4823 PII-400:6500 TIME_WAIT
TCP cyber_server:4824 PII-400:6500 TIME_WAIT
TCP cyber_server:4825 PII-400:6500 TIME_WAIT
TCP cyber_server:4826 PII-400:6500 TIME_WAIT
TCP cyber_server:4827 PII-400:6500 TIME_WAIT
TCP cyber_server:4828 PII-400:6500 TIME_WAIT
TCP cyber_server:4829 PII-400:6500 TIME_WAIT
TCP cyber_server:4830 PII-400:6500 TIME_WAIT
TCP cyber_server:4831 PII-400:6500 TIME_WAIT
TCP cyber_server:4832 PII-400:6500 TIME_WAIT
(This test was made by running the client in a endless loop)
The first column is the client and the server is second column which listens on port 6500.

So how can the socket stay open even though I closed it?

Nothing to worry about :):
4.11 - What do the FIN_WAIT_x, TIME_WAIT, CLOSE_WAIT and other states mean?

The other problem I'm having is a slow DNS lookup. On one of the machines I have it can take up to about 20-30 seconds to make a dns lookup even though it's on local network. The following code (written in C) is what I use to get the server ip:

// Try to use the host as a ip
sin.sin_addr.s_addr = inet_addr(szHost);
if (sin.sin_addr.s_addr == INADDR_NONE)
{
// Make a dns request
if ((lpHost = gethostbyname(szHost)) == NULL)
{
printf("DNS lookup of server failed\n");
return false;
}
sin.sin_addr.s_addr = *((unsigned long *) lpHost->h_addr);
}

It seems the process called System is taking all cpu time in the time the lookup happens. The odd thing is though that a few times in a row the lookup can be fast while most of the time it's slow. Is this a correct way to do DNS lookups?

It is the correct way, I don't know why your DNS lookup is so slow. Is it slow as well when you use nslookup in a cmd screen? The only time I've seen slow blocking host lookups is when the host doesn't exists. Maybe somehow your local network names are not resolved correctly and windows tries to find them with your ISPs DNS server :confused:. You could try a packet logger like ethereal to see what's happening behind the scenes.

Thomas
Posted on 2003-03-21 13:10:44 by Thomas



Thanks thats what I wanted to know.
It is the correct way, I don't know why your DNS lookup is so slow. Is it slow as well when you use nslookup in a cmd screen? The only time I've seen slow blocking host lookups is when the host doesn't exists. Maybe somehow your local network names are not resolved correctly and windows tries to find them with your ISPs DNS server :confused:. You could try a packet logger like ethereal to see what's happening behind the scenes.

Thomas


I just checked with ping command on the same machine and it acts the same way so I conclude it's some mess with bad dns setup.

Thanks again.

// CyberHeg
Posted on 2003-03-22 01:47:18 by CyberHeg