我写的一个从百度服务器获取应答的程序_本来想获取百度首页的源代码的

#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<netinet/in.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> #define HTTP_PORT 80 int main(int argc,char *argv[]) {   int sock;   FILE *fp;   char buf[BUFSIZ];   char *http_request="GET / HTTP/1.1/r/nAccept:image/jpeg/r/nAccept-Language:zh-cn/r/nAccept-Encoding:gzip,deflate/r/nAgent:Mozilla/4.0/r/nHost:www.baidu.com/r/nConnection:Keep-Alive/r/n/r/n";   struct sockaddr_in serv;   struct hostent *hp;   if((sock=socket(AF_INET,SOCK_STREAM,0))<0)   {     perror("sock error");     exit(1);   }   if((hp=gethostbyname("www.baidu.com"))==NULL)   {     perror("gethostbyname error");     exit(1);   }       bzero(&serv,sizeof(serv));   bcopy(hp->h_addr,&serv.sin_addr,hp->h_length);   serv.sin_port=htons(HTTP_PORT);   serv.sin_family=AF_INET;   if(connect(sock,(struct sockaddr*)&serv,sizeof(serv))<0)   {     perror("connet error");     exit(1);   }   /*if((fp=fdopen(sock,"w"))==NULL)   {     perror("fdopen error");     exit(1);   } */   write(sock,http_request,strlen(http_request));   if((fp=fdopen(sock,"r"))==NULL)   {     perror("fdopen error");     exit(1);   }   while(fgets(buf,BUFSIZ,fp)!=NULL)     printf("%s",buf);     fclose(fp);   close(sock);   printf("read finished!");       exit(0); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值