How to detect completion of Socket recv() function.
-
Vis C/C++ 6.0 Vis Studio I am receiving web pages using recv(). I am looping and receiving chunks of the page and am concatinating them to a buffer. I get the whole file but the loop hangs because I have no detection of the end of file. If I manually close the socket, I always have the full file. I have tried the recv() MSG_PEEK flag inside of the loop to detect the end of the file, but I don't seem to be able to get it to work. It hangs as well. I've seen some code on CodeProject that looks for 2 consequtive CRLFs but that really only works if you are only expecting a Header. Any help would be much appreciated. Thanks, Robert :-)
-
Vis C/C++ 6.0 Vis Studio I am receiving web pages using recv(). I am looping and receiving chunks of the page and am concatinating them to a buffer. I get the whole file but the loop hangs because I have no detection of the end of file. If I manually close the socket, I always have the full file. I have tried the recv() MSG_PEEK flag inside of the loop to detect the end of the file, but I don't seem to be able to get it to work. It hangs as well. I've seen some code on CodeProject that looks for 2 consequtive CRLFs but that really only works if you are only expecting a Header. Any help would be much appreciated. Thanks, Robert :-)
-
Vis C/C++ 6.0 Vis Studio I am receiving web pages using recv(). I am looping and receiving chunks of the page and am concatinating them to a buffer. I get the whole file but the loop hangs because I have no detection of the end of file. If I manually close the socket, I always have the full file. I have tried the recv() MSG_PEEK flag inside of the loop to detect the end of the file, but I don't seem to be able to get it to work. It hangs as well. I've seen some code on CodeProject that looks for 2 consequtive CRLFs but that really only works if you are only expecting a Header. Any help would be much appreciated. Thanks, Robert :-)
Receive returns the number of bytes now in buffer(Received). If it is 0(zero) Then received all.You can turn back.
-
Receive returns the number of bytes now in buffer(Received). If it is 0(zero) Then received all.You can turn back.
Hi Ajesh, Thank-you for reply. Unfortunately what is happening is that the recv() call is blocking - it is hung, so I don't get any "number of bytes" to even look at! If I manually close the socket, then I have the full file, but a 'manual close' approach would be very undesirable to implement. Any other suggestions? Many thanks, Robert
-
I have no experience at all, but did you try using http / 1.0 or add the "Connection: close" header?
Hi zzattack, Thanks for reply. I am using the recv() function and it is a blocking call. Since I do not know the size of the file that is coming, I keep calling recv() to get the full file. What happens is that when I call it the final time, the call blocks - and I am hung there. Robert
-
I have no experience at all, but did you try using http / 1.0 or add the "Connection: close" header?
Hi zzatack, Got it working! :-) I added Connection: close as you recommended. I guess that the recv() function was continuing to wait for more packets since the connection was still open. I'm not sure how you can say, "I have no experience at all..." when in fact you put your finger right on the problem!! Thanks again Robert :-)
-
Hi zzatack, Got it working! :-) I added Connection: close as you recommended. I guess that the recv() function was continuing to wait for more packets since the connection was still open. I'm not sure how you can say, "I have no experience at all..." when in fact you put your finger right on the problem!! Thanks again Robert :-)
Haha, cool, good to hear. I'll try to explain how I think it works. Actually your recv() function returns when it has read what you wanted (like 4096 or 8192 bytes etc.), or if the connection closes. But, if there's not enough data left to fill the buffer (eof), recv() keeps waiting, and the connection remains opened so that more data can be sent and added to the buffer later. By default on HTTP/1.0 the connection is closed, but with HTTP/1.1 it remains opened unless "Connection: close" was sent along with the headers. Therefor, recv() returns only when the server closes the connection because of a timeout. That might take minutes, and your code inded appears to hang :( I meant I have no c/c++ experience, only a bit of the HTTP protocol which I learned fom mIRC scripting. I was actually looking at the boards for tutorials :). Anyway, have fun.