网上说这个是非常好的网络引擎,具体介绍请百度之
拿来研究了一番,发现资料比较少(除了官网的那些文档),例子也比较少
Step1:
官网下载压缩包(还让填信息发邮件确认..orz...)
解压缩以后,编译RakNet_VS2008.sln生成静态链接库(Static Lib)或者动态链接库(DLL),看各人喜好,
我用的是StaticLib,生成了一个RakNet_VS2008_LibStatic_Debug_Win32.lib
Step2:
建立一个console工程(VS2010下),加入刚才的Lib,以及下载的头文件,注意还要加入ws2_32.lib
否则链接会出错
现在就基本完成了工程的配置了
Step3:
最简单的服务器端程序
头文件
// MyChatServer.h
/************************************************************************
FileName:MyChatServer.h
Author :eliteYang
Mail :elite_yang[at]163.com
Desc :服务器头文件
TODO :后面再进行整理
************************************************************************/
#ifndef __MY_CHAT_SERVER_H__
#define __MY_CHAT_SERVER_H__
#pragma once
#include
#include "RakPeerInterface.h" // RakNet Peer
#include "RakNetTypes.h"
#include "MessageIdentifiers.h" // RakNet自定义消息枚举定义处
#include "BitStream.h" // RakNet消息包的Bit数据流
#define MAX_CLIENTS 10
#define SERVER_PORT 60000
enum ChatMessagesDefine
{// 自定义消息枚举值,消息ID
MSG_CHATWORD = ID_USER_PACKET_ENUM + 1, // 消息ID从RakNet定义的最后一个枚举开始
};
#endif
// MyChatServer.cpp
#include "MyChatServer.h"
#include
#include
#include
int main()
{
//给服务器端创建一个实例
RakNet::RakPeerInterface* pPeer = RakNet::RakPeerInterface::GetInstance();
if ( NULL == pPeer )
{
std::cout << "RakNet::RakPeerInterface::GetInstance() Error!" << std::endl;
return -1;
}
else
{
std::cout << "---------MyChatServer Init Success(C)-----------" << std::endl;
}
RakNet::Packet* pPacket;
std::cout << "Start Server ......" << std::endl;
//启动服务器
pPeer->Startup( MAX_CLIENTS, &RakNet::SocketDescriptor( SERVER_PORT, 0 ), 1 );
//设置最大链接数
pPeer->SetMaximumIncomingConnections( MAX_CLIENTS );
//////////////////////////////////////////////////////////////////////////
while (1)
{
for ( pPacket = pPeer->Receive(); pPacket; pPeer->DeallocatePacket( pPacket ), pPacket = pPeer->Receive() )
{
switch ( pPacket->data[0] )
{
case ID_REMOTE_DISCONNECTION_NOTIFICATION:
std::cout << "Another client has disconnected" << std::endl;
break;
case ID_REMOTE_CONNECTION_LOST:
std::cout << "Another client has lost the connection" << std::endl;
break;
case ID_REMOTE_NEW_INCOMING_CONNECTION:
std::cout << "Another client has connected" << std::endl;
break;
case ID_CONNECTION_REQUEST_ACCEPTED:
{
std::cout << "Our connection request has been accepted" << std::endl;
RakNet::BitStream bsOut;
bsOut.Write( ( RakNet::MessageID )MSG_CHATWORD );
bsOut.Write("Hello world");
pPeer->Send( &bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, pPacket->systemAddress, false );
}
break;
case ID_NEW_INCOMING_CONNECTION:
std::cout << "A connection is incoming" << std::endl;
break;
case ID_NO_FREE_INCOMING_CONNECTIONS:
std::cout << "The server is full" << std::endl;
break;
case ID_DISCONNECTION_NOTIFICATION:
std::cout << "A client has disconnected" << std::endl;
break;
case ID_CONNECTION_LOST:
std::cout << "A client lost the connection" << std::endl;
break;
//用户自定义数据包
case MSG_CHATWORD:
{
RakNet::RakString rs;
RakNet::BitStream bsIn( pPacket->data, pPacket->length, false );
bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
bsIn.Read(rs); //提取字符串
std::istringstream input_str(rs.C_String()); //存入字符串流
double v, h;
input_str >> v >> h; //提取想要的变量
std::cout << v << " " << h << std::endl; //打印结果
}
break;
default:
std::cout << "Message with identifier %i has arrived" << pPacket->data[0] << std::endl;
break;
}
}
}
//////////////////////////////////////////////////////////////////////////
RakNet::RakPeerInterface::DestroyInstance( pPeer );
return 0;
}
然后是一个字符串,我把它放入字符串流istringstream,方便之后提取各种类型(多为浮点)
比如以飞行器的速度高度为例,定义为
double v, h;
从流中提取到变量里面
Step4:
客户端
头文件
#ifndef __MY_CLIENT_H__
#define __MY_CLIENT_H__
#pragma once
#include
#include "RakPeerInterface.h" // RakNet Peer
#include "RakNetTypes.h"
#include "MessageIdentifiers.h" // RakNet自定义消息枚举定义处
#include "BitStream.h" // RakNet消息包的Bit数据流
#define MAX_CLIENTS 10
#define SERVER_PORT 60000
enum ChatMessagesDefine
{// 自定义消息枚举值,消息ID
MSG_CHATWORD = ID_USER_PACKET_ENUM + 1, // 消息ID从RakNet定义的最后一个枚举开始
};
#endif
#include "MyClient.h"
#include
#include
#include
int main(void)
{
double v = 1000.023;
double h = 10023.23;
RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
RakNet::SocketDescriptor sd;
peer->Startup(1,&sd, 1);
//作为客户端连接,连接到主机ip地址和端口号
peer->Connect("192.168.6.1", SERVER_PORT, 0, 0);
float k = 0;
int i=0;
for (i = 0; i < 10; i++)
{
//给主机发送数据
RakNet::BitStream bsOut;
bsOut.Write((RakNet::MessageID)MSG_CHATWORD);
std::ostringstream format_messege;
format_messege << v++ << " " << h++; //输入信息到字符串流,用空格隔开,此处为飞行器速度高度V, h为double型
// bsOut.Write("hello");
bsOut.Write(format_messege.str().c_str());
peer->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,RakNet::UNASSIGNED_SYSTEM_ADDRESS,true);
Sleep(500);
}
//断开连接,这个会给对方发送一个消息,告诉它要断开连接了。
peer->Shutdown(300);
RakNet::RakPeerInterface::DestroyInstance(peer);
return 0;
}
实验结果
分别为客户端发来的v, h变量的数值