-
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?
-
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.