資源簡介
UDP實現P2P技術打洞實例源碼,測試過的,百分百有效
代碼片段和文件信息
/*?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?
#include?
using?namespace?std;
UserList?ClientList;
#define?COMMANDMAXC?256
#define?MAXRETRY????5
SOCKET?PrimaryUDP;
char?UserName[10];
char?ServerIP[20];
bool?RecvedACK;
//初始化SOCKET
void?InitWinSock()
{
WSADATA?wsaData;
if?(WSAStartup(MAKEWORD(2?2)?&wsaData)?!=?0)
{
printf(_T(“初始化Socket?失敗\r\n“));
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?0)
{
????????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))?0)
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“
<
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2010-07-22?16:30??P2P\
?????文件?????????498??2004-05-21?21:58??P2P\Exception.h
?????目錄???????????0??2017-08-25?01:46??P2P\P2PClient\
?????目錄???????????0??2017-08-25?00:45??P2P\P2PClient\Backup\
?????文件?????????907??2004-05-22?08:47??P2P\P2PClient\Backup\P2PClient.sln
?????文件????????9903??2017-08-25?01:46??P2P\P2PClient\P2PClient.cpp
?????文件????????4324??2004-05-21?22:55??P2P\P2PClient\P2PClient.dsp
?????文件?????????541??2004-05-21?21:55??P2P\P2PClient\P2PClient.dsw
?????文件???????33792??2010-07-21?18:38??P2P\P2PClient\P2PClient.ncb
?????文件???????48640??2010-07-21?18:38??P2P\P2PClient\P2PClient.opt
?????文件????????1489??2010-07-21?18:32??P2P\P2PClient\P2PClient.plg
?????文件????????1070??2017-08-25?00:46??P2P\P2PClient\P2PClient.sln
?????文件????????8192??2004-05-22?09:24??P2P\P2PClient\P2PClient.suo
?????文件????????4361??2004-05-22?08:47??P2P\P2PClient\P2PClient.vcproj
?????文件????????6498??2017-08-25?00:49??P2P\P2PClient\P2PClient.vcxproj
?????文件?????????905??2017-08-25?00:46??P2P\P2PClient\P2PClient.vcxproj.filters
?????目錄???????????0??2017-08-25?00:38??P2P\P2PServer\
?????目錄???????????0??2017-08-25?00:38??P2P\P2PServer\Backup\
?????文件?????????907??2004-05-22?08:47??P2P\P2PServer\Backup\P2PServer.sln
?????文件????????5348??2010-07-22?14:06??P2P\P2PServer\P2PServer.cpp
?????文件????????4320??2004-05-21?22:55??P2P\P2PServer\P2PServer.dsp
?????文件?????????541??2004-05-21?21:51??P2P\P2PServer\P2PServer.dsw
?????文件???????50176??2010-07-22?15:22??P2P\P2PServer\P2PServer.ncb
?????文件???????54784??2010-07-22?15:22??P2P\P2PServer\P2PServer.opt
?????文件?????????252??2010-07-22?15:15??P2P\P2PServer\P2PServer.plg
?????文件????????1075??2017-08-25?00:38??P2P\P2PServer\P2PServer.sln
?????文件????????8192??2004-05-22?09:24??P2P\P2PServer\P2PServer.suo
?????文件????????4305??2004-05-22?08:47??P2P\P2PServer\P2PServer.vcproj
?????文件????????6286??2017-08-25?00:38??P2P\P2PServer\P2PServer.vcxproj
?????文件?????????905??2017-08-25?00:38??P2P\P2PServer\P2PServer.vcxproj.filters
?????文件???????72314??2010-07-22?16:29??P2P\Peer-to-Peer?(P2P).txt
............此處省略3個文件信息
- 上一篇:課程設計 可視化計算器 VS2017實現
- 下一篇:馬哥k8s視頻資料
評論
共有 條評論