当前位置: 首页 > 工具软件 > Raknet > 使用案例 >

RakNet最简单的服务器客户端程序

乐正焕
2023-12-01

//下面这段程序使用的是当前最新的RakNet_PC-4.05

//把RakNet下载下来后先编译

//依赖的库:ws2_32.lib RakNetLibStaticDebug.lib

#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)
{
 RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
 bool isServer;
 RakNet::Packet *packet;
 
 RakNet::SocketDescriptor sd(SERVER_PORT,0);

 //设置这个peer最多能连接MAX_CLIENTS个客户端

 peer->Startup(MAX_CLIENTS, &sd, 1);
 isServer = true;
 
 printf("Starting the server.\n");
 //作为服务器
 peer->SetMaximumIncomingConnections(MAX_CLIENTS);
 

 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;
}

 //客户端程序

int main(void)
{
 
 RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();

 
 RakNet::SocketDescriptor sd;
 peer->Startup(1,&sd, 1);
 
 //作为客户端连接,连接到主机ip地址和端口号
 peer->Connect("192.168.1.198", SERVER_PORT, 0, 0);


 int i=0;
 while(i++<10)
 {

  //给主机发送数据

  RakNet::BitStream bsOut;
  bsOut.Write((RakNet::MessageID)ID_GAME_MESSAGE_1);
  bsOut.Write("Hello world");

  //给所有连接方,发送数据
  peer->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,RakNet::UNASSIGNED_SYSTEM_ADDRESS,true);

  Sleep(500);
 }

 //断开连接,这个会给对方发送一个消息,告诉它要断开连接了。
 peer->Shutdown(300); 
  
 RakNet::RakPeerInterface::DestroyInstance(peer);

 return 0;
}

 

 

 

 类似资料: