2019独角兽企业重金招聘Python工程师标准>>>
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define GET_CMD "GET %s HTTP/1.1\r\nConnection: Keep-Alive\r\n\r\n"
#define array_size(array) \
(sizeof(array)/sizeof(array[0]))
#define for_each(head, ptr) \
for(ptr = head; ptr != NULL; ptr = ptr->ai_next)
struct web_database
{
const char* hostname;
const char* service;
FILE* file;
}database[] = {
{"www.baidu.com", "80", NULL},
{"www.renren.com","80", NULL},
{"www.nwpu.edu.cn", "80", NULL},
{"www.bjtu.edu.cn", "80", NULL},
{"www.openlan.net", "80", NULL},
{"www.google.com", "80", NULL},
{"web.qq.com", "80", NULL},
{"www.cnblog.com", "80", NULL}
};
struct addrinfo* host_svr(const char* hostname, const char* service,
int family,
int sock_type)
{
int n;
struct addrinfo hints;
struct addrinfo* res;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME;
hints.ai_family = family;
hints.ai_socktype = sock_type;
if((n = getaddrinfo(hostname, service, &hints, &res)) != 0)
{
printf("getaddrinfo error: %s\n", gai_strerror(n));
return NULL;
}
return res;
}
int tcp_connect(const char* hostname, const char* service)
{
int sockfd;
int n;
struct addrinfo hints;
struct addrinfo* res;
struct addrinfo* ressave;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
if((n = getaddrinfo(hostname, service, &hints, &res)) != 0)
{
printf("getaddrinfo failed: %s, %s, %s\n", hostname, service, gai_strerror(n));
return -1;
}
ressave = res;
do
{
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(sockfd < 0)
continue;
if(connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
break;
close(sockfd);
}while((res = res->ai_next) != NULL);
if(res == NULL)
printf("getaddrinfo error\n");
freeaddrinfo(ressave);
return sockfd;
}
int main()
{
int sockfd;
int n;
struct addrinfo* res_head;
struct addrinfo* ptr;
int ret;
char buffer[1024] = {0};
for(n = 0; n != array_size(database); n++)
{
res_head = host_svr(database[n].hostname, database[n].service, AF_INET, SOCK_STREAM);
for_each(res_head, ptr)
printf("%s ip: %s\ncanonname: %s\n", database[n].hostname, inet_ntoa(((struct sockaddr_in*)(ptr->ai_addr))->sin_addr), ptr->ai_canonname);
sockfd = tcp_connect(database[n].hostname, database[n].service);
snprintf(buffer, sizeof(buffer), GET_CMD, "/");
printf("cmd: %s\n", buffer);
ret = write(sockfd, buffer, strlen(buffer));
database[n].file = fopen(database[n].hostname, "w+");
if(database[n].file < 0)
printf("%s file create failed\n", database[n].hostname);
ret = read(sockfd, buffer, 1024);
fprintf(database[n].file, "%s", buffer);
close(sockfd);
}
return 0;
}