#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <iconv.h>
int main()
{
int sock=socket(AF_INET,SOCK_STREAM,0);
struct hostent* he=gethostbyname("war.163.com");
if(he==NULL)
{
perror("gethostbyname");
exit(1);
}
const char* srv_ip=inet_ntoa(*(struct in_addr*)(he->h_addr_list[0]));
//printf("%s\n",srv_ip);
struct sockaddr_in srv_addr;
srv_addr.sin_family=AF_INET;
srv_addr.sin_addr.s_addr=inet_addr(srv_ip);
srv_addr.sin_port=htons(80);
if(-1==connect(sock,(struct sockaddr*)&srv_addr,sizeof(srv_addr)))
{
perror("connect");
exit(1);
}
char request[]="GET / HTTP/1.1\r\n"
"Host: war.163.com\r\n"
"Connection: close\r\n"
"\r\n";
send(sock,request,strlen(request),0);
int buff_len=5*1024*1024;
char *buff=(char*)malloc(buff_len);
if(buff==NULL) perror("malloc");
int ret;
int cnt=0;
while(1)
{
ret=recv(sock,buff+cnt,buff_len-cnt,0);
if(ret<1) break;
cnt+=ret;
}
buff[cnt]='\0';
//printf("%s\n",buff);
char *buff2=(char*)malloc(2*cnt);
memset(buff2,0,2*cnt);
char* pb1=buff;
char* pb2=buff2;
size_t len1=cnt;
size_t len2=2*cnt;
iconv_t cd=iconv_open("UTF8","GBK");
iconv(cd,&pb1,&len1,&pb2,&len2);
iconv_close(cd);
//printf("%s\n",buff2);
char* p1=strstr(buff2,"国际军情");
if(p1!=NULL)
{
int i;
for(i=0;i<5;i++)
{
char* p2=strstr(p1,"html\">")+strlen("html\">");
char* p3=strstr(p2,"</a>");
char msg[1000]="";
strncpy(msg,p2,p3-p2);
printf("%s\n",msg);
p1=p3;
}
}
free(buff);
free(buff2);
close(sock);
return 0;
}
标签:addr,int,编程,char,获取,srv,buff,include,头条
From: https://www.cnblogs.com/HappyTeemo/p/18509815