资源简介
论坛上经常有对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
相关资源
- 使用选择性重传协议实现UDP可靠通信
- 网络(UDP)转串口程序
- 用Socket写的简易FTP服务器和客户端
- socket小工具(可方便建立TCP UDP的soc
- UDP组播的发送和接收程序
- Skype 协议分析(2006版)
- 采用WINSOCK2 编写的TCP/UDP通信程序
- TCP_UDP对结构体加密数据传输
- frp内网穿透工具 v0.33.0
- 欧姆龙以太网通讯TCP/UDP及欧姆龙PLC程
- UDP协议进行P2P打洞
- 易语言P2P聊天源码
- 易语言p2p服务器源码
- 易语言P2P源代码1源码易语言P2P网络电
- 易语言客户端源码易语言服务器源码
- p2p种子在线播放器绿色版
- Sniffer原理及开源代码
- PeerSim chord仿真
- P2P、网贷后台原型
- 计算机网络socket编程实验
- 局域网封杀软件-P2POVER 2.7完美破解.
- chord-0.1实现代码
- UDP P2P打洞服务器
- FLASH局域网免服务器P2P聊天室源码(
- 一个老外写的RUDP的源代码
- udp丢包率统计
- 计算机网络课程设计: 基于P2P的局域
- Socket-UDP整合消息接收,发送
- 基于qt的udp实时录音
- p2p搜索神器绿色无广告
评论
共有 条评论