⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socket_html.h

📁 it is about embeded system
💻 H
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <stdarg.h>#include <string.h>#include <errno.h>#include <netdb.h>#include <fcntl.h>#include <sys/time.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>int getHtmlFile(char *hostip,char *file){	int fd;	int flag=0;	FILE *fp;	char buf[1024];	int dnum;	char quest[1024];	char filename[60];	int rc;	struct sockaddr_in peer;	char hosturl[40];	struct hostent *hp; //= gethostbyname(argv[1]);	printf("start...\n");	strcpy(hosturl,hostip);	hp=gethostbyname(hosturl);	bzero( &peer, sizeof(peer) );	peer.sin_family = AF_INET;	printf("why? %s\n",hosturl);	if ( hp == NULL )	{		printf("unknow host: %s\n",hosturl);		return 0;	}	peer.sin_addr = *( ( struct in_addr * )hp->h_addr );	peer.sin_port = htons(80);	printf("test...\n");	printf("conncet to %s :%d \n",inet_ntoa(peer.sin_addr),ntohs( peer.sin_port ));		fd = socket( AF_INET, SOCK_STREAM, 0 );	if ( fd < 0 )	{		printf("socket call failed\n");		return 0;	}	if( connect( fd, (struct sockaddr *)&peer, sizeof( peer ) ) )	{		printf("%d connect failed \n",errno);		close( fd );		return 0;	}	sprintf(filename,file);	sprintf(quest,"GET /%s HTTP/1.0\r\nUser-agent:Mozilla/4.0\r\nAccept-language:zh-cn\r\n\r\n",file);	printf("I will read for you! %s \n------------------------\n%s\n",quest,filename);	if((rc = send(fd, quest, sizeof(quest), 0)) == -1)	{		if((errno != EWOULDBLOCK) && (errno != EAGAIN))		{			printf("send conn error!\n");			return 0;		}	}	fp = fopen( filename, "wb" );	if(fp==NULL)		return 0;	flag=0;	while((rc = recv( fd, buf, sizeof(buf) - 1, 0 )) > 0)	{		fwrite(buf, sizeof(char), rc, fp);		printf("%s",buf);		flag+=1;	}	fclose( fp );	return flag-1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -