1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
WSADATA wsaData;
string http;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0)
{
struct sockaddr_in addr;
// describe the type of the connection and what port to use
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
HOSTENT* hostent = gethostbyname("www.google.com");
if (hostent)
{
addr.sin_addr.S_un.S_addr = (*reinterpret_cast<IPNumber*>(hostent->h_addr_list[0]));
memset(addr.sin_zero, '\0', sizeof(addr.sin_zero));
// create and bind the socket
int sock = socket(PF_INET, SOCK_STREAM, 0);
bind(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr));
// connect
connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr));
// send the HTTP HEAD request
char* msg = "HEAD / HTTP/1.1\r\nHost: www.google.com\r\n\r\n";
send(sock, msg, strlen(msg), 0);
// get back the response
char buf[1024];
memset(buf, 0, 1024);
int n = recv(sock, buf, 1024, MSG_PEEK);
int len = strlen(buf);
shutdown(sock, 2);
}
}
WSACleanup();
|