not recv()'ing everything [modified]
-
Hey guys, I am trying to fetch the data from here -> http://74.54.176.106/whois/whois.cgi?domain=127.0.0.1[^] Though I am not receiving everything, this is all I get:
Tried to google but couldn't find anything that helped me.
my code:
char buffer[4096];
char strRequest[] = "GET /whois/whois.cgi?domain=127.0.0.1 / HTTP/1.0\r\n\r\n";
send(sock, strRequest, sizeof(strRequest), 0);
int i = recv(sock, buffer, sizeof(buffer), 0);modified on Friday, November 26, 2010 11:49 AM
-
Hey guys, I am trying to fetch the data from here -> http://74.54.176.106/whois/whois.cgi?domain=127.0.0.1[^] Though I am not receiving everything, this is all I get:
Tried to google but couldn't find anything that helped me.
my code:
char buffer[4096];
char strRequest[] = "GET /whois/whois.cgi?domain=127.0.0.1 / HTTP/1.0\r\n\r\n";
send(sock, strRequest, sizeof(strRequest), 0);
int i = recv(sock, buffer, sizeof(buffer), 0);modified on Friday, November 26, 2010 11:49 AM
Please try to correct the "code block" so readers can understand. You supplied a buffer length that good enough. However you should note that the socket :recv API does not garantee to you the receipt of all incoming data at once. You must loop on that function untill either the peer server closes the connection (I think you will get a -1 return value from recv) or until you know that you received what you "want" ( can only be done if you decode and interpret data at same time ). Let me suppose that you are talking to a peer webserver, so we know that when all data is sent, the server immediately closes the connection. In that case, your code should be like this :
char buffer[4096];
int offset = 0;
while ( true )
{
int i = recv(sock, buffer+offset, sizeof(buffer)-offset, 0);
if(i != -1){
offset += i; // increment your offset, so the next chunk of data will be appended to the previously received chunk and not overwrite it
else
break;
}It is the same thing with the ::send API. You can not garantee that all passed data will be sent. In both cases the system will give you the count of what it managed to process. Of course you can help avoid these complexities but just using an existing library. Hope this helps.
Push Framework - now released ! http://www.pushframework.com
-
Please try to correct the "code block" so readers can understand. You supplied a buffer length that good enough. However you should note that the socket :recv API does not garantee to you the receipt of all incoming data at once. You must loop on that function untill either the peer server closes the connection (I think you will get a -1 return value from recv) or until you know that you received what you "want" ( can only be done if you decode and interpret data at same time ). Let me suppose that you are talking to a peer webserver, so we know that when all data is sent, the server immediately closes the connection. In that case, your code should be like this :
char buffer[4096];
int offset = 0;
while ( true )
{
int i = recv(sock, buffer+offset, sizeof(buffer)-offset, 0);
if(i != -1){
offset += i; // increment your offset, so the next chunk of data will be appended to the previously received chunk and not overwrite it
else
break;
}It is the same thing with the ::send API. You can not garantee that all passed data will be sent. In both cases the system will give you the count of what it managed to process. Of course you can help avoid these complexities but just using an existing library. Hope this helps.
Push Framework - now released ! http://www.pushframework.com
Thanks, I got where you are coming from. Unfortunately the connection doesnt close, it just sits there. Tried sending
char strRequest[] = "GET /whois/whois.cgi?domain=127.0.0.1 HTTP/1.0\r\nConnection: Close\r\n\r\n";
but nothing. Can you suggest me an already existing library that isn't as huge as curlpp or boost, or generally one that is somewhat easy to handle? I am still new to c++ as you may know.
modified on Friday, November 26, 2010 12:13 PM
-
Hey guys, I am trying to fetch the data from here -> http://74.54.176.106/whois/whois.cgi?domain=127.0.0.1[^] Though I am not receiving everything, this is all I get:
Tried to google but couldn't find anything that helped me.
my code:
char buffer[4096];
char strRequest[] = "GET /whois/whois.cgi?domain=127.0.0.1 / HTTP/1.0\r\n\r\n";
send(sock, strRequest, sizeof(strRequest), 0);
int i = recv(sock, buffer, sizeof(buffer), 0);modified on Friday, November 26, 2010 11:49 AM
You need to study the rules of sockets. A
recv()
call will only return the amount of data that is currently available on the socket. You must continue to receive data until you get the end of the transmission (I cannot remember the last piece of an HTTP message, but I'm sure you can Google it). Don't just assume you will get the entire message in one go.Just say 'NO' to evaluated arguments for diadic functions! Ash
-
Thanks, I got where you are coming from. Unfortunately the connection doesnt close, it just sits there. Tried sending
char strRequest[] = "GET /whois/whois.cgi?domain=127.0.0.1 HTTP/1.0\r\nConnection: Close\r\n\r\n";
but nothing. Can you suggest me an already existing library that isn't as huge as curlpp or boost, or generally one that is somewhat easy to handle? I am still new to c++ as you may know.
modified on Friday, November 26, 2010 12:13 PM
Use Poco C++. I used it before eventhough I personaly did not touch the network library of it. (consists of 3 dlls maybe you just need the network one only). If I remember well there was a class called HttpClient so it will automatize your task. There are test suites as well so you can figure how things are being used and even take your decision of whether to use the library or not. http://pocoproject.org/
Push Framework - now released ! http://www.pushframework.com