socket模拟http版下载者代码分享。本人才疏学浅,抛砖引玉,欢迎大牛指导。

#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#pragma  comment(lib,"WS2_32.lib")
#define  KBYTES  1024
struct sockaddr_in server;
struct hostent *hostinfo;
WSADATA wsaData;
WORD wVersionRequested;
SOCKET LocalSock;
char IpAddr[16];
char buff[100*KBYTES];
FILE *pMyDownFile=NULL;
int nWriteBytes=0;
int FindEntityMsgPos=0;
int MsgInfoSize=0;
char *pHostName="www.xxx.com";

int main(int argc,char *argv[])
{
	memset(IpAddr,0,sizeof(IpAddr));
    memset(buff,0,sizeof(buff)); 

    char *pRequestHeader="GET /xxx.exe HTTP/1.1\r\n"
		"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight, application/vnd.ms-excel\r\n"
		"Accept-Language: zh-cn\r\n"
		"UA-CPU: x86\r\n"
		"Accept-Encoding: gzip, deflate\r\n"
		"User-Agent: Mozilla/12.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)\r\n"
		"Host:www.xxxx.com\r\n"
        "Connection: Keep-Alive\r\n\r\n";
	wVersionRequested = MAKEWORD( 2, 2 );
	if (WSAStartup(wVersionRequested , &wsaData))
	{
		printf("Winsock Initialization failed.\n");
		exit(1);
	}
	if ((LocalSock=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET)
	{
		printf("Can not create socket.\n");	
		exit(1);	
	}

	if ((hostinfo=gethostbyname(pHostName))!=NULL)
	{	
		memcpy(IpAddr,inet_ntoa(*(struct   in_addr   *)*hostinfo-> h_addr_list),sizeof(IpAddr));
	}
	else
	{
			WSACleanup();
			exit(1);
	}
	server.sin_family = AF_INET;
	server.sin_port = htons(80);
	server.sin_addr.s_addr= inet_addr(IpAddr);

	if (connect(LocalSock,(struct sockaddr*)&server,sizeof(server))==0)
	{
		int nSendedByte=send(LocalSock,pRequestHeader,strlen(pRequestHeader),0);
		printf("the sended data's length:%d bytes\n\n",nSendedByte);
		printf("RequestHeader1 :\n%s\n",pRequestHeader);
		int nRecvBytes=recv(LocalSock,buff,sizeof(buff),0);
		printf("the recieved data's length is :%d bytes\n\n",nRecvBytes);
		printf("ResponseHeader1 :\n%s\n",buff);
		int i=0;
		while(true)
		{
			if ((buff[i]=='\r')&&(buff[i+1]=='\n')&&(buff[i+2]=='\r')&&(buff[i+3]=='\n'))
			{
				FindEntityMsgPos=i+4;
				break;
			}
			i++;
		}

		if(pMyDownFile=fopen("C:\\download.html","w+"))
		{
			printf("openfile success\n");
			while(FindEntityMsgPos!=nRecvBytes)
			{
			fputc(buff[FindEntityMsgPos++],pMyDownFile);
			MsgInfoSize++;
			}
			printf("file download success\n");
			printf("total size %d bytes \n",nRecvBytes);
			printf("response header size:%d bytes\n",nRecvBytes-MsgInfoSize);
			printf("request header size :%d bytes\n",MsgInfoSize);
		}
		else
			printf("open file fail\n");
		exit(1);
		
	}
	fclose(pMyDownFile);
	closesocket(LocalSock);
	WSACleanup();
	getchar();
	return(1);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值