您的位置:首页 > 其它

RakNet 客户端与服务器 启动 区别之 RakNet::SocketDescriptor()

2013-12-16 00:10 387 查看
的确是用来绑定IP和端口号的,测试过程如下:

本机ip:172.16.30.47

朋友ip:172.16.30.38

(1) 客户端和服务端都在本机,客户端绑定了本机ip和端口,结果收不到数据。

客户端设置:

m_pRakpeerInterface->Startup(1,&RakNet::SocketDescriptor(SERVER_PORT,"172.16.30.47"),1);服务端设置
m_pRakpeerInterface->Startup(MAX_CLIENTS,&RakNet::SocketDescriptor(SERVER_PORT,0),1);

原因:端口被占用

(2) 客户端和服务端都在本机,客户端绑定了本机ip、但是没有绑定端口,一切ok

客户端设置:

m_pRakpeerInterface->Startup(1,&RakNet::SocketDescriptor(0,"172.16.30.47"),1);服务端设置:
m_pRakpeerInterface->Startup(MAX_CLIENTS,&RakNet::SocketDescriptor(SERVER_PORT,0),1);

(3) 服务端在本机(47),客户端在朋友电脑(38)。客户端绑定我的ip(47)
客户端设置:

m_pRakpeerInterface->Startup(1,&RakNet::SocketDescriptor(0,"172.16.30.47"),1);

服务端设置:
m_pRakpeerInterface->Startup(MAX_CLIENTS,&RakNet::SocketDescriptor(SERVER_PORT,0),1);

服务端收不到数据,因为绑定的ip与本机ip不符合

(4) 服务端在本机(47),客户端在朋友电脑(38)。客户端绑定朋友的ip(38)

客户端设置:

m_pRakpeerInterface->Startup(1,&RakNet::SocketDescriptor(0,"172.16.30.38"),1);服务端设置:
m_pRakpeerInterface->Startup(MAX_CLIENTS,&RakNet::SocketDescriptor(SERVER_PORT,0),1);

一切ok。
-----------------------------------------------------------------------------------------------------------------------

初步估计是用来绑定ip和端口号的,明天再看。。。。

----------------------------------------------------------------------------------------------------------------------------

刚才测试的时候,服务器收不到数据。

和例子代码对比看,发现 客户端使用了服务器的一个参数

m_pRakpeerInterface->Startup(MAX_CLIENTS,&RakNet::SocketDescriptor(SERVER_PORT,0),1);


例子代码中,Startup第二个参数是

m_pRakpeerInterface->Startup(1,&RakNet::SocketDescriptor(),1);


至于为什么……我也不知道。。求各位路过大婶解答。

下面是客户端和服务器的启动代码。

bool clientStart(std::string ipAddrStr,std::string passwdStr)
{
//start raknet network connect
if (m_pRakpeerInterface==nullptr)
{
std::cout<<"(client) m_pRakpeerInterface is null"<<std::endl;;
}

m_pRakpeerInterface->Startup(1,&RakNet::SocketDescriptor(),1);

RakNet::ConnectionAttemptResult res= m_pRakpeerInterface->Connect(ipAddrStr.c_str(), SERVER_PORT, passwdStr.c_str(), passwdStr.length());

m_targetAddress=RakNet::SystemAddress(ipAddrStr.c_str(),SERVER_PORT);

return res==RakNet::CONNECTION_ATTEMPT_STARTED;
}

void serverStart(std::string passwdStr)
{
//start raknet network connect
if (m_pRakpeerInterface==nullptr)
{
std::cout<<"(server) m_pRakpeerInterface is null"<<std::endl;;
}

m_pRakpeerInterface->SetIncomingPassword(passwdStr.c_str(),passwdStr.length());

//start
m_pRakpeerInterface->Startup(MAX_CLIENTS,&RakNet::SocketDescriptor(SERVER_PORT,0),1);
m_pRakpeerInterface->SetMaximumIncomingConnections(MAX_CLIENTS);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: