Hey guys I am kinda new to the network programming scene so forgive the bad style, anyways I can't seem to find out how to recv() the Response header with Content-Length so I know how many bytes to recv() with the socket to get the whole page (I don't want to have to read it in one byte at a time). I have used both GET / and HEAD / in my request to the server, I do get a header and some of the actual html but I am not getting the Content-Length. If someone can give me a few pointers on how to grab it using a socket ("Not a link to beej's programming guide") It would be much appreciated.
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
const char *pServer = "www.google.com";
const char *pPort = "80";
int Receive(int sock, char *pLine, unsigned int line_size) {
char byte = 0;
while (byte != '\n' && strlen(pLine) < line_size) {
if (!recv(sock, (char *) &byte, 1, 0))
return 0;
if (byte != '\r' && byte != '\n' && byte != '\0') {
strncat(pLine, &byte, 1);
}
}
printf("pLine\r\n%s\r\n", pLine);
if (!recv(sock, pLine, 3604, 0)) {
perror("recv()");
} else {
printf("<< Recieved: %s\r\n", pLine);
}
return 0;
}
int main() {
struct addrinfo hints, *res;
int sock, status;
char ipstr[INET6_ADDRSTRLEN], *ipver;
void *addr;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo(pServer, pPort, &hints, &res)) != 0) {
printf("GetAddrInfo: %s\n", gai_strerror(status));
return 2;
}
printf("Ip for %s\r\n", pServer);
if (res->ai_family == AF_INET) { // IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else { // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)res->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
// Convert IP To string and print
inet_ntop(res->ai_family, addr, ipstr, sizeof(ipstr));
printf(" %s: %s\r\n", ipver, ipstr);
// Create the socket
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock == -1) {
perror("Socket()");
return 1;
} else {
printf("- Socket created\r\n");
}
// Connect with socket to server
if ((connect(sock, res->ai_addr, res->ai_addrlen)) != 0) {
perror("Connect()");
return 1;
} else {
printf("- Con Success\r\n");
}
// Send Message to Server
char buffer[3604], *Msg = "GET / HTTP/1.0\r\nHost: www.google.org\r\n\r\n";
if ((send(sock, Msg, strlen(Msg), 0)) == -1) {
perror("send()");
return 1;
} else {
printf("- Send Success\r\n");
}
Receive(sock, buffer, 3604);
// Kill Socket
shutdown(sock,2);
printf("- Socket Closed\r\n");
freeaddrinfo(res);
return 0;
}
/**** OUTPUT ******** (USING "GET /")
Ip for www.google.com
IPv4: 74.125.19.103
- Socket created
- Con Success
- Send Success
pLine
HTTP/1.0 200 OK
<< Recieved: Content-Type: text/html
Last-Modified: Wed, 03 Jun 2009 20:15:03 GMT
Date: Tue, 29 Sep 2009 01:43:07 GMT
Server: gws
Cache-Control: private, x-gzip-ok=""
X-XSS-Protection: 0
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Google.org</title>
<link href="fixed.css" rel="stylesheet" type="text/css">
<link href="
http://www.google.com/earthday08/images/btn-flextoolbtn/btn-flextoolbtn.css" rel="stylesheet" type="text/css" />
<style>
.promos { padding-top:0; margin-top:0; color:#52AA65; border:1px double #E8E8E8; border-width:3px; padding:5px; }
</style>
<script type="text/javascript" src="
http://www.google.com/jsapi?key=ABQIAAAAqMAZpYIqo07WG-7RUKnPWRTEmX02MCJTV1yX409NSTlckkgiDxSKPj1hADfauFLOkcG4efIoBrv60Q"></script>
<script type="text/javascript" src="sidefeeds.js"></script>
<script src="
http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1629883-1";
urchinTracker();
</script>
</head>
<body id="home">
<div id="container">
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="layout">
<tr>
<td colspan="3" id="header"><div class="right">
<div class="divider"><a href="
http://www.google.org"><img src="images/logo_tiny.gif" width="156" height="46"><
- Socket Closed
**************** END OUTPUT *****************/