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

📄 client.cpp

📁 Visual C++网络通信编程实用案例精选光盘内容6-9章
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			fileLength-=retCode;
			if(fileLength==0)
				break;
		}
	}
	cout<<endl<<"File finished!"<<endl;
	file.close();
	closesocket(sock);
	return 0;
}

/**********************************************************************************/
u_long ConvertHostnameToLongHostAddress(const char * destAddress)
{
	hostent * host=gethostbyname((const char *)destAddress);
	if(host==NULL)
	{
		cout<<"Cann't resolve this host name: "<<GetLastError()<<endl;
		return -1;
	}
	return ntohl((*(struct in_addr*)host->h_addr).S_un.S_addr);
}

int Socks5StartIPv4(const string& proxyServerHostName,
				u_int proxyServerPort,
				const string& username, 
				const string& pass,
				u_long destAddress,
				u_int destPort,
				SOCKET& sock)
{
	sockaddr_in serverAddr;
	int retCode;
	char buffer[BUFFER_BLOCK_SIZE];
	BYTE version=5;
	BYTE authorWay1=0;	//no authority
	BYTE authorWay2=2;	//user/pass authority
	int rsv=0;
	string addrStr;
	u_int t;
	
	sock=socket(AF_INET, SOCK_STREAM,0);
	serverAddr.sin_addr.S_un.S_addr=htonl(ConvertHostnameToLongHostAddress(proxyServerHostName.c_str()));
	serverAddr.sin_family=AF_INET;
	serverAddr.sin_port=htons(proxyServerPort);
	cout<<"Connecting to "<<inet_ntoa(serverAddr.sin_addr)<<"\tPort:"<<proxyServerPort<<endl;
	cout.flush();
	if(connect(sock,(const sockaddr*)&serverAddr,sizeof(sockaddr))==SOCKET_ERROR)
	{
		cout<<"Cannot connect to Server: "<<proxyServerHostName.c_str()<<endl;
		cout.flush();
		closesocket(sock);
		return -1;
	}

	//1. authoutication negotiation
	sprintf(buffer,"%c%c%c%c",version,2,authorWay1,authorWay2);
	send(sock,buffer,4,0);
	retCode=recv(sock,buffer,BUFFER_BLOCK_SIZE,0);
	DEAL_SOCK_ERROR(retCode,sock);
	if(char(retCode)==0xFF)
		return 1;
	buffer[retCode]='\0';
	cout<<buffer;

	//user/pass authentication
	if(buffer[1]==authorWay2)
	{
		//user name
		sprintf(buffer,"%c%c%s%c%s",char(0x01),char(username.length()),username.c_str(),
				char(pass.length()),pass.c_str());
		t=3+username.length()+pass.length();
		send(sock, buffer, t, 0);
		retCode=recv(sock,buffer,BUFFER_BLOCK_SIZE,0);
		DEAL_SOCK_ERROR(retCode,sock);
		if(char(retCode)==0xFF)
			return 1;
		buffer[retCode]='\0';
		if(buffer[1]!=0)
			return 1;
	}
	
	//1. authoutication negotiation
	addrStr=buffer;
	t=htons(destPort);
	sprintf(buffer,"%c%c%c%c%c%c%c%c%c%c",version,char(0x01),rsv,char(0x01),
		((char*)&destAddress)[3],((char*)&destAddress)[2],((char*)&destAddress)[1],((char*)&destAddress)[0],
		((char *)&t)[0],((char *)&t)[1]);
	cout<<buffer;
	send(sock,buffer,10,0);
	retCode=recv(sock,buffer,BUFFER_BLOCK_SIZE,0);
	DEAL_SOCK_ERROR(retCode,sock);
	buffer[retCode]='\0';
	if(buffer[1]!=0)
	{
		sprintf(buffer,"%x",buffer[1]);
		cout<<"Failure in negotiation: "<<buffer<<endl;
		return 1;
	}
	return 0;
}

//"CONNECT " + destaddr + ":" + destport + " HTTP/1.1" + "\r\n" + "Host: " + destAddr + ":" + destPort + "\r\n\r\n"
int HttpProxyStart(const string& proxyServerHostName,
				u_int proxyServerPort,  
				const string& username, 
				const string& pass,
				u_long destAddress,
				u_int destPort,
				SOCKET& sock)
{
	sockaddr_in serverAddr;
	int retCode;
	char buffer[BUFFER_BLOCK_SIZE];
	string addrStr;
	char destAddrStr[BUFFER_BLOCK_SIZE];

	sock=socket(AF_INET, SOCK_STREAM,0);
	serverAddr.sin_addr.S_un.S_addr=htonl(ConvertHostnameToLongHostAddress(proxyServerHostName.c_str()));
	serverAddr.sin_family=AF_INET;
	serverAddr.sin_port=htons(proxyServerPort);
	cout<<"Connecting to "<<inet_ntoa(serverAddr.sin_addr)<<"\tPort:"<<proxyServerPort<<endl;
	cout.flush();
	if(connect(sock,(const sockaddr*)&serverAddr,sizeof(sockaddr))==SOCKET_ERROR)
	{
		cout<<"Cannot connect to Server: "<<proxyServerHostName.c_str()<<endl;
		cout.flush();
		closesocket(sock);
		return -1;
	}

	//"CONNECT " + destaddr + ":" + destport + " HTTP/1.1" + "\r\n" + "Host: " + destAddr + ":" + destPort + "\r\n\r\n"
	serverAddr.sin_addr.S_un.S_addr=htonl(destAddress);
	strcpy(destAddrStr,inet_ntoa(serverAddr.sin_addr));
	sprintf(buffer,"CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n",destAddrStr,destPort,destAddrStr,destPort);
	if(username.length())
	{
		//Encode username, pass using base64
		//Proxy-Authorization: Basic <base64-stuff> <CRLF>
		char t1[BUFFER_BLOCK_SIZE],t2[BUFFER_BLOCK_SIZE];
		EncodingBase64(username.c_str(),t1);
		EncodingBase64(pass.c_str(),t2);
		strcat(buffer,"Proxy-Authorization: Basic ");
		strcat(buffer,t1);
		strcat(buffer,":");
		strcat(buffer,t2);
		strcat(buffer,"\r\n");
	}
	strcat(buffer,"\r\n");
	send(sock,buffer,strlen(buffer),0);
	retCode=recv(sock,buffer,BUFFER_BLOCK_SIZE,0);
	DEAL_SOCK_ERROR(retCode,sock);
	buffer[retCode]='\0';
	sscanf(buffer,"%s%d",destAddrStr,&retCode);
	if(retCode/100!=2)
	{
		cout<<"Failure in negotiation: "<<buffer<<endl;
		return 1;
	}
	return 0;
}


