RakNet基本教程

官方网址:http://www.jenkinssoftware.com/raknet/manual/tutorialsample3.html

Tutorial code sample 3
Remote procedure call setup. 

The target of this exercise was to add the following features to sample 2:
  1. When the client successfully connects, send a custom user message.
  2. The first byte of custom messages should always start after the enumeration ID_USER_PACKET_ENUM, defined in MessageIdentifiers.h. Otherwise, RakNet would confuse its own identifiers with your game identifiers.
  3. Read the custom message in our packet processing loop. The native RakString type can read and write to the BitStream class. You would not be able to do this using std::string.
New code over sample 2 is in bold.

#include <stdio.h>
#include <string.h>
#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include "RakNetTypes.h"  // MessageID

#define MAX_CLIENTS 10
#define SERVER_PORT 60000

enum GameMessages
{
	ID_GAME_MESSAGE_1=ID_USER_PACKET_ENUM+1
};

int main(void)
{
	char str[512];

	RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
	bool isServer;
	RakNet::Packet *packet;

	printf("(C) or (S)erver?\n");
	gets(str);

	if ((str[0]=='c')||(str[0]=='C'))
	{
		RakNet::SocketDescriptor sd;
		peer->Startup(1,&sd, 1);
		isServer = false;
	} else {
		RakNet::SocketDescriptor sd(SERVER_PORT,0);
		peer->Startup(MAX_CLIENTS, &sd, 1);
		isServer = true;
	}

	if (isServer)
	{
		printf("Starting the server.\n");
		// We need to let the server accept incoming connections from the clients
		peer->SetMaximumIncomingConnections(MAX_CLIENTS);
	} else {
		printf("Enter server IP or hit enter for 127.0.0.1\n");
		gets(str);
		if (str[0]==0){
			strcpy(str, "127.0.0.1");
		}
		printf("Starting the client.\n");
		peer->Connect(str, SERVER_PORT, 0,0);

	}

	while (1)
	{
		for (packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive())
		{
			switch (packet->data[0])
			{
			case ID_REMOTE_DISCONNECTION_NOTIFICATION:
				printf("Another client has disconnected.\n");
				break;
			case ID_REMOTE_CONNECTION_LOST:
				printf("Another client has lost the connection.\n");
				break;
			case ID_REMOTE_NEW_INCOMING_CONNECTION:
				printf("Another client has connected.\n");
				break;
			case ID_CONNECTION_REQUEST_ACCEPTED:
				{
					printf("Our connection request has been accepted.\n");

					// Use a BitStream to write a custom user message
					// Bitstreams are easier to use than sending casted structures, and handle endian swapping automatically
					RakNet::BitStream bsOut;
					bsOut.Write((RakNet::MessageID)ID_GAME_MESSAGE_1);
					bsOut.Write("Hello world");
					peer->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,packet->systemAddress,false);
				}
				break;
			case ID_NEW_INCOMING_CONNECTION:
				printf("A connection is incoming.\n");
				break;
			case ID_NO_FREE_INCOMING_CONNECTIONS:
				printf("The server is full.\n");
				break;
			case ID_DISCONNECTION_NOTIFICATION:
				if (isServer){
					printf("A client has disconnected.\n");
				} else {
					printf("We have been disconnected.\n");
				}
				break;
			case ID_CONNECTION_LOST:
				if (isServer){
					printf("A client lost the connection.\n");
				} else {
					printf("Connection lost.\n");
				}
				break;
				
			case ID_GAME_MESSAGE_1:
				{
					RakNet::RakString rs;
					RakNet::BitStream bsIn(packet->data,packet->length,false);
					bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
					bsIn.Read(rs);
					printf("%s\n", rs.C_String());
				}
				break;
			
			default:
				printf("Message with identifier %i has arrived.\n", packet->data[0]);
				break;
			}
		}
	}


	RakNet::RakPeerInterface::DestroyInstance(peer);

	return 0;
}

转载于:https://www.cnblogs.com/coolbear/p/6214258.html

Raknet是一个基于UDP网络传输协议的C++网络库,允许程序员在他们自己的程序中实现高效的网络传输服务。通常情况下用于游戏,但也可以用于其它项目。   Radnet有以下特点:   l 高性能 在同一台计算机上,Raknet可以实现在两个程序之间每秒传输25,000条信息;   l 容易使用 Radnet有在线用户手册,视频教程。每一个函数和类都有详细的讲解,每一个功能都有自己的例程;   l 跨平台,当前Radnet支持Windows, Linux, Macs,可以建立在Visual Studio, GCC, Code: Blocks, DevCPP 和其它平台上;   l 在线技术支持 RakNet有一个活跃的论坛,邮件列表,你只要给他们发信,他们可以在几小时之内回复你。   l 安全的传输 RakNet在你的代码中自动使用SHA1, AES128, SYN,用RSA避免传输受到攻击   l 音频传输 用Speex编码解码,8位的音频只需要每秒500字节传输。   l 远程终端 用RakNet,你能远程管理你的程序,包括程序的设置,密码的管理和日志的管理。   l 目录服务器 目录服务器允许服务器列举他们自己需要的客户端,并与他们连接。   l Autopatcher Autopatcher系统将限制客户端传输到服务端的文件,这样是为了避免一些不合法的用户将一些不合法的文件传输到服务端。   l 对象重载系统   l 网络数据压缩 BitStream类允许压缩矢量,矩阵,四元数和在-1到1之间的实数。   l 远程功能调用   l 强健的通信层 可以保障信息按照不同的信道传输   RakNet支持两种版权,如果你是做免费游戏,RakNet将是免费的。相反,你必须支付一定的费用。   从这里你可以下载到最新的RakNet:   http://www.rakkarsoft.com/raknet/downloads/RakNet.zip   例子:   #include   #include   #include   #include "RakClientInterface.h" //建立客服端所需要的信息,其中包括客服端的建立,连接和数据的发送和接收   #include "RakNetworkFactory.h" //用于管理我们在程序中使用的类,包括类内存分配和类内存的释放   #include "RakServerInterface.h" //用于建立服务器所需用的信息,包括服务器的建立,连接和数据的发送和接收   #include "PacketEnumerations.h" //用于处理网络引擎在运行过程中得到的信息   Packet *packet;//网络传输中用于存储数据的一个数据结构   /////////////////////////////////////////////////////   /*   Struct Packet   {   PlayerID playerId; //表明了包的出处。每一个连接服务器的客户端都将被分配一个唯一的ID号,用于标识自己。   Unsigned long length; //数据长度   Unsigned long bitsize; //比特大小   Char *data; //包中的数据   }   */   /////////////////////////////////////////////////////   int main(void)   {   char str[512];   RakClientInterface *rakClientInterface;   RakServerInterface *rakServerInterface;   printf("(C)客服端 (S)服务器?\n");   gets(str);   if (str[0]=='c')   {   rakClientInterface=RakNetworkFactory::GetRakClientInterface();//初始化一个客户端实例,为它分配内存   rakServerInterface=0;   printf("客服端已经建立。");   }   else   {   rakClientInterface=0;   rakServerInterface=RakNetworkFactory::GetRakServerInterface();//初始化一个服务器实例,为它分配内存   printf("服务器已经建立。");   }   if (rakServerInterface)   {  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值