lkml.org 
[lkml]   [1998]   [Feb]   [16]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectDoes close() close a TCP socket?
Date
According to "Unix Network Programming" by RW Stevens, closing a
socket should perform a shutdown if the reference count is zero. What
I am seeing is that closing a socket without performing a shutdown
will not close it completely. Here is the order of events in my test
fixture:

1) Server listens on well known port. 2112 by default.
2) Client connects to server.
3) Server accepts connection and writes data to socket.
4) Server closes socket and returns to accept.
5) Client reads data until EOF and then closes socket.

This all appears to work correctly except for one small catch. If I
terminate the server and attempt to restart it immediately the Linux
kernel refuses to allow the server to bind to the port.

Let me introduce my test software and show the output.

ftp://ftp.netcom.com/pub/el/elf/tcptest-0.0.1.tar.gz

This archive will unpack and compile with kernel 2.0.3x and gcc
2.7.2.3. Run make.

Execute the test program on two machines, or from two VTs on the same
machine. Start the server once with:

tcptest --test=noshutdown

Start the client several times with:

tcptest --test=noshutdown --server=<hostname_or_ip_of_server>

Interrupt the server with ^C and try to restart it. When I do this I
get the message

elf@mojo ~/src/tcptest-0.0.1 > tcptest --test=noshutdown
00.000 tcptest: executing as server
00.000 tcptest: port 2112
00.000 tcptest: test 'noshutdown'
00.000 tcptest: unable to bind to port 2112 (Address already in use)

Looking at /proc/net/tcp on the server machine I see

sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
0: 0100007F:0840 0100007F:203E 06 00000000:00000001 01:00001432 00000000 0 2 0
1: 0100007F:0840 0100007F:203D 06 00000000:00000001 01:00001401 00000000 0 2 0
2: 0100007F:0840 0100007F:203C 06 00000000:00000001 01:000013D4 00000000 0 2 0

These entries are sockets that were connected to the client. Notice
that the rx_queue has '1' byte of queued data. I discovered that this
is the EOF marker from the client by watching /proc/net/tcp as the
client performed its reads and then closed its socket.

I can solve this problem in two ways. The server can shutdown the
write portion socket and then read on the socket until EOF, or the
server can shutdown the whole socket. In either case, the sockets
are absent from /proc/net/tcp after the server quits.

So. Is this the expected behavior from the kernel? Is there a reason
why the kernel is not terminating the sockets after the server closes
them and then quits?

-- Marc Singer (elf@netcom.com)





-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu

\
 
 \ /
  Last update: 2005-03-22 13:41    [W:1.242 / U:0.228 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site