• 大小: 29KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-20
  • 语言: 其他
  • 标签: P2P  UDP  穿透  NAT  源代码  

资源简介

论坛上经常有对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 < 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“
<}

/* 这是主要的函数:发送一个消息给某个用户(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


评论

共有 条评论