这是我从服务器(实际上是 google.com)获取网页的代码:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
char http[] = "GET / HTTP/1.1\nAccept: */*\nHost: www.google.com\nAccept-Charset: utf-8\nConnection: keep-alive\n\n";
char page[BUFSIZ];
int main(int argc, char **argv)
{
struct addrinfo hint, *res, *res0;
char *address = "www.google.com";
char *port = "80";
int ret, sockfd;
memset(&hint, '\0', sizeof(struct addrinfo));
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_STREAM;
/* hint.ai_protocol = IPPROTO_TCP; */
if((ret = getaddrinfo(address, port, &hint, &res0)) < 0)
{
perror("getaddrinfo()");
exit(EXIT_FAILURE);
}
for(res = res0; res; res = res->ai_next)
{
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(-1 == sockfd)
{
perror("socket()");
continue;
}
ret = connect(sockfd, res->ai_addr, res->ai_addrlen);
if(-1 == ret)
{
perror("connect()");
sockfd = -1;
continue;
}
break;
}
if(-1 == sockfd)
{
printf("Can't connect to the server...");
exit(EXIT_FAILURE);
}
send(sockfd, http, strlen(http), 0);
recv(sockfd, page, 1023, 0);
printf("%s\n", page);
return 0;
}
我刚刚定义了一个“BUFSIZ”字符数组来存储网页。BUFSIZ 在我的操作系统上实际上是 1024 个字符,因此,我可以存储一个长度为 1024 个字符的网页。但是如果页面实际上大于 1024 怎么办?我的意思是,如何存储大于 1024 个字符的页面?我可以定义一个包含 2048、4096 甚至 10,000 个字符的数组,但我认为这不是常规方式。
谢谢。