官方网址: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;
}

RakNet基本教程的更多相关文章

  1. React JS快速入门教程

    翻译至官方文档<Tutorial>http://facebook.github.io/react/docs/tutorial.html 转载请注明出处:http://blog.csdn.n ...

  2. React JS高速新手教程

    翻译至官方文档<Tutorial>http://facebook.github.io/react/docs/tutorial.html 转载请注明出处:http://blog.csdn.n ...

  3. 《Visual C++ 2010入门教程》系列三:关于C++的一些问题

    <Visual C++ 2010入门教程>系列三:关于C++的一些问题   这一回我自己都不知道应该写点什么好,或许今天的篇幅会比往常短很多.我说过,这不是C++的教程,因为我还没有那个能 ...

  4. Hololens官方教程精简版 - 08. Sharing holograms(共享全息影像)

    前言 注意:本文已更新到5.5.1f1版本号 本篇集中学习全息影像"共享"的功能,以实如今同一房间的人,看到"同一个物体".之所以打引號,是由于.每一个人看到的 ...

  5. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  6. Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数

    上一篇:Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数 之前介绍了简单的路由以及传参,这篇文章我们将要学习复杂一些的路由以及传递其他附加参数.一个好的路由系统可以使我们 ...

  7. Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数

    上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...

  8. Angular2入门系列教程4-服务

    上一篇文章 Angular2入门系列教程-多个组件,主从关系 在编程中,我们通常会将数据提供单独分离出来,以免在编写程序的过程中反复复制粘贴数据请求的代码 Angular2中提供了依赖注入的概念,使得 ...

  9. Angular2入门系列教程1-使用Angular-cli搭建Angular2开发环境

    一直在学Angular2,百忙之中抽点时间来写个简单的教程. 2016年是前端飞速发展的一年,前端越来越形成了(web component)组件化的编程模式:以前Jquery通吃一切的田园时代一去不复 ...

随机推荐

  1. html5+css+div随时笔记

    首先给头部文件引用格式 <link href="<%=basePath%>temp/public/css/style.css" rel="stylesh ...

  2. HDU 1233 还是畅通工程(最小生成树)

    传送门 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. PHP 进程详解

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...

  4. My97 DatePicker 日期选择插件.

    My97DatePicker是一个纯js写的时间插件,体积较小,更可以自己扩展皮肤,且样式不和页面相冲突,有多钟配置,近期项目用了一下感觉还不错...分享给大家 软件官网:http://www.my9 ...

  5. mybatis-generator运行命令

    java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml -overwrite

  6. Java学习笔记-按值传递

    参数的值传递 实参必须与方法中次你故意的参数在次序和数量上匹配,在类型上兼容.类型兼容是指不需要经过显式的类型转换,实参的值就可以传递给形参.如将int型的实参值传递给double型形参. 当调用方法 ...

  7. 关于ie版本判断

    //see: http://tanalin.com/en/articles/ie-version-js/ var browser = function(){ var isIE=!!window.Act ...

  8. Excel——使用OFFSET、MATCH、COUNTA实现二级菜单

    如图所示,接下来提供两种办法实现: 1.将A.B.C.D定义为名称NAME. 2.设置一级菜单单元格数据有效性为NAME. 3.设置二级菜单格数据有效为: =OFFSET($A$1,MATCH($A6 ...

  9. windows安装rabbitmq

    官网下载windows安装版本:http://www.rabbitmq.com/install-windows.html ,安装文件rabbitmq-server-3.6.5.exe 前提:安装erl ...

  10. Jquery制作--焦点图淡出淡入

    之前写了一个焦点图左右轮播的,感觉淡出淡入用得也比较多,就干脆一起放上来啦.这个容器用了百分比宽度,图片始终保持居中处理,定宽或者自适应宽度都是可以的. 兼容到IE6+以上浏览器,有淡出淡入速度和切换 ...