Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[C socket]state CLOSE_WAIT in netstat (solved)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
Il turisto
l33t
l33t


Joined: 12 May 2004
Posts: 968
Location: Battincourt - Belgique

PostPosted: Wed Dec 13, 2006 9:46 am    Post subject: [C socket]state CLOSE_WAIT in netstat (solved) Reply with quote

Hi all,

I have made a C daemon who listening on socket and fork when a client connect.

But after few connections I have this in netstat :

Code:

tcp        1      0 localhost.localdo:44444 localhost.localdo:51964 CLOSE_WAIT
tcp        1      0 localhost.localdo:44444 localhost.localdo:51966 CLOSE_WAIT
tcp        1      0 localhost.localdo:44444 localhost.localdo:51960 CLOSE_WAIT
tcp        1      0 localhost.localdo:44444 localhost.localdo:51962 CLOSE_WAIT
tcp        1      0 localhost.localdo:44444 localhost.localdo:51956 CLOSE_WAIT
tcp        1      0 localhost.localdo:44444 localhost.localdo:51958 CLOSE_WAIT
tcp        1      0 localhost.localdo:44444 localhost.localdo:51952 CLOSE_WAIT
tcp        1      0 localhost.localdo:44444 localhost.localdo:51954 CLOSE_WAIT


When I kill the daemon all of these disappear.

Does this can affect pertformance or connections number?
How can I correct these in my program?


Last edited by Il turisto on Wed Dec 13, 2006 11:38 am; edited 1 time in total
Back to top
View user's profile Send private message
didymos
Advocate
Advocate


Joined: 10 Oct 2005
Posts: 4798
Location: California

PostPosted: Wed Dec 13, 2006 10:04 am    Post subject: Reply with quote

Basically, that's normal. The connections are dead, but the standards specify they remain in the wait state in case any stray packets should wander in after a routing delay, etc. There's a time out set by the kernel, which you can alter with sysctl or directly writing to proc. When the timeout is up, then the connection is finally dead, and it'll disappear from the netstat listing. Or, like you found out, you can kill the program and clear them that way. As to performance, I don't think they really impact it much, unless you're opening and closing a huge mass of connections. I don't really know how much memory each one uses, or how often they're checked while in the wait state, though. Oh, yeah, I think the relevant proc entry is:
Code:

/proc/sys/net/ipv4/tcp_fin_timeout

or for sysctl

net.ipv4.tcp_fin_timeout


The default is 60 secs, at least on my machine.

[edit] No, wait, sorry. That controls a different, but related connection state. I don't know what the timeout for CLOSE_WAIT is. I was thinking of FIN_WAIT.
_________________
Thomas S. Howard
Back to top
View user's profile Send private message
Il turisto
l33t
l33t


Joined: 12 May 2004
Posts: 968
Location: Battincourt - Belgique

PostPosted: Wed Dec 13, 2006 10:11 am    Post subject: Reply with quote

Thanks for your response.

Yes I already change this value in /proc.
And other one like tcp_keepalive_* like writtent in man tcp.

So for you I can forget these if I don't do too much connection?
Back to top
View user's profile Send private message
_droop_
l33t
l33t


Joined: 30 May 2004
Posts: 957

PostPosted: Wed Dec 13, 2006 10:28 am    Post subject: Re: [C socket]state CLOSE_WAIT in netstat Reply with quote

Il turisto wrote:
Hi all,
Does this can affect pertformance or connections number?
How can I correct these in my program?


Hi,

The message shows that your program does not close socket correctly.

Performance : each unclosed socket will waste some small amount of memory (in kernel and in program), it can be a problem if your program is designed to run allways or accept large amount of client.

Correction : If you fork after the creation of the socket, you should close the socket in the child process and in the father one, example :
Code:

$socket = $listener->accept ();
if (fork ()) {
#father : nothing to do with the socket
close ($socket);
} else {
#child : do some treatment
...
close ($socket);
}
Back to top
View user's profile Send private message
Il turisto
l33t
l33t


Joined: 12 May 2004
Posts: 968
Location: Battincourt - Belgique

PostPosted: Wed Dec 13, 2006 11:38 am    Post subject: Re: [C socket]state CLOSE_WAIT in netstat Reply with quote

_droop_ wrote:

Hi,

The message shows that your program does not close socket correctly.

Performance : each unclosed socket will waste some small amount of memory (in kernel and in program), it can be a problem if your program is designed to run allways or accept large amount of client.

Correction : If you fork after the creation of the socket, you should close the socket in the child process and in the father one, example :
Code:

$socket = $listener->accept ();
if (fork ()) {
#father : nothing to do with the socket
close ($socket);
} else {
#child : do some treatment
...
close ($socket);
}


Is seems to work.

Thanks all for big help.
Back to top
View user's profile Send private message
x22
Apprentice
Apprentice


Joined: 24 Apr 2006
Posts: 208

PostPosted: Wed Dec 13, 2006 12:23 pm    Post subject: Reply with quote

didymos wrote:
Basically, that's normal. The connections are dead, but the standards specify they remain in the wait state in case any stray packets should wander in after a routing delay, etc.

That state is called TIME_WAIT. CLOSE_WAIT means half-open connection, closed from remote side but not from local side. In this state data can still be sent but not received.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum