I am trying to create a simple web browser, but I have not found any tutorials. I have read a socket tutorial (but it is not on creating a web browser), and the code I give below is what I have come with. I do not know why it does not work at all. It kind of stops at the receive part. I used a packet sniffer and saw that I was sending some data to the site address, but I am not getting the webpage HTML code back. Can somebody tell me what I am doing wrong?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <winsock.h>
WSADATA wsaData;
char buf[100];
int socketsrc, bytesRecv, bytesSent;
struct sockaddr_in dest_addr;
struct hostent *h;
char *msg = "GET / HTTP/1.1";
int main(int argc, char *argv[])
{
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
{
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
if ((socketsrc = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
printf("\nsuccess: socket");
if (argc != 2) { // error check the command line
fprintf(stderr,"usage: getip address\n");
exit(1);
}
if ((h=gethostbyname(argv[1])) == NULL) { // get the host info
printf("\nNo Host for %s", argv[1]);
exit(1);
}
printf("\nsuccess: hostname");
dest_addr.sin_family = AF_INET; // host byte order
dest_addr.sin_port = htons(80); // short, network byte order
dest_addr.sin_addr.s_addr = inet_addr("216.239.57.99");
memset(&(dest_addr.sin_zero), 0, 8); // zero the rest of the struct
if (connect(socketsrc, (struct sockaddr *)&dest_addr,
sizeof(struct sockaddr)) == -1) {
perror("connect");
exit(1);
}
printf("\nsuccess: connect");
bytesSent = send(socketsrc, msg, strlen(msg), 0);
printf( "\nBytes Sent: %d", bytesSent );
//and this is where I think the program just stops
while((bytesRecv = recv( socketsrc, buf, 99, 0 )) != -1 ) {
if ( bytesRecv == 0) {
printf( "\nConnection Closed.");
break;
}
printf( "\nBytes Recv: %d", bytesRecv );
}
printf("\nsuccess: recieve");
closesocket(socketsrc);
WSACleanup();
return 0;
}