http get requests etc

This is a discussion on http get requests etc within the C++ Programming forums, part of the General Programming Boards category; perror("recv") tells me "Recv: no error" even though recv teturned -1. Mt code is huge, and i don't know what ...

  1. #16
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    perror("recv") tells me "Recv: no error" even though recv teturned -1.

    Mt code is huge, and i don't know what part tp post. So, until i hear otherwise i am going to post my recvBlock code, and one of the contexts in which it is called.

    Code:
    ...
    
    int recvBlock(SOCKET client, char reply[], unsigned int flags = 0){
      int ret;
      if ((ret=recv(client, reply, (sizeof(reply)*256)-1, 0)) == -1) {
        cout << "Recv() returned: " << ret << endl; // DEBUG
        cout << "Recv() recieved: " << reply << endl; // DEBUG
        perror("recv");
      }
      return ret;
    }
    
    ...
    Code:
    ...
    
       char queryname[200];
       int queryport;
       SOCKET query;
       struct hostinfo queryhost;
       char buffer[1024];
       memset(buffer, '\0', sizeof(buffer));
    
       ...
    
       cin >> queryname;
       queryhost = remote_host_info(queryname);
    
       ...
    
       cin >> queryport;
       query = openSocket(queryhost.ip[0],queryport); 
    
       ...
    
       sendLine(query, "GET / HTTP/1.1\n");
       sendLine(query, "Accept: text/html, text/plain, */*\r\n");
       sendLine(query, "Accept-Encoding: gzip, deflate\r\n");
       sendLine(query, "Accept-Language: en-us\r\n");
       sendLine(query, "Host: ");
       sendLine(query, queryhost.name);//toUpper(queryhost.name));
       sendLine(query, "\r\n\r\n");
       while (recvBlock(query, buffer) == -1){
          cout << buffer;
       }
    
    ...
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    int recvBlock(SOCKET client, char reply[], unsigned int flags = 0){
      int ret;
      if ((ret=recv(client, reply, (sizeof(reply)*256)-1, 0)) == -1) {
    <snip>
    I believe you are specifying the buffer size incorrectly. You cannot use sizeof() on reply because it's only a pointer. To verify this, try something like:
    cout <<sizeof(reply)*256)-1;
    and see if it's what you expect it to be.

    To do this properly, I'd suggest adding another parameter to the recvBlock function that denotes the buffer length, and use that instead.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    Yep, it outputs what it should, 1023. And, it works fine with incoming sockets, just not my outgoing ones.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  4. #19
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    while (recvBlock(query, buffer) == -1){
          cout << buffer;
    }
    Don't you mean while (recvBlock() != -1) { print data } ? But you should also be checking for 0, which will probably be returned when the connection ends.

    >>Yep, it outputs what it should, 1023.
    It only does that because the sizeof a pointer is 4 (on your system), multiply by 256 = 1024, minus 1 to give you 1023. It is NOT working out the size of the array on it's own, you are telling it how big it is. I presume you know this though? Just to make sure, run this and see what you get:
    Code:
    #include <iostream>
    
    using std::cout;
    
    int func(char ary[])
    {
        return sizeof ary;
    }
    
    int main()
    {
        char myary[1024];
        cout <<func(myary);
    }
    But the buffer size isn't your problem, as it is ending up to be what you want by *accident*.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #20
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    THanks, and no i dodn't kmnow that. I don't have enough time tonight to test it out, bit i do want to explain why i had it == instead of != I wanted to see if it would chance the messace after a few loops. (Which it didn't.)

    I have also told it to connect it to my Apache server, and it doesn't even register the request. Hasn't anyone ever made their own HTTP reequests?? If you have, let me know which parts of my code you want t osee, and i will post it ASAP. Thanks.

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  6. #21
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I have also told it to connect it to my Apache server, and it doesn't even register the request.
    Have you run a sniffer to see what happens under the hood?
    Also, do you have any logging on your HTTP server that will show you malformed HTTP requests?

    If you want, post a program (zipped) that I can compile and run, and I'll have a look (no promises to fix it though )
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #22
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    Attached is the zipped source files. I am not very good with commenting things, so ask me if you have questions. I don't know if you will be able to compile, however, as I am using DEV-c++. And, it obviously needs the -lwsock32 command line option.

    Where can i get a sniffer program to do what you are suggesting?

    I don't think my Apache has one of those logs, but i do know that it is supposed to show all requests in the "access" log.

    ~Inquirer
    Attached Files Attached Files
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  8. #23
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm still looking, but here's the first bug:

    In function recvBlock():
    >>} else if (ret = 0) {
    You only put one equals sign.

    [edit]
    And another:
    >>reply[length] = '\0';
    This is outside the array bounds. This one is a common mistake I'm afraid

    [edit again]
    And you have more serious problems in remote_host_info().
    The reason recvBlock() isn't working is becuase you never made a connection to the server in the first place. All your send() calls are failing without warning. The problem stems from remote_host_info() which doesn't populate the IP address list properly. To see what I mean, add this to main() just after calling
    that function.
    cout <<"queryhost.ip[0] is " <<queryhost.ip[0]<<endl;

    And because the IP is not present, openSocket() doesn't work, and doesn't report any errors either.


    I strongly recommend you go back and add proper error checking to all your socket calls. It'll help you in the long run
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Persistent HTTP Requests
    By reelbigtim in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-08-2008, 01:06 AM
  2. Timing HTTP GET requests
    By traef06 in forum C Programming
    Replies: 9
    Last Post: 09-08-2008, 10:33 PM
  3. viewing HTTP requests
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-09-2007, 07:44 PM
  4. Writing all HTTP requests from a website to a log file
    By goomyman in forum C# Programming
    Replies: 1
    Last Post: 07-29-2005, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21