Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. not recv()'ing everything [modified]

not recv()'ing everything [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ALLERSLIT
    wrote on last edited by
    #1

    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

    A L 2 Replies Last reply
    0
    • A ALLERSLIT

      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

      A Offline
      A Offline
      Ahmed Charfeddine
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • A Ahmed Charfeddine

        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

        A Offline
        A Offline
        ALLERSLIT
        wrote on last edited by
        #3

        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

        A 1 Reply Last reply
        0
        • A ALLERSLIT

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • A ALLERSLIT

            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

            A Offline
            A Offline
            Ahmed Charfeddine
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups