Hi.
I would like to make a c program that will be able to download a webpage, then save it the the harddisk, through using sockets.
For example, it will download a webpage onto the disk as an HTML file .
I am finding this very difficult,
I have found the following code, but when i compile it with gcc it comes up with about 20 errors![]()
I would be grateful for any help at all
Code:/** * This function sends the http headers to the specified server. The result is then * copied into a passed buffer. * lpszServer - The webserver address * lpszHttp - The http request * data - buffer to hold the html returned * datasize - the size of the data buffer */ #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> BOOL SendHttp(LPCTSTR lpszServer, LPCTSTR lpszHttp, LPTSTR data, UINT datasize) { SOCKET s; WSADATA wsaData; struct sockaddr_in hostaddr; struct hostent *serverent; char *serverip; char buff[1024]; int i,bytes; /* Initialize sockets */ if(WSAStartup(MAKEWORD(2,2),&wsaData)) { DisplayError("Error Initializing Sockets",GetLastError()); return FALSE; } /* create a socket descriptor */ s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(s == INVALID_SOCKET) { DisplayError("Error creating socket.",GetLastError()); return FALSE; } /* Get a hostent structure from the domain name */ if(!(serverent = gethostbyname(lpszServer))) { DisplayError("Could not resolve host name.",GetLastError()); return FALSE; } /* Get the ip address from the hostent structure */ if(!(serverip = inet_ntoa(*(struct in_addr *)*serverent->h_addr_list))) { DisplayError("Call to inet_ntoa failed",0); return FALSE; } memset(&hostaddr,0,sizeof(struct sockaddr_in)); hostaddr.sin_family = AF_INET; hostaddr.sin_addr.s_addr = inet_addr(serverip); hostaddr.sin_port = htons(80); /* Connect to the server */ if(connect(s,(struct sockaddr*)&hostaddr,sizeof(struct sockaddr))) { DisplayError("Unable to connect to server.",GetLastError()); return FALSE; } /* Send the http headers */ if(send(s,lpszHttp,strlen(lpszHttp),0) == SOCKET_ERROR) { DisplayError("Error Sending HTTP data.",GetLastError()); return FALSE; } /* Receive a response */ i = 0; while(1) { bytes = recv(s,buff,sizeof(buff),0); if(bytes <= 0) break; if( (bytes + i + 1) > datasize) break; /* dont overflow the buffer */ memcpy(data + i, buff,bytes); i += bytes; } data[i] = 0; closesocket(s); return TRUE; } void DisplayError(LPCTSTR lpszError, int errornum) { char szError[256]; if(errno) sprintf(szError,"%s\n\nError Number: %d",lpszError,errornum); else sprintf(szError,"%s",lpszError); MessageBox(NULL,szError,"ERROR",MB_OK | MB_ICONERROR); }



LinkBack URL
About LinkBacks



