|
Hi, all!
I know it is a simple question, but I assure that have searched in maaaany places for an answer for it and haven't found yet. I've looked even at HTTP RFC and Transmission Control Protocol RFC, but didn't find at first sight, not so deep.
I also don't know if here at WSK Forum is the real place to take this question, but I didn't find any better.
I just want to know how can I know if one side of the connection has ended up sending data. I am trying to connect to an IIS webserver, for example. I am the client and send to the server any GET command. Then, I enter in a loop of recv(), like this: char buffer[1024]; int bytesRecv=0, totalBytes=0; while(bytesRecv!=SOCKET_ERROR) { bytesRecv=recv(ConnectSocket, buffer, 1024, 0); if(bytesRecv!=SOCKET_ERROR) totalBytes+=bytesRecv; if(bytesRecv==0) {printf("bytes received: %d\n", totalBytes);break;} }
When the server sends the last data, then the recv returns a number different of 1024 (400, for instance), and the loop take the program to another call of recv(), that stays stopped, waiting for more data... And my program stays locked.
Of course that I've already tried an IF inside of the loop that tests if the bytesRecv is different from 1024 to break the loop. Theorically, this must be the last data received. But I have two problems: if the transaction was perfect, with no errors, than I could find someday one transaction that has a data of 1024*x size, with x an integer. Than the LAST call of recv would return 1024, and would not be TRUE with my IF test. And also... The transactions aren't perfect. My first "IF test" receiving 80kB from a webserver broke the loop at the first 12kB of data. Because at this point, my recv returned less than 1024 bytes... And was not the end of the transaction.
The example code in MSDN doesn't have a loop of recv(), because it's just... Too simple! Just take a buffer larger than the data, than make ONE call to the recv() function. But the common case is when the buffer is smaller than the data. And I just don't find an example of it! The various examples that I found in the web that shows buffers smaller than the data uses a convention created by the owners of the examples... And all conventions sends the first data how many bytes will come in the transaction. But that's still not the common case of the HTTP servers, or the FTP servers, or the POP servers, ....
Can somebody help me with that?
Gratefully, Thales Medeiros. |