RakNet简单应用——hello World(服务器端)

我们学习很多编程语言的时候都是从Hello world开始的,所以这次我们也打算开始从hello world写起,不过本文只写了服务器端,客户端后面一篇文章会放出来的,一个人写有点无聊,而且工程配置比较麻烦,一切都得按项目标准来,不能随便写写就好了,为了以后也可以用,就标准些吧。

(不过,还是写的很匆忙,很多东西没写好,后面会整理的)

说起 Hello world就不得不说一个非常经典的笑话了。

某程序员对书法十分感兴趣,退休后决定在这方面有所建树。于是花重金购买了上等的文房四宝。一日,饭后突生雅兴,一番磨墨拟纸,并点上了上好的檀香,颇有王羲之风范,又具颜真卿气势,定神片刻,泼墨挥毫,郑重地写下一行字:hello world

好了,言归正传,写一个简单的hello world的服务端的例子,客户端还没写,不过客户端好写一些,代码没整理,有点乱,各位见谅。

建立一个空的Win32 Console工程,在工程配置中加入RakNet的头文件和Lib文件,上篇中有提到的。

[cpp]
// 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 <iostream>
#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
[/cpp]

[cpp]
// MyChatServer.cpp
#include "MyChatServer.h"

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::cout << "%s"<< rs.C_String() << std::endl;
}
break;

default:
std::cout << "Message with identifier %i has arrived" << pPacket->data[0] << std::endl;
break;
}
}
}

RakNet::RakPeerInterface::DestroyInstance( pPeer );
return 0;
}
[/cpp]

里面除过我自己定义的MSG_CHATWORD之外,其他都是RakNet自己的消息枚举,大家可以跳进去看看含义,就不一一讲解了。
这篇就先这样吧,后面将客户端的写法,其他的东西慢慢都会讲的,我也是一边应用一边分享给大家经验,勿喷,交流而已。

PS:VS2010用的真的有点不习惯,不过坚持用吧,而且笔记本写代码真的有点蛋疼。

    发表评论

    电子邮件地址不会被公开。必填项已用 * 标注