Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.linux.development.apps > #351
| From | David Dyer-Bennet <dd-b@dd-b.net> |
|---|---|
| Newsgroups | comp.os.linux.development.apps |
| Subject | EOF, close(), shutdown(), on POSIX sockets |
| Date | 2012-01-05 17:17 -0600 |
| Message-ID | <ylfk62gpn3hg.fsf@dd-b.net> (permalink) |
| Organization | ipHouse - Welcome Home! |
I'm getting myself confused in an application I'm having trouble getting
working. I've now got too many different sets of test code that behave
differently, and which don't always do what I expect.
I'm developing this on a Centos 5 system. The actual code is in Perl,
but I don't think that's important (based on strace monitoring of the
actual system calls being made, and what happens). Portability isn't a
requirement (but is my default way of thinking in general).
I'm working on the core of a tcp proxy application. The overall
architecture is that the proxy server listens on a socket, accepts
connections, reads the first line and decides (based on information
transmitted in the local protocol that the request is encoded in) what
server to send that request to, forks a child, the child connects to the
chosen server, and drops the client and server socket into the core
relay bit (while the parent goes back to accept the next connection).
The proxy is supposed to be transparent (except, obviously, that the
real server can tell where the connection it gets came from if it asks).
Much of it seems to work :-).
One thing I think I'm having trouble with (tests using netcat don't do
quite what I expect) involves what to do in the proxy copy loop when I
get EOF (meaning a 0 return from a read, no error). The proxy is
copying bi-directionally between two sockets, call them socket A and
socket B.
If I receive EOF when I read from A, I think I should do shutdown(B,
SHUT_WR), and if I receive EOF when I read from B, I should similarly do
shutdown (A, SHUT_WR). (And there is accounting about which sockets
I've received EOF reading from, to avoid trying to read them in future,
and to keep track of when both sides have shut down so I should close
everything and exit.) This appears to be necessary to propogate the EOF
through the proxy from client to server or vice-versa. Right?
One confusing factor: in the strace from nc (netcat), I get:
17:58:48.764342 poll([{fd=3, events=POLLIN}, {fd=0,
events=POLLIN}], 2, -1) = 2 ([{fd=3, revents=POLLIN}, {fd=0,
revents=POLLIN}])
17:58:48.764474 read(3, "", 1024) = 0
nc receives EOF from the proxy (port 3 is the socket connected to the
proxy).
17:58:48.764575 shutdown(3, 0 /* receive */) = 0
nc promptly shuts down the socket it just received EOF on. Should I be
doing this in addition to the other?
Another confusing factor: immediately after this, nc reads 1024 bytes
from stdin (leaving it in the middle of the test file, about 56%; and
that's only 1/8 of the amount of data requested), writes it to 3, then
closes and exits -- meaning it aborts reading and transmitting the input
file.
The nc that the strace was from is invoked as:
nc localhost $PORT1 < $TD1 >$TDIR/o2 &
where $PORT1 is 8000 and is where the proxy is listening.
--
David Dyer-Bennet, dd-b@dd-b.net; http://dd-b.net/
Snapshots: http://dd-b.net/dd-b/SnapshotAlbum/data/
Photos: http://dd-b.net/photography/gallery/
Dragaera: http://dragaera.info
Back to comp.os.linux.development.apps | Previous | Next — Next in thread | Find similar
EOF, close(), shutdown(), on POSIX sockets David Dyer-Bennet <dd-b@dd-b.net> - 2012-01-05 17:17 -0600
Re: EOF, close(), shutdown(), on POSIX sockets Kaz Kylheku <kaz@kylheku.com> - 2012-01-06 01:10 +0000
Re: EOF, close(), shutdown(), on POSIX sockets David Dyer-Bennet <dd-b@dd-b.net> - 2012-01-06 09:59 -0600
Re: EOF, close(), shutdown(), on POSIX sockets Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-01-05 21:20 -0700
Re: EOF, close(), shutdown(), on POSIX sockets David Dyer-Bennet <dd-b@dd-b.net> - 2012-01-06 10:00 -0600
Re: EOF, close(), shutdown(), on POSIX sockets Andrei Voropaev <avorop@mail.ru> - 2012-01-12 16:07 +0000
Re: EOF, close(), shutdown(), on POSIX sockets Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-01-12 12:57 -0700
Re: EOF, close(), shutdown(), on POSIX sockets Kaz Kylheku <kaz@kylheku.com> - 2012-01-12 20:04 +0000
csiph-web