Re: Receive no data
- From: "Roger Hunen" <rhunen@xxxxxxxxx>
- Date: Thu, 6 Dec 2007 12:38:13 +0100
"Volodymyr Shcherbyna" <v_scherbina@xxxxxxxxxxxxxxx> wrote...
You should erase this line from your code:
err = shutdown(sockfd, SD_SEND);
There is absolutely nothing wrong with this. It is a best practice to shutdown
a socket for sending once all data has been sent. The code follows this best
practice perfectly.
Nit pick: in order to also shutdown the receive direction cleanly, a
"shutdown(sockfd, SD_RECEIVE);" should be inserted before closesocket();
"mmlab_js" <mmlabjs@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote...
I write a web client which downloads a specific file from specific web server.
The link of code is here:[http://src.wtgstudio.com/?Ydw5Nc#]
I try to download this file
"http://mail.google.com/mail/help/images/logo.gif" by command:[Download
http://mail.google.com/mail/help/images/logo.gif].
After sending GET request, I check the return value of function recv and
find the value is 0.
I see multiple recv() statements in the code (lines 117 and 146). It is unclear
which one returns 0. I assume it is the one on line 117.
The recv() on line 117 may return 0 bytes. This is a normal case which may arise
due to various circumstances. 0 bytes does not mean "no data sent". It means
"no data available at this time, but there was a reason to return". This case is not
handled by the code.
When debugging the code, timing will be totally different. By the time the recv()
call at line 117 is executed in debug mode, data will be available and is returned.
The receive data processing in the code is a typical example of misunderstanding
winsock TCP sockets. There is an invalid implicit assumption that all headers will
be delivered by the first recv() call and that the data is delivered by the second
recv() call.
There is no guarantee that this happens. Each recv() call may return an arbitrary
amount of data (up to the specified buffer size) which is unrelated to the chunks
in which the data is sent.
TCP is a byte stream protocol with no message boundaries!! Never ever forget
this.
The correct way to do this:
- send the request //OK
- shutdown the socket for send //OK
- receive all data in a single recv() loop and process the incoming data a stream
in accordance with the HTTP protocol semantics
- shutdown the socket for receive
- close the socket
Regards,
-Roger
--
E-mail: rhunen@xxxxxxxxx
Home: http://www.xs4all.nl/~rhunen
ADSL: http://adsl.hunen.net
.
- Follow-Ups:
- Re: Receive no data
- From: mmlab_js
- Re: Receive no data
- From: Volodymyr Shcherbyna
- Re: Receive no data
- References:
- Re: Receive no data
- From: Volodymyr Shcherbyna
- Re: Receive no data
- Prev by Date: Re: Tcp Idle (inactive) Connection goes down
- Next by Date: Re: Receive no data
- Previous by thread: Re: Receive no data
- Next by thread: Re: Receive no data
- Index(es):
Relevant Pages
|