Thread: socket (recive data from httpd)

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    How I can recive response from httpd (for example Apache). Where I can get size response?

    My code:

    Code:
     #define BUFFER_SIZE 1024
    
     char message[] = "GET /index.php HTTP/1.1\r\nHOST:  example.com\r\n\r\n";
     char buf[BUFFER_SIZE];
     int sock;
     struct sockaddr_in addr;
     char hostname[40] = "example.com";
     struct hostent *he;
     struct in_addr ip_addr;
     char content[102401];
    
    
     sock = socket(AF_INET, SOCK_STREAM, 0);
     if(sock < 0)
     {
      perror("socket");
      exit(1);
     }
    
     addr.sin_family = AF_INET;
     addr.sin_port = htons(80);
     he = gethostbyname(hostname);
     if(he==NULL)
     {
      printf("unknown host: %s \n ", hostname);
      exit(1);
     }
     bcopy(he->h_addr, (char *) &ip_addr, sizeof(ip_addr));
     addr.sin_addr.s_addr = inet_addr(inet_ntoa(ip_addr));
     if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
     {
      perror("connect");
      exit(2);
     }
     send(sock,message,strlen(message),0);
    
     int key = 0;
     while(key < 100)
     {
      recv(sock,buf,BUFFER_SIZE,0);
      strcat(content,buf);
      key++;
     }
     printf("%s",content);

    I have problem with this loop:
    Code:
     int key = 0;
     while(key < 100)
     {
      recv(sock,buf,BUFFER_SIZE,0);
      strcat(content,buf);
      key++;
     }
    - unclean when stoping reciving data.


    And how I can send response to httpd and say him "Successful recive data"??



    From httpd I recive HTTP header like this:

    Code:
    HTTP/1.1 200 OK
    Date: Sat, 12 Mar 2011 17:31:45 GMT
    Server: Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 PHP/5.2.16
    X-Powered-By: PHP/5.2.16
    Set-Cookie: PHPSESSID=79c26a99d9050d6d5345f397f949f81f; path=/
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    Connection: close
    Transfer-Encoding: chunked
    Content-Type: text/html
    
    11f6e
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    And if I need recive image (binary file), how I can remove from response HTTP header?
    Last edited by grytskiv; 03-12-2011 at 11:56 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There are many things wrong with your recv loop.

    1. You're trying to strcat to a block of memory which is uninitialised.
    2. You're trying to strcat what might suddenly turn into binary data. The header is all text for sure, but the rest of it?
    3. You're ignoring the return result of recv

    > how I can remove from response HTTP header?
    Look for the first \r\n\r\n

    I was kinda surprised that you didn't get a Content-Length field in your header.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Replies: 6
    Last Post: 12-27-2007, 11:10 AM
  4. jumble socket data
    By stormy in forum C Programming
    Replies: 10
    Last Post: 08-23-2005, 10:07 AM
  5. Replies: 4
    Last Post: 06-14-2005, 05:45 AM