Re: Non-blocking sockets
- From: Gigi <dandago@xxxxxxxxx>
- Date: Tue, 25 Mar 2008 15:59:05 -0700 (PDT)
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.
.
- Follow-Ups:
- Re: Non-blocking sockets
- From: David Schwartz
- Re: Non-blocking sockets
- From: Barry Margolin
- Re: Non-blocking sockets
- References:
- Non-blocking sockets
- From: Gigi
- Re: Non-blocking sockets
- From: Rainer Weikusat
- Re: Non-blocking sockets
- From: Gigi
- Re: Non-blocking sockets
- From: Scott Lurndal
- Re: Non-blocking sockets
- From: Gigi
- Re: Non-blocking sockets
- From: David Schwartz
- Non-blocking sockets
- Prev by Date: Re: Non-blocking sockets
- Next by Date: Re: Turning cursor off
- Previous by thread: Re: Non-blocking sockets
- Next by thread: Re: Non-blocking sockets
- Index(es):
Relevant Pages
|