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

RakNet的远程功能调用(RPC)客户端

史磊
2023-12-01

在本系列中使用的raknet版本号为3.611

 

// RakNetRpcClient.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdafx.h"
#include "Winsock2.h"
#include "Kbhit.h"
#include "stdio.h"
#include "stdlib.h"
#include "RakSleep.h"
#include "RakNetTypes.h"
#include "RakNetworkFactory.h"
#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"

#pragma comment(lib, "WS2_32.lib")
//
//int _tmain(int argc, _TCHAR* argv[])
//{
// char   szIP[32];
// char   szPort[10];
// 
// RakPeerInterface* Client;
// Client = RakNetworkFactory::GetRakPeerInterface();
//
// printf("-------------------------------------/n");
// printf("输入服务器信息,不输入采用默认配置,IP:127.0.0.1,端口为:8000/n");
// printf("-------------------------------------/n");
// printf("IP地址:");
//
// gets(szIP);
// if (szIP[0]==0)
// {
//  strcpy(szIP, "127.0.0.1");
//  printf("%s/n",szIP);
// }
// printf("端口号:");
// gets(szPort);
// Client->AllowConnectionResponseIPMigration(false);
// if (szPort[0]==0)
// {
//  strcpy(szPort, "8000");
//  printf("%s/n",szPort);
// }  
// 
// Client->SetTimeoutTime(2000,UNASSIGNED_SYSTEM_ADDRESS);//设置超时时间
// Client->SetSplitMessageProgressInterval(10000);//设置download_progress时间间隔
//
// SocketDescriptor  socketDescriptor(0, 0);
// Client->Startup(1,30,&socketDescriptor, 1);
//
// Client->SetOccasionalPing(true);
//
// bool b = Client->Connect(szIP, atoi(szPort), NULL, 0); 
//
// if (b)
//  puts("Attempting connection");
// else
// {
//  puts("Bad connection attempt.  Terminating.");
//  goto CLIENT_END;
// }
//    while(true)
// {
// /* RPC("PrintMessage",
//   message,
//   (strlen(message)+1)*8,
//   HIGH_PRIORITY,
//   RELIABLE_ORDERED, 0,UNASSIGNED_PLAYER_ID , true, false, UNASSIGNED_NETWORK_ID,0);
//
// */ //Client->RPC("PrintMessage", 0, HIGH_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_SYSTEM_ADDRESS,  false,0,0,0);
//  Client->RPC("PrintMessage",0, 0, HIGH_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_SYSTEM_ADDRESS, true, false, UNASSIGNED_NETWORK_ID, 0);
// }
//CLIENT_END:
// RakNetworkFactory::DestroyRakPeerInterface(Client);
// return 0;
//}
//

Packet *packet;//网络传输中用于存储数据的一个数据结构
/
/*
Struct Packet
{
PlayerID playerId; //表明了包的出处。每一个连接服务器的客户端都将被分配一个唯一的ID号,用于标识自己。
Unsigned long length; //数据长度
Unsigned long bitsize; //比特大小
Char *data; //包中的数据
}
*/
/
char message[2048];
char message1[300];
RakPeerInterface* rakClientInterface;

void PrintMessage(RPCParameters *rpcParameters)
{
 printf("客户端RPC调用:%s/n",rpcParameters->input);
 sprintf(message1,"%s",rpcParameters->input);

}

int main(void)
{
 char szIP[32];
 char szPort[10];

 rakClientInterface = RakNetworkFactory::GetRakPeerInterface();//初始化一个客户端实例,为它分配内存

 printf("客服端已经建立。");

 // 运行客户端
 printf("输入服务器IP地址:/n");
 gets(szIP);
 if (szIP[0]==0)
 {
  strcpy(szIP, "127.0.0.1");
  printf("%s/n",szIP);
 }
 printf("端口号:");
 gets(szPort);
 rakClientInterface->AllowConnectionResponseIPMigration(false);
 if (szPort[0]==0)
 {
  strcpy(szPort, "60000");
  printf("%s/n",szPort);
 }  
 
 SocketDescriptor    socketDescriptor(0, 0);
 rakClientInterface->Startup(1,30,&socketDescriptor, 1);

 bool b = rakClientInterface->Connect(szIP, atoi(szPort), NULL, 0); 

 if (b)
  puts("Attempting connection");
 else
 {
  puts("Bad connection attempt.  Terminating.");
  goto CLIENT_END;
 }


 REGISTER_STATIC_RPC(rakClientInterface, PrintMessage);
 while (1)
 {
  if(kbhit())
  {
   gets(message);

   rakClientInterface->RPC("PrintMessage",
    message,
    (strlen(message)+1)*8,
    HIGH_PRIORITY,
    RELIABLE_ORDERED, 0,UNASSIGNED_SYSTEM_ADDRESS , true, false, UNASSIGNED_NETWORK_ID,0);

  }


  packet = rakClientInterface->Receive();

  if (packet)
  {
   switch (packet->data[0])
   {
   case ID_NO_FREE_INCOMING_CONNECTIONS:
    printf("服务器已满./n");
    break;
   case ID_DISCONNECTION_NOTIFICATION:
    printf("连接中断./n");
   case ID_CONNECTION_LOST:
    printf("连接丢失./n");
                goto CLIENT_END;
   case ID_CONNECTION_REQUEST_ACCEPTED:
                printf("服务器连接成功./n");
    break;
   default:
    printf("%s/n", packet->data);
    break;
   }
 
    rakClientInterface->DeallocatePacket(packet);
  }
 }

 // TODO - Add code body here
 //getch();
 if (rakClientInterface)
  RakNetworkFactory::DestroyRakPeerInterface(rakClientInterface);
CLIENT_END:
 return 0;

}

 

 类似资料: