首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

穿透代理服务器编程(2)

穿透代理服务器编程(2)

3,如果需要用户验证,则类似:  
BOOL   CCommunicator::SendUserTest()  
{  
int   usernamelen=0;  
int   userpasslen=0;  
usernamelen=m_strTestUserName.GetLength();  
userpasslen=m_strTestUserPass.GetLength();  
char   tempbuf[100];  
tempbuf[0]=5;  
tempbuf[1]=usernamelen;//标示后面所根的字接数  
strcpy(&tempbuf[2],m_strTestUserName);  
tempbuf[2+usernamelen]=userpasslen;  
strcpy((char*)&tempbuf   [3+usernamelen],m_strTestUserPass);  
int   senddatalen;
int   len;  
len=usernamelen+userpasslen+3;  
senddatalen=send(m_sock,(char*)tempbuf,len,0);
}   如果失败,断开建立的tcp联接,   如果用户返回成功,步骤4.
4,发送请求的协议类似:  
void   CCommunicator::SendRequestUDP()  
{  
int   const   datasize=10;  
BYTE   tempbuf[datasize];   tempbuf[0]=5;  
tempbuf[1]=3;//标示UDP连接  
tempbuf[2]=0;  
tempbuf[3]=1;  
tempbuf[4]=0;  
tempbuf[5]=0;  
tempbuf[6]=0;  
tempbuf[7]=0;  
*((SHORT*)(&(tempbuf[8])))=m_uBindUDPPort;   //UDP在客户端绑定的端口,就是你本地机器的做udp数据传送的端口调用  
                                                                                      //socket函数后,再调用bind()来邦定一个端口。  
char   temp;  
temp=tempbuf[8];  
tempbuf[8]=tempbuf[9];  
tempbuf[9]=temp;  
int   senddatalen=send(m_sock,(char*)tempbuf,datasize,0);  
}
如果失败,断开建立的tcp联接,如果返回成功,验证完毕!步骤5  
5,真正的数据传送,用代理传送的时候,数据包的前面加上10个字节类似:  
void   CCommunicator::CopyDataHead(BYTE   *   ptempbuf)  
{  
struct   in_addr   addr;  
addr.s_addr=inet_addr(“202.220.33.333”);//这个ip是服务器端的ip  
ptempbuf[0]=0;  
ptempbuf[1]=0;  
ptempbuf[2]=0;  
ptempbuf[3]=1;  
ptempbuf[4]=(char)addr.S_un.S_un_b.s_b1;
ptempbuf[5]=(char)addr.S_un.S_un_b.s_b2;  
ptempbuf[6]=(char)addr.S_un.S_un_b.s_b3;
ptempbuf[7]=(char)addr.S_un.S_un_b.s_b4;  
*((SHORT*)(&(ptempbuf[8])))=m_uServerUDPPort;//服务器的端口,就是你最终要发到那个服务器的端口,也就是你的qq服务器。  
char   temp;  
temp=ptempbuf[8];  
ptempbuf[8]=ptempbuf[9];  
ptempbuf[9]=temp;  
}  
真正发送的时候类似:
int   CCommunicator::SendBufferUDP(LPBYTE   lpBuf,int   nLen)  
{  
BYTE   tempbuf[1000];  
int   iHeadData=0;  
struct   sockaddr_in   her;  
her.sin_family=AF_INET;  
her.sin_addr.s_addr=inet_addr(m_szProxyAddr);//代理服务器  
her.sin_port=htons(m_uSocksPort);//发送请求的时候返回的代理服务器端的端口,记住,这是最重要的。  
CopyDataHead(tempbuf);  
iHeadData=10;  
nLen=nLen+10;  
int   addr_len;  
addr_len=sizeof(struct   sockaddr);  
CopyMemory((char*)&tempbuf[iHeadData],lpBuf,nLen);  
int   returndatalen=sendto(m_socket,(char   *)tempbuf,nLen,0,(struct   sockaddr   *)&her,addr_len);  
}
继承事业,薪火相传
返回列表