Re: Non-blocking sockets



On Mar 25, 11:31 pm, David Schwartz <dav...@xxxxxxxxxxxxx> wrote:
On Mar 25, 3:26 pm, Gigi <dand...@xxxxxxxxx> wrote:

What I've been doing so far was call recv() and giving it a certain
number... an expected size for the whole data (header + payload). So
from what you're saying I suppose I have to call recv() repeatedly
with 1 as the size to receive? (i.e. receive one byte at a time until
\r\n\r\n is reached)
I don't know if I'm understanding you correctly, but if I am, isn't
this inefficient? I mean, you're calling a system call for every byte,
which I suppose causes considerable overhead because of continuous
context switching.

That would be horrible, don't do that. Use the following logic:

1) Call 'recv' passing it a reasonable amount of bytes to get.

2) See if you have a complete header, by searching for a '\r\n\r\n' in
the data you got. (Caution, do not use 'strstr' until/unless you zero-
terminate the data!)

3) If you don't have a complete header, you definitely don't have a
complete request.

4) If you do have a complete header, check if you you have all the
data too. If not, go to 1.

DS

OK, so let's say I receive 8 bytes, and the \r\n\r\n is at the
beginning of it. Let's assume that the payload is less than 4 bytes
long (for example, when issuing a HEAD request). Then I am expecting
to read more data than I will actually receive, which means recv()
will block.

Another problem is that the \r\n\r\n can be split over two 8-byte
fragments, but that's not really a big deal... a bit of extra code
will cater for this situation.

Of course, your method will work perfectly for a response with a
decent payload. But if it can work universally, even for HEAD
requests, then it would be so much better.
.



Relevant Pages

  • Re: Subclassing socket
    ... of data that has a header and payload, ... length of the payload, then at some point you have to receive N ... but you are presumably also aware that a zero-length result from recv() indicates that the other party to a TCP connection has closed their end of the socket? ...
    (comp.lang.python)
  • Re: Non-blocking sockets
    ... What I've been doing so far was call recv() and giving it a certain ... an expected size for the whole data (header + payload). ... context switching. ...
    (comp.unix.programmer)
  • Re: Non-blocking sockets
    ... an expected size for the whole data (header + payload). ... from what you're saying I suppose I have to call recvrepeatedly ... Call 'recv' passing it a reasonable amount of bytes to get. ...
    (comp.unix.programmer)
  • Re: On bit fields usage
    ... puts the header and payload in one contiguous block of memory because ... payload and i was wondering where i could have possibly gone wrong. ...
    (comp.lang.c)
  • Re: Messages being received out of order
    ... sure how much your data will be send in one packet, because if IP header is ... Are you saying that tcp assures you that if you send data1, ... then I only need to fix my client - specifically the recv handling? ...
    (microsoft.public.win32.programmer.networks)