91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

論壇上經常有對P2P原理的討論,但是討論歸討論,很少有實質的東西產生(源代碼)。呵呵,在這里我就用自己實現的一個源代碼來說明UDP穿越NAT的原理。

資源截圖

代碼片段和文件信息

/*?P2P?程序客戶端
?*?
?*?文件名:P2PClient.c
?*
?*?日期:2004-5-21
?*
?*?作者:shootingstars(zhouhuis22@sina.com)
?*
?*/

#pragma?comment(lib“ws2_32.lib“)

#include?“windows.h“
#include?“..\proto.h“
#include?“..\Exception.h“
#include?
using?namespace?std;

UserList?ClientList;



#define?COMMANDMAXC?256
#define?MAXRETRY????5

SOCKET?PrimaryUDP;
char?UserName[10];
char?ServerIP[20];

bool?RecvedACK;

void?InitWinSock()
{
WSADATA?wsaData;

if?(WSAStartup(MAKEWORD(2?2)?&wsaData)?!=?0)
{
printf(“Windows?sockets?2.2?startup“);
throw?Exception(““);
}
else{
printf(“Using?%s?(Status:?%s)\n“
wsaData.szDescription?wsaData.szSystemStatus);
printf(“with?API?versions?%d.%d?to?%d.%d\n\n“
LOBYTE(wsaData.wVersion)?HIBYTE(wsaData.wVersion)
LOBYTE(wsaData.wHighVersion)?HIBYTE(wsaData.wHighVersion));
}
}

SOCKET?mksock(int?type)
{
SOCKET?sock?=?socket(AF_INET?type?0);
if?(sock? {
????????printf(“create?socket?error“);
throw?Exception(““);
}
return?sock;
}

stUserListNode?GetUser(char?*username)
{
for(UserList::iterator?UserIterator=ClientList.begin();
UserIterator!=ClientList.end();
++UserIterator)
{
if(?strcmp(?((*UserIterator)->userName)?username)?==?0?)
return?*(*UserIterator);
}
throw?Exception(“not?find?this?user“);
}

void?BindSock(SOCKET?sock)
{
sockaddr_in?sin;
sin.sin_addr.S_un.S_addr?=?INADDR_ANY;
sin.sin_family?=?AF_INET;
sin.sin_port?=?0;

if?(bind(sock?(struct?sockaddr*)&sin?sizeof(sin))? throw?Exception(“bind?error“);
}

void?ConnectToServer(SOCKET?sockchar?*username?char?*serverip)
{
sockaddr_in?remote;
remote.sin_addr.S_un.S_addr?=?inet_addr(serverip);
remote.sin_family?=?AF_INET;
remote.sin_port?=?htons(SERVER_PORT);

stMessage?sendbuf;
sendbuf.iMessageType?=?LOGIN;
strncpy(sendbuf.message.loginmember.userName?username?10);

sendto(sock?(const?char*)&sendbuf?sizeof(sendbuf)?0?(const?sockaddr*)&remotesizeof(remote));

int?usercount;
int?fromlen?=?sizeof(remote);
int?iread?=?recvfrom(sock?(char?*)&usercount?sizeof(int)?0?(sockaddr?*)&remote?&fromlen);
if(iread<=0)
{
throw?Exception(“Login?error\n“);
}

//?登錄到服務端后,接收服務端發來的已經登錄的用戶的信息
cout<<“Have?“< for(int?i?=?0;i {
stUserListNode?*node?=?new?stUserListNode;
recvfrom(sock?(char*)node?sizeof(stUserListNode)?0?(sockaddr?*)&remote?&fromlen);
ClientList.push_back(node);
cout<<“Username:“<userName< in_addr?tmp;
tmp.S_un.S_addr?=?htonl(node->ip);
cout<<“UserIP:“< cout<<“UserPort:“<port< cout<<““< }
}

void?OutputUsage()
{
cout<<“You?can?input?you?command:\n“
<<“Command?Type:\“send\“\“exit\“\“getu\“\n“
<<“Example?:?send?Username?Message\n“
<<“??????????exit\n“
<<“??????????getu\n“
<}

/*?這是主要的函數:發送一個消息給某個用戶(C)
?*流程:直接

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----

?????文件????????498??2004-05-21?21:58??P2P_by_shootingstars\P2P\Exception.h

?????文件???????9841??2004-05-21?22:54??P2P_by_shootingstars\P2P\P2PClient\P2PClient.cpp

?????文件???????4324??2004-05-21?22:55??P2P_by_shootingstars\P2P\P2PClient\P2PClient.dsp

?????文件????????541??2004-05-21?21:55??P2P_by_shootingstars\P2P\P2PClient\P2PClient.dsw

?????文件??????19456??2004-05-22?09:24??P2P_by_shootingstars\P2P\P2PClient\P2PClient.ncb

?????文件??????48640??2004-05-21?22:55??P2P_by_shootingstars\P2P\P2PClient\P2PClient.opt

?????文件????????252??2004-05-21?22:55??P2P_by_shootingstars\P2P\P2PClient\P2PClient.plg

?????文件????????907??2004-05-22?08:47??P2P_by_shootingstars\P2P\P2PClient\P2PClient.sln

????..A..H.??????8192??2004-05-22?09:24??P2P_by_shootingstars\P2P\P2PClient\P2PClient.suo

?????文件???????4361??2004-05-22?08:47??P2P_by_shootingstars\P2P\P2PClient\P2PClient.vcproj

?????目錄??????????0??2004-05-22?11:15??P2P_by_shootingstars\P2P\P2PClient

?????文件???????5364??2004-05-21?22:48??P2P_by_shootingstars\P2P\P2PServer\P2PServer.cpp

?????文件???????4320??2004-05-21?22:55??P2P_by_shootingstars\P2P\P2PServer\P2PServer.dsp

?????文件????????541??2004-05-21?21:51??P2P_by_shootingstars\P2P\P2PServer\P2PServer.dsw

?????文件??????19456??2004-05-22?09:24??P2P_by_shootingstars\P2P\P2PServer\P2PServer.ncb

?????文件??????48640??2004-05-21?22:55??P2P_by_shootingstars\P2P\P2PServer\P2PServer.opt

?????文件????????252??2004-05-21?22:55??P2P_by_shootingstars\P2P\P2PServer\P2PServer.plg

?????文件????????907??2004-05-22?08:47??P2P_by_shootingstars\P2P\P2PServer\P2PServer.sln

????..A..H.??????8192??2004-05-22?09:24??P2P_by_shootingstars\P2P\P2PServer\P2PServer.suo

?????文件???????4305??2004-05-22?08:47??P2P_by_shootingstars\P2P\P2PServer\P2PServer.vcproj

?????目錄??????????0??2004-05-22?10:57??P2P_by_shootingstars\P2P\P2PServer

?????文件???????1725??2004-05-21?22:35??P2P_by_shootingstars\P2P\proto.h

?????文件?????????93??2004-05-25?09:35??P2P_by_shootingstars\P2P\readme.txt

?????文件???????8733??2004-05-22?11:16??P2P_by_shootingstars\P2P\UDP穿越NAT.TXT

?????目錄??????????0??2004-05-25?09:34??P2P_by_shootingstars\P2P

?????目錄??????????0??2004-05-25?09:32??P2P_by_shootingstars

-----------?---------??----------?-----??----

???????????????199540????????????????????26


評論

共有 條評論