void GivenUniqueFileName(const string& dir,
					const string& subject,
					string& name,
					string postfix)
{
	//if subject is NULL,
	WIN32_FIND_DATA FileData;
	HANDLE hSearch;
	char buffer[BUFFER_BLOCK_SIZE];
	int count=1;
	string curdir=".\\";
	string title=subject;
	RenameFileName(title);
	sprintf(buffer,"%s",postfix.c_str());
	while(1)
	{
		name=curdir+dir+"\\"+title+buffer;
		hSearch = FindFirstFile(name.c_str(), &FileData);
		if (hSearch == INVALID_HANDLE_VALUE)
			break;
		if(FileData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
		{
			hSearch = FindFirstFile(name.c_str(), &FileData);
			if (hSearch == INVALID_HANDLE_VALUE)
				break;
		}
		sprintf(buffer,"(%d)%s",count++,postfix.c_str());
	}
	FindClose(hSearch);
}

void SplitURLToHostAndSubURL(const string& url, string& hostname, string& suburl)
{
	string::size_type pos=0;
	string str=url;
	//filter blank space
	while(str.find(' ',0)!=string::npos)
		str.replace(0,1,"");
	//filter http://
	if(str.find("http://",0)!=string::npos)
		str=str.substr(7,str.length()-7);
	//filter hostname and suburl
	if((pos=str.find('/',0))==string::npos)
	{
		hostname=str;
		suburl="/";
		return;
	}
	hostname=str.substr(0,pos);
	suburl=str.substr(pos,str.length()-pos);
}

void SplitURLToNameAndPostfix(string& src, string& name, string& postfix)
{
	string::size_type pos=0,apos=0;
	if(src[0]=='\'' && src[src.length()-1]=='\'' || src[0]=='"' && src[src.length()-1]=='"')
		src=string(src).substr(1,src.length()-2);
	if((pos=src.find_last_of('.',src.length()))==string::npos)
		pos=src.length();
	postfix=src.substr(pos+1,src.length()-pos);
	if((apos=src.find_last_of('/',src.length()))==string::npos)
		name=src.substr(0,pos);
	else
		name=src.substr(apos+1,src.length()-apos-postfix.length()-2);
}

BOOL FilterSourceImageURL(string& body, string& url)
{
	string::size_type pos,endpos;
	string str;
	while(1)
	{
		if((pos=body.find("<img ", 0))==string::npos)
			break;
		if((endpos=body.find(">", pos))==string::npos)
			break;
		url=body.substr(pos,endpos-pos+1);
		body=body.substr(endpos+1,body.length()-endpos);
		pos=0;
		endpos=0;
		if(FilterStartTag(url, "<img")==FALSE)
			continue;
		if((pos=url.find("src",0))==string::npos)
			continue;
		url=url.substr(pos,url.length()-pos);
		if(FilterStartTag(url, "src")==FALSE)
			continue;
		if(FilterStartTag(url, "=")==FALSE)
			continue;
		FilterStartTag(url, "\"");
		GetStartString(url,str);
		url=str;
		return TRUE;
	}
	return FALSE;
}

void GetStartString(string& str, string& startStr)
{
	//front blank space
	string::size_type pos=0;
	while(str.find(' ',0)==0)
		str.replace(0,1,"");
	if((pos=str.find('"',0))!=string::npos)
	{
		startStr=str.substr(0,pos);
		str=str.substr(pos+1,str.length());
	}
	else
	{
		pos=str.find(' ',0);
		if(pos==string::npos)
			pos=str.find('>',0);
		assert(pos!=string::npos);
		if(pos==string::npos)
		{
			startStr=str;
		}
		else
		{
			startStr=str.substr(0,pos);
			str=str.substr(pos+1,str.length()-pos);
		}
	}
}

BOOL FilterStartTag(string& str, const string& tag)
{
	//front blank space
	while(str.find(' ',0)==0)
		str.replace(0,1,"");
	if(str.find(tag,0)!=0)
		return FALSE;
	else
	{
		str=str.substr(tag.length(),str.length()-tag.length());
		while(str.find(' ',0)==0)
			str.replace(0,1,"");
		return TRUE;
	}
}

void RenameFileName(string& filename)
{
	char illegalChar[]="\\/:*?\"<>|";
	int i=0;
	string::size_type pos=0;
	while((pos=filename.find_first_of(illegalChar, pos))!=string::npos)
	{
		filename.replace(pos,1,"_");
	}
}

void EncodingBase64(const char* src, char* des)
{
	assert(src&&des);
	char sixbit_encoding[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	while(*src!=NULL)
	{
		*des++=sixbit_encoding[(UCHAR)*src++>>2]; //6
		if(*src==NULL)
		{
			*des++=sixbit_encoding[(*(src-1)&0x3)<<4];//2+4
			*des++='=';
			*des++='=';
			break;
		}
		else
		{
			*des++=sixbit_encoding[(*(src-1)&0x3)<<4 | UCHAR(*src)>>4];//2+4
			src++;
		}
		if(*src==NULL)
		{
			*des++=sixbit_encoding[(*(src-1)&0xF)<<2];//4+2
			*des++='=';
		}
		else
		{
			*des++=sixbit_encoding[(*(src-1)&0xF)<<2 | UCHAR(*src)>>6];//4+2
			*des++=sixbit_encoding[*src&0x3F];//6
			src++;
		}
	}
	*des=NULL;
}

⌨️ 快捷键说明

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