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

📄 socket_html.c

📁 利用SOCKET编程做的一个获取html源码的程序
💻 C
字号:
/***************************************************  Copyright(C), 2008 , JUST  File name: socket_html.c  Author: Zhang Haidi   Version: 0.8     Date: 08/09/10  Description: CSocket.c的头文件	     完成网页的下载***************************************************/#include "./inc/socket_html.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>#include "./inc/t2s.h"/*网页原文件保存名*/#define DOWNLOAD_FILE_NAME "./wdt/getHtml.htm"extern char MP3_NAME[100];int is_mp3(char *filename);int getMp3FileSon(char *hostip,char *file);int getHtmlFileSon(char *hostip,char *file);int getHtmlFile(char *hostip){	char myhost[512];	char filename[512];	int i,j; 	printf("hostip=\n\n%s\n\n\n",hostip);	//sleep(1);	if(strstr(hostip,"http")!=NULL)	{		//printf("1\n");		//sleep(1);		i=0;		j=0;		strcpy(myhost,"\0");		strcpy(filename,"\0");		while(hostip[i+7]!='/'&&hostip[i+7]!='\0')		{			myhost[i]=hostip[i+7];			i++;		}		myhost[i]='\0';		if(hostip[i+7]=='\0')			strcpy(filename,"index.html");		else if(hostip[i+8]=='\0')			strcpy(filename,"index.html");		else		{			while(hostip[i+7]!='\0')			{				filename[j++]=hostip[i+7];				i++;			}			filename[j]='\0';			}		printf("Host:<%s>\nFileName:<%s>\n",myhost,filename);		//sleep(1);	}		else{		i=0;		j=0;		strcpy(myhost,"\0");		strcpy(filename,"\0");		while(hostip[i]!='/')		{			myhost[i]=hostip[i];			i++;		}		myhost[i]='\0';		if(hostip[i+7]=='\0')			strcpy(filename,"index.html");		else if(hostip[i+8]=='\0')			strcpy(filename,"index.html");		else		{			while(hostip[i+7]!='\0')			{				filename[j++]=hostip[i+7];				i++;			}			filename[j]='\0';			}			printf("Host:%s\nFileName:%s\n",myhost,filename);		}		printf("myhost=%s\nfilename=%s\n",myhost,filename);	//sleep(1);	if(is_mp3(filename))	{		strcpy(MP3_NAME,".");		strcat(MP3_NAME,filename);		return(getMp3FileSon(myhost,filename));	}	else  return(getHtmlFileSon(myhost,filename));}int is_mp3(char *filename){	char *ptr;	ptr=&(filename[strlen(filename)-3]);	printf("ptr=%s\n",ptr);	//sleep(1);	if(strcmp(ptr,"mp3")==0||strcmp(ptr,"MP3")==0)	{		printf("is_mp3 return 1\n");		//sleep(1);		return 1;	}	else 	{		printf("is_mp3 return 0\n");		//sleep(1);		return 0;	}}int getMp3FileSon(char *hostip,char *file){	int fd;	int flag=0,debug=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 -1;	}	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 -1;	}	if( connect( fd, (struct sockaddr *)&peer, sizeof( peer ) ) )	{		printf("%d connect failed \n",errno);		close( fd );		return -1;	}	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 -1;		}	}	fp = fopen( MP3_NAME, "w+" );	if(fp==NULL)	{		return -1;	}	flag=0;	while((rc = recv( fd, buf, sizeof(buf) - 1, 0 )) > 0)	{		fwrite(buf, sizeof(char), rc, fp);		printf("%d\x0d",debug++);		//printf("%s",buf);		if(flag%1000==0)			T2S_t2s("下载中");		flag+=1;	}	close( fd );	fclose( fp );	if(flag==0)	{		strcpy(buf,"rm -rf ");		strcat(buf,MP3_NAME);		system(buf);		return -1;	}	else 	{		return 2;	}	//return flag-1;}int getHtmlFileSon(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( DOWNLOAD_FILE_NAME, "w+" );	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;	}	close( fd );	fclose( fp );	if(flag==0)		return 0;	else return 1;	//return flag-1;}

⌨️ 快捷键说明

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