Code:
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
#define STORAGE_LEN 1000
#define DEFAULT_PORT "80"
char storage[STORAGE_LEN];
SOCKET ConnectSocket = INVALID_SOCKET;
void write_to_file(char *storage)
{
FILE *p_openfile = fopen("c:\\programs\\networking\\webpagecontent.txt" , "a+");
fprintf(p_openfile, "%s\n", storage);
fclose(p_openfile);
}
int main(void)
{
WSADATA wsaData;
char ServerName[40] = "www.example.com";
// char FilePath[40] = "/index.html";
int iResult;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if(iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo(ServerName, DEFAULT_PORT, &hints, &result);
if(iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// Create a socket for connecting to the server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if(ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Connect to server
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if(iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if(ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to the server!\n");
return 1;
}
strcpy(storage,"GET / HTTP/1.1\r\n");
strcat(storage, "User-Agent: Opera/9.27 (Windows NT 5.1; U; en)");
strcat(storage, "\r\n");
strcat(storage, "Host: www.example.com");
strcat(storage, "\r\n");
strcat(storage, "Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */ *;q=0.1");
strcat(storage, "\r\n");
strcat(storage, "Accept-Language: en-US,en;q=0.9\r\nAccept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1");
strcat(storage, "\r\n");
strcat(storage, "Accept-Encoding: deflate, gzip, x-gzip, identity, *;q=0");
strcat(storage, "\r\n");
strcat(storage, "Connection: Keep-Alive");
strcat(storage, "\r\n\r\n");
printf("%s", storage);
// Send an initial buffer
iResult = send(ConnectSocket,storage,sizeof(storage),0);
if (iResult == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
sprintf(storage, "shutdown = %d\n", iResult);
write_to_file(storage);
do {
iResult = recv(ConnectSocket,storage,STORAGE_LEN,0);
if ( iResult > 0 ) {
write_to_file(storage);
printf("Bytes received: %d\n", iResult);
}
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
} while( iResult > 0 );
// cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}