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

📄 client.cpp

📁 Visual C++网络通信编程实用案例精选光盘内容6-9章
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//Extract all pictures in a url
//by caucy 2005.12.5
#include "stdafx.h"
#include "Client.h"
#include <winsock2.h>
#include <afxsock.h>
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <VECTOR>
#include <sys/TIMEB.H>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

using namespace std;

#define BUFFER_BLOCK_SIZE 4096
#define DEAL_SOCK_ERROR(retCode,sock)	if((retCode)==SOCKET_ERROR || (retCode)==0)\
										{										\
											cout<<"Sock error: "<<GetLastError()<<endl;\
											closesocket(sock);					\
											return -1;							\
										}
//Auxiliary Functions
int HttpRequest();
u_long ConvertHostnameToLongHostAddress(const char * destAddress);
void RenameFileName(string& filename);
void GivenUniqueFileName(const string& dir,
					const string& subject,
					string& name,
					string postfix);
void SplitURLToHostAndSubURL(const string& url, string& hostname, string& suburl);
void SplitURLToNameAndPostfix(string& src, string& name, string& postfix);

//Agent Application Functions
int GetHTML(const string& proxyServerName,
			 UINT proxyServerPort,
			 const string& proxyUser,
			 const string& proxyPass,
			 const string& serverHostName,	 
			 UINT serverPort,
			 string& url);
int GetImage(const string& proxyServerName,
			 UINT proxyServerPort,
			 const string& proxyUser,
			 const string& proxyPass,
			 const string& serverHostName,	 
			 UINT serverPort,
			 string& url,
			 string& filename);
void GetStartString(string& str, string& startStr);
BOOL FilterStartTag(string& str, const string& tag);
BOOL FilterSourceImageURL(string& body, string& url);
void EncodingBase64(const char* src, char* des);

int Socks5StartIPv4(const string& proxyServerHostName,
				u_int proxyServerPort,
				const string& username, 
				const string& pass,
				u_long destAddress,
				u_int destPort,
				SOCKET& sock);
int HttpProxyStart(const string& proxyServerHostName,
				u_int proxyServerPort,  
				const string& username, 
				const string& pass,
				u_long destAddress,
				u_int destPort,
				SOCKET& sock);

#define INTEREST_IMAGE_TYPE_COUNT 2
char *gInterestImagePostfix[]={"jpg","bmp"};


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	HttpRequest();
	return 0;
}

int HttpRequest()
{
	//URL
	string url;
	string serverHostName;
	string suburl;
	UINT serverPort=80;

	//proxy
	string proxyServerName;
	UINT proxyServerPort;
	string proxyUser;
	string proxyPass;

 	SplitURLToHostAndSubURL(url, serverHostName, suburl);
	if(url.find("http:/",0)==string::npos)
	{
		url=string("http://")+url;
	}
	if(suburl=="/")
	{
		if(url.find_last_of('/',url.length())!=url.length()-1)
			url+="/";
	}
	
	WSADATA wsaData;
	if(WSAStartup(MAKEWORD( 2, 2 ), &wsaData)!=0)
		cerr<<"socket init error"<<endl;

	if(GetHTML(proxyServerName,proxyServerPort,proxyUser,proxyPass,
					serverHostName,serverPort,url)==-1)
		cout<<"Unsuccessfully request."<<endl;
	return 0;
}

int ConnectToServer(const string& proxyServerName, UINT proxyServerPort,
			 const string& proxyUser, const string& proxyPass,
			 const string& serverHostName, UINT serverPort, SOCKET& sock)
{
	sockaddr_in serverAddr;
	hostent * host;

	if(proxyServerName.length()!=0)//using socks5 proxy
	{
		u_long addr=ConvertHostnameToLongHostAddress(serverHostName.c_str());
//		if(Socks5StartIPv4(proxyServerName,proxyServerPort,proxyUser,proxyPass,addr,serverPort,sock)!=0)
		if(HttpProxyStart(proxyServerName,proxyServerPort,proxyUser,proxyPass,addr,serverPort,sock)!=0)
		{
			cerr<<"Proxy server error!"<<endl;
			return -1;
		}
	}
	else
	{
		host=gethostbyname(serverHostName.c_str());
		if(host==NULL)
		{
			cout<<"Cann't resolve this host name: "<<GetLastError()<<endl;
			return -1;
		}

		sock=socket(AF_INET, SOCK_STREAM,0);
		serverAddr.sin_addr=*(struct in_addr*)host->h_addr;
		serverAddr.sin_family=AF_INET;
		serverAddr.sin_port=htons(serverPort);
		cout<<"Connecting to "<<inet_ntoa(serverAddr.sin_addr)<<endl;
		cout.flush();
		if(connect(sock,(const sockaddr*)&serverAddr,sizeof(sockaddr))==SOCKET_ERROR)
		{
			cout<<"Cannot connect to Server: "<<serverHostName.c_str()<<endl;
			cout.flush();
			closesocket(sock);
			return -1;
		}
	}
	return 0;
}

