linux c socket编程抓取网页的问题
最近在学习linux的网络编程,从网上找了分抓取网页的代码,但是不知为什么不能够获取整个网页
代码如下:
[code=C/C++]
[#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#define HTTPPORT 80
char *head = "GET /about HTTP/1.1\r\n"
"Accept: */*\r\n"
"Accept-Language:zh-cn\r\n"
"Accept-Encoding:gzip,deflate\r\n"
"Host:www.ietf.org\r\n"
"Connection:Keep-Alive\r\n\n";
int connect_URL(char *domain,int port)
{
int sock;
struct hostent *host;
struct sockaddr_in server;
host = gethostbyname(domain);
if(host == NULL)
{
printf("gethostbyname error\n");
return -2;
}
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0)
{
printf("invalid socket\n");
return -1;
}
memset(&server,0,sizeof(struct sockaddr_in));
memcpy(&server.sin_addr,host->h_addr_list[0],host->h_length);
server.sin_family = AF_INET;
server.sin_port = htons(port);
if(connect(sock,(struct sockaddr *) &server,sizeof(struct sockaddr)) < 0 )
return -1;
else
return sock;
}
int main()
{
int sock;
int n;
char buf[1024];
char *domain ="www.ietf.org";
FILE *fp;
fp = fopen("test.txt","w");
if(NULL == fp)
{
printf("can't open stockcode file\n");
return -1;
}
sock = connect_URL(domain,HTTPPORT);
if(sock < 0)
{
printf("connect errror\n");
return -1;
}
send(sock,head,strlen(head),0);
while(1)
{
if((n=recv(sock,buf,1024,MSG_WAITALL)) < 1)
&nbs