官方网址: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. wordpress默认index主页选择Your Projects提示无法找到项目解决办法?

    wordpress_4.5.3默认index主页选择Your Projects下部署的项目发现报错无法找到目标解决办法: 1.其实细心的小伙伴已经发现问题出在哪里,跳转后链接地址发生了错误没有加loc ...

  2. django 基础篇

    jdango 简介: 一个可以使Web开发工作愉快并且高效的Web开发框架. 使用Django,使你能够以 小的代价构建和维护高质量的Web应用. Python的WEB框架有Django.Tornad ...

  3. PL/SQL循环

    1.if循环做判断 SET SERVEROUTPUT ON accept num prompt 'qinshuu'; DECLARE pnum NUMBER :=& num ; BEGIN T ...

  4. zookeeper原理解析-序列化

    1)底层通信数据封装与操作           BinaryInputArchive& BinaryOutputArchive底层通信数据封装与操作     BinaryInputArchiv ...

  5. ajax-登陆+验证码

    登陆用户名和密码判断+验证码验证 省略dao层和service层 1.生成验证码的number.jsp <%@ page contentType="image/jpeg" l ...

  6. Redis之个人简单理解

    1.什么是redis? 在过去的几年中,NoSQL数据库一度成为高并发.海量数据存储解决方案的代名词,与之相应的产品也呈现出雨后春笋般的生机.然而在众多产品中能够脱颖而出的却屈指可数,如Redis.M ...

  7. 将list集合的元素按照添加顺序的倒序进行排列取出

    1.方法 Collections.reverse(list); 2.代码示例 /** * 从redis中将现场状态的记录全部取出 * @param aucId * @return */ @Reques ...

  8. web应用中浏览器与服务端的编码和解码

    转自:http://blog.sina.com.cn/s/blog_87cb63e50102w2b6.html 以下为正文: ************************************* ...

  9. jmeter分布式操作-远程启动功能探索

    一.背景: 之前在Jmeter插件监控服务器性能一篇中说到,在非GUI环境中监控时为了保存监控数据需要修改jmeter脚本,并且每次通过施压机(远程服务器,非GUI环境)来压测时都要将jmeter脚本 ...

  10. svn 图标不显示

    1.判断注册表里面是否有 SVN图标信息 方法:输入:win+R,输入regedit,调出注册表信息,按下Ctrl+F,在注册表里搜索"ShellIconOverlayIdentifiers ...