使用WinSock。
#include <string>
#include <iostream>
#include <fstream>
#include "winsock2.h"
#include <time.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#define DEFAULT_PAGE_BUF_SIZE 1048576
void main()
{
WSADATA wsaData;
int err;
err = WSAStartup(MAKEWORD(2,2), &wsaData);
if( err != 0 )
{
return;
}
// timer is start
clock_t start, finish;
double duration;
start = clock();
char host[] = "www.baidu.com";
char *request = "GET / HTTP/1.0\r\nHost: www.baidu.com\r\nConnection: Close\r\n\r\n";
struct hostent *hp;
hp = gethostbyname(host);
if(hp == NULL)
{
cout << "gethostbyname() error in GetIpByHost: " << host << endl;
return;
}
// 获取域名对应的IP
struct in_addr inAddr;
LP