Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.linux.development.apps > #359
| From | David Dyer-Bennet <dd-b@dd-b.net> |
|---|---|
| Newsgroups | comp.os.linux.development.apps |
| Subject | Re: EOF, close(), shutdown(), on POSIX sockets |
| References | <ylfk62gpn3hg.fsf@dd-b.net> <1br4zdh37t.fsf@pfeifferfamily.net> |
| Date | 2012-01-06 10:00 -0600 |
| Message-ID | <ylfkboqglt1h.fsf@dd-b.net> (permalink) |
| Organization | ipHouse - Welcome Home! |
Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
> David Dyer-Bennet <dd-b@dd-b.net> writes:
>
>> 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.
>
> I've never been able to get a clear understanding of exactly the
> conditions under which a receiver will get an EOF so it can know the
> other end has shut down. For my own applications (and protocols) I've
> taken to sending a distinguished character (a period '.' in many cases,
> appropriately enough) as the last output from a transmitter, so I can
> treat it as if it were an EOF and shut down the receiver appropriately.
I know the feeling. And in fact bits of the actual protocol I'm
proxying do have some things like that -- but unfortunately not all of
them. And I'm half-thinking of the basic copying-both-ways engine as
being general-purpose rather than specific to this one protocol.
--
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 — Previous in thread | 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