int GetHTML(const string& proxyServerName,
			 UINT proxyServerPort,
			 const string& proxyUser,
			 const string& proxyPass,
			 const string& serverHostName,	 
			 UINT serverPort,
			 string& url)
{
	int retCode;
	SOCKET sock;
	string str;
	string::size_type pos;
	char buffer[BUFFER_BLOCK_SIZE+1];
	char filter[BUFFER_BLOCK_SIZE+1];
	string head,body;
	string filename;
	string imageurl, imagename, imagepostfix;
	char * p;

	if(ConnectToServer(proxyServerName, proxyServerPort,proxyUser,proxyPass,\
			 serverHostName, serverPort,sock)==-1)
		return -1;
	
	//1. GET Command
	sprintf(buffer,"GET %s HTTP/1.1\r\nHost: %s\r\n\r\n",url.c_str(),serverHostName.c_str());

	cout<<buffer;
	cout.flush();
	send(sock,buffer,strlen(buffer),0);
	while(1)
	{
		retCode=recv(sock,buffer,BUFFER_BLOCK_SIZE,0);
		if(retCode==0 || retCode==-1)
			break;
		buffer[retCode]='\0';
		cout<<buffer;
		cout.flush();
		str+=buffer;
	}

	//2. Split head / body information
	pos=0;
	if((pos=str.find("\r\n\r\n",pos))==string::npos)
		goto _FAIL;
	//head
	head=str.substr(0,pos+2);
	pos=0;
	p= new char[head.length()+1];
	strcpy(p,head.c_str());
	_strlwr(p);
	head=p;
	delete[] p;
	//body
	body=str.substr(pos+4,str.length()-(pos+4));
	if(body.length()==0)
		goto _FAIL;
	p= new char[body.length()+1];
	strcpy(p,body.c_str());
	_strlwr(p);
	body=p;
	delete[] p;
	
	//3. Filter request return code
	pos=head.find("\r\n",0);
	strcpy(buffer,head.substr(0,pos).c_str());
	head=string(head).substr(pos+2,head.length()-pos-1);
	if(sscanf(buffer,"%s%d",filter,&retCode)==-1 || retCode/100!=2)
		goto _FAIL;

	//4. Retrive all picture links
	while(FilterSourceImageURL(body,imageurl))
	{
		SplitURLToNameAndPostfix(imageurl,imagename,imagepostfix);
		for(int i=0; i<INTEREST_IMAGE_TYPE_COUNT; i++)
		{
			if(stricmp(imagepostfix.c_str(),gInterestImagePostfix[i])==0)
			{
				//FilterStartTag(imageurl,"http:/");
				string newhostname,suburl;
				if(imageurl.find("http:/",0)!=string::npos)
				{
					SplitURLToHostAndSubURL(imageurl, newhostname, suburl);
				}
				else
				{
					newhostname=serverHostName;
					pos=url.find_last_of('/',url.length());
					suburl=url.substr(0,pos+1);
					suburl=suburl+imageurl;
				}
				GetImage(proxyServerName, proxyServerPort, proxyUser,proxyPass, newhostname, serverPort, suburl,imageurl);
			}
		}
	}
	return 0;
_FAIL:
	return -1;
}

int GetImage(const string& proxyServerName,
			 UINT proxyServerPort,
			 const string& proxyUser,
			 const string& proxyPass,
			 const string& serverHostName,	 
			 UINT serverPort,
			 string& url,
			 string& filename)
{
	int retCode;
	SOCKET sock;
	string::size_type pos;
	char buffer[BUFFER_BLOCK_SIZE+1];
	char filter[BUFFER_BLOCK_SIZE+1];
	string head;
	int contentLength,fileLength;
	int tag;
	ofstream file;
	BOOL bProcessHead=TRUE;
	char *p;
	if(ConnectToServer(proxyServerName, proxyServerPort,proxyUser,proxyPass,\
			 serverHostName, serverPort,sock)==-1)
		return -1;
	GivenUniqueFileName("",string(filename),filename,"");
	file.open(filename.c_str(),ios::binary);
	assert(!file.fail());

	//1. GET Command
	sprintf(buffer,"GET %s HTTP/1.1\r\nHost: %s\r\n\r\n",url.c_str(),serverHostName.c_str());
	cout<<buffer;
	cout.flush();
	send(sock,buffer,strlen(buffer),0);
	contentLength=0;
	while(1)
	{
		retCode=recv(sock,buffer,BUFFER_BLOCK_SIZE,0);
		if(retCode==-1)
		{
			closesocket(sock);
			return -1;
		}
		if(retCode==0)
			break;
		if(bProcessHead)
		{
			buffer[retCode]='\0';
			cout<<buffer;
			cout.flush();
			strcpy(filter,buffer);
			_strlwr(filter);
			head+=filter;
			if((pos=head.find("\r\n\r\n",0))!=string::npos)
			{
				pos=head.find("\r\n",0);
				strcpy(filter,head.substr(0,pos).c_str());
				if(sscanf(string(filter).c_str(),"%s%d",filter,&tag)==-1 || tag/100!=2)
				{
					closesocket(sock);
					return -1;
				}
				strcpy(filter,"\r\ncontent-length:");
				if((p=strstr(head.c_str(),filter))!=NULL)
				{
					p+=strlen(filter);
					strcpy(filter,p);
					if(sscanf(filter,"%d",&fileLength)==-1)
					{
						closesocket(sock);
						return -1;
					}
					cout<<"File length: "<<fileLength<<endl;
					cout.flush();
				}
				bProcessHead=FALSE;
				pos=head.find("\r\n\r\n",0);
				contentLength=pos+4-contentLength;
				file.write(buffer+contentLength,retCode-contentLength);
				fileLength-=(retCode-contentLength);
			}
			else
				contentLength+=retCode;
		}
		else
		{
			cout<<".";
			cout.flush();
			file.write(buffer,retCode);

⌨️ 快捷键说明

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