#pragma once
#include
// 定义iMessageType的值
#define LOGIN 1
#define LOGOUT 2
#define P2PTRANS 3
#define GETALLUSER 4
// 服务器端口
#define SERVER_PORT 6060
// Client登录时向服务器发送的消息
struct stLoginMessage
{
char userName[10];
char password[10];
};
// Client注销时发送的消息
struct stLogoutMessage
{
char userName[10];
};
// Client向服务器请求另外一个Client(userName)向自己方向发送UDP打洞消息
struct stP2PTranslate
{
char userName[10];
};
// Client向服务器发送的消息格式
struct stMessage
{
int iMessageType;
union _message
{
stLoginMessage loginmember;
stLogoutMessage logoutmember;
stP2PTranslate translatemessage;
}message;
};
// 客户节点信息
struct stUserListNode
{
char userName[10];
unsigned int ip;
unsigned short port;
};
// Server向Client发送的消息
struct stServerToClient
{
int iMessageType;
union _message
{
stUserListNode user;
}message;
};
//======================================
// 下面的协议用于客户端之间的通信
//======================================
#define P2PMESSAGE 100 // 发送消息
#define P2PMESSAGEACK 101 // 收到消息的应答
#define P2PSOMEONEWANTTOCALLYOU 102 // 服务器向客户端发送的消息
// 希望此客户端发送一个UDP打洞包
#define P2PTRASH 103 // 客户端发送的打洞包,接收端应该忽略此消息
// 客户端之间发送消息格式
struct stP2PMessage
{
int iMessageType;
int iStringLen; // or IP address
unsigned short Port;
};
using namespace std;
typedef list UserList;
#ifndef NATCLIENT_H
#define NATCLIENT_H
#pragma comment(lib,"ws2_32.lib")
#include "windows.h"
#include "..\common\msgproto.h"
#include
#include
using namespace std;
#include
#define COMMANDMAXC 25600
#define MAXRETRY 5
class Nat_Client
{
private:
USHORT g_nClientPort ;
USHORT g_nServerPort ;
char UserName[10];
char ServerIP[20];
void command_exit();
void command_send(char * CommandLine);
void command_tell(char * CommandLine);
void command_getu();
bool SendMessageTo(char *UserName, char *Message);
// 接收到P2P的消息
static void command_p2pMsg(sockaddr_in &remote,stP2PMessage &recvbuf,int sinlen);
// 接收到打洞命令,向指定的IP地址打洞
static void command_p2pSomeoneWantToCallYou(sockaddr_in &remote,stP2PMessage &recvbuf,int sinlen);
public:
static UserList ClientList;
static SOCKET PrimaryUDP;
static bool RecvedACK;
Nat_Client();
~Nat_Client();
SOCKET mksock(int type);
void InitWinSock();
stUserListNode GetUser(char *username);
void BindSock(SOCKET sock);
void ConnectToServer(SOCKET sock,char *username, char *serverip);
void OutputUsage();
bool SendMessageTo2(char *UserName, char *Message, const char *pIP, USHORT nPort );
void ParseCommand(char * CommandLine);
static void RecvThreadProc();
void Init();
};
UserList Nat_Client::ClientList ;
bool Nat_Client::RecvedACK;
SOCKET Nat_Client::PrimaryUDP = 0;
Nat_Client::~Nat_Client()
{
command_exit();
Sleep(100);
}
void Nat_Client::command_send(char * CommandLine)
{
char sendname[20];
char message[COMMANDMAXC];
int i;
for(i=5;;i++)
{
if(CommandLine[i]!=' ')
sendname[i-5]=CommandLine[i];
else
{
sendname[i-5]='\0';
break;
}
}
strcpy_s(message,sizeof(message), &(CommandLine[i+1]));
if(SendMessageTo(sendname, message))
printf("Send OK!\n");
else
printf("Send Failure!\n");
}
void Nat_Client::command_tell(char * CommandLine)
{
char sendname[20];
char sendto[ 64 ] = {0};
char message[COMMANDMAXC];
int i;
for(i=5;;i++)
{
if(CommandLine[i]!=' ')
sendname[i-5]=CommandLine[i];
else
{
sendname[i-5]='\0';
break;
}
}
i++;
int nStart = i;
for(;;i++)
{
if(CommandLine[i]!=' ')
sendto[i-nStart]=CommandLine[i];
else
{
sendto[i-nStart]='\0';
break;
}
}
strcpy_s(message,sizeof(message), &(CommandLine[i+1]));
char szIP[32] = {0};
char *p1 = sendto;
char *p2 = szIP;
while ( *p1 != ':' )
{
*p2++ = *p1++;
}
p1++;
USHORT nPort = atoi( p1 );
if(SendMessageTo2(sendname, message, strcmp( szIP, "255.255.255.255" ) ? szIP : NULL, nPort ))
printf("Send OK!\n");
else
printf("Send Failure!\n");
}
void Nat_Client::command_getu()
{
int command = GETALLUSER;
sockaddr_in server;
server.sin_addr.S_un.S_addr = inet_addr(ServerIP);
server.sin_family = AF_INET;
server.sin_port = htons(g_nServerPort);
sendto(Nat_Client::PrimaryUDP,(const char*)&command, sizeof(command), 0, (const sockaddr *)&server, sizeof(server));
}
// 接收到P2P的消息
void Nat_Client::command_p2pMsg(sockaddr_in &remote,stP2PMessage &recvbuf,int sinlen)
{
char *comemessage= new char[recvbuf.iStringLen];
int iread1 = recvfrom(PrimaryUDP, comemessage, 256, 0, (sockaddr *)&remote, &sinlen);
comemessage[iread1-1] = '\0';
if(iread1<=0)
printf("Recv Message Error\n");
else
{
printf("Recv a Message, %s:%ld -> %s\n", inet_ntoa( remote.sin_addr), htons(remote.sin_port), comemessage);
stP2PMessage sendbuf;
sendbuf.iMessageType = P2PMESSAGEACK;
sendto(PrimaryUDP, (const char*)&sendbuf, sizeof(sendbuf), 0, (const sockaddr*)&remote, sizeof(remote));
printf("Send a Message Ack to %s:%ld\n", inet_ntoa( remote.sin_addr), htons(remote.sin_port) );
}
delete []comemessage;
}
void Nat_Client::command_p2pSomeoneWantToCallYou(sockaddr_in &remote,stP2PMessage &recvbuf,int sinlen)
{
// 接收到打洞命令,向指定的IP地址打洞
printf("Recv p2someonewanttocallyou from %s:%ld\n", inet_ntoa( remote.sin_addr), htons(remote.sin_port) );
sockaddr_in _remote;
_remote.sin_addr.S_un.S_addr = htonl(recvbuf.iStringLen);
_remote.sin_family = AF_INET;
_remote.sin_port = htons(recvbuf.Port);
// UDP hole punching
stP2PMessage message;
message.iMessageType = P2PTRASH;
sendto(PrimaryUDP, (const char *)&message, sizeof(message), 0, (const sockaddr*)&_remote, sizeof(_remote));
printf("Send p2ptrash to %s:%ld\n", inet_ntoa( _remote.sin_addr), htons(_remote.sin_port) );
}
void Nat_Client::Init()
{
InitWinSock();
Nat_Client::PrimaryUDP = mksock(SOCK_DGRAM);
BindSock(Nat_Client::PrimaryUDP);
std::cout<<"cin user name"<>UserName;
ConnectToServer(Nat_Client::PrimaryUDP, UserName, ServerIP);
}
void Nat_Client::RecvThreadProc()
{
sockaddr_in remote;
int sinlen = sizeof(remote);
stP2PMessage recvbuf;
for(;;)
{
int iread = recvfrom(PrimaryUDP, (char *)&recvbuf, sizeof(recvbuf), 0, (sockaddr *)&remote, &sinlen);
if(iread<=0)
{
//printf("recv error\n");
continue;
}
switch(recvbuf.iMessageType)
{
case P2PMESSAGE:
{
// 接收到P2P的消息
Nat_Client::command_p2pMsg(remote,recvbuf,sinlen);
break;
}
case P2PSOMEONEWANTTOCALLYOU:
{
Nat_Client::command_p2pSomeoneWantToCallYou(remote,recvbuf,sinlen);
break;
}
case P2PMESSAGEACK:
{
// 发送消息的应答
RecvedACK = true;
printf("Recv message ack from %s:%ld\n", inet_ntoa( remote.sin_addr), htons(remote.sin_port) );
break;
}
case P2PTRASH:
{
// 对方发送的打洞消息,忽略掉。
//do nothing ...
printf("Recv p2ptrash data from %s:%ld\n", inet_ntoa( remote.sin_addr), htons(remote.sin_port) );
break;
}
case GETALLUSER:
{
int usercount;
int fromlen = sizeof(remote);
int iread = recvfrom(PrimaryUDP, (char *)&usercount, sizeof(int), 0, (sockaddr *)&remote, &fromlen);
if(iread<=0)
printf("Login error\n");
ClientList.clear();
cout<<"Have "<userName<ip);
cout<<"UserIP:"<port<userName), UserName) == 0 )
{
UserIP = (*UserIterator)->ip;
UserPort = (*UserIterator)->port;
FindUser = true;
}
}
if(!FindUser)
return false;
}
strcpy_s(realmessage,sizeof(realmessage), Message);
sockaddr_in remote;
remote.sin_addr.S_un.S_addr = htonl(UserIP);
remote.sin_family = AF_INET;
remote.sin_port = htons(UserPort);
stP2PMessage MessageHead;
MessageHead.iMessageType = P2PMESSAGE;
MessageHead.iStringLen = (int)strlen(realmessage)+1;
printf( "Send message, %s:%ld -> %s\n", inet_ntoa( remote.sin_addr ), ntohs( remote.sin_port ), realmessage );
for(int i=0;iuserName), UserName) == 0 )
{
UserIP = (*UserIterator)->ip;
UserPort = (*UserIterator)->port;
FindUser = true;
}
}
if(!FindUser)
return false;
strcpy_s(realmessage,sizeof(realmessage), Message);
for(int i=0;i 0 )
{
if ( FD_ISSET( sock, &readfds ) )
{
int iread = recvfrom(sock, (char *)&usercount, sizeof(int), 0, (sockaddr *)&remote, &fromlen);
if(iread<=0)
printf("Login error\n");
break;
}
}
else if ( n < 0 )
printf("Login error\n");
sendto(sock, (const char*)&sendbuf, sizeof(sendbuf), 0, (const sockaddr*)&remote,sizeof(remote));
}
// 登录到服务端后,接收服务端发来的已经登录的用户的信息
cout<<"Have "<userName<ip);
cout<<"UserIP:"<port<userName), username) == 0 )
return *(*UserIterator);
}
return *(*UserIterator);
}
SOCKET Nat_Client::mksock(int type)
{
SOCKET sock = socket(AF_INET, type, 0);
if (sock < 0)
{
printf("create socket error");
}
return sock;
}
Nat_Client::Nat_Client():g_nClientPort(9896),g_nServerPort(SERVER_PORT)
{
strcpy_s(UserName,"client_");
strcpy_s(ServerIP,"120.25.2.24");
}
void Nat_Client::InitWinSock()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Windows sockets 2.2 startup");
}
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));
}
}
int Client_Run()
{
std::auto_ptr clt(new Nat_Client());
try
{
clt->Init();
std::thread t(&Nat_Client::RecvThreadProc);
clt->OutputUsage();
for(;;)
{
char Command[COMMANDMAXC];
gets_s(Command);
clt->ParseCommand(Command);
}
}
catch(exception &e)
{
printf("some thing is error:%s",e.what());
return 1;
}
}
#endif
#include "NatClient.h"
int main(int argc, char* argv[])
{
Client_Run();
return 0;
}