Basic client/server communication in C++ using the RakNet networking engine

Quote:

RakNet is a cross-platform C++ and C# game networking engine. It is designed to be a high performance, easy to integrate, and complete solution for games and other applications.

The snippets below demonstrate basic client/server communication using the RakNet library.

ExampleServer.cpp:

// ExampleServer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#pragma comment(lib, "ws2_32.lib")

#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include "RakNetTypes.h"

#define SERVER_PORT 12345
#define MAX_CONNS 2000

int _tmain(int argc, _TCHAR* argv[])
{
	RakNet::RakPeerInterface *peerInterface = RakNet::RakPeerInterface::GetInstance();
	RakNet::Packet *packet;
	peerInterface->SetMaximumIncomingConnections(MAX_CONNS);
	peerInterface->Startup(MAX_CONNS, &RakNet::SocketDescriptor(SERVER_PORT,0), 1);

	printf("Server is ready to receive connections.\n");

	// Handle messages
	while(true){
		for (packet=peerInterface->Receive(); packet; peerInterface->DeallocatePacket(packet), packet=peerInterface->Receive())
		{
			switch (packet->data[0])
			{
			case ID_NEW_INCOMING_CONNECTION:
				printf("A remote system has successfully connected.\n");
				break;
			case ID_DISCONNECTION_NOTIFICATION:
				printf("A remote system has disconnected.\n");
				break;
			case ID_CONNECTION_LOST:
				printf("A remote system lost the connection.\n");
				break;
			}
		}
	}

	RakNet::RakPeerInterface::DestroyInstance(peerInterface);

	return 0;
}

ExampleClient.cpp:

// ExampleClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#pragma comment(lib, "ws2_32.lib")

#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>

#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include "RakNetTypes.h"
#include "StringCompressor.h"

using namespace std;

#define SERVER_PORT 12345
#define SERVER_ADDR "127.0.0.1"

RakNet::RakPeerInterface *peerInterface;
RakNet::Packet *packet;

int _tmain(int argc, _TCHAR* argv[])
{
	peerInterface = RakNet::RakPeerInterface::GetInstance();
	peerInterface->Startup(1,&RakNet::SocketDescriptor(), 1);

	printf("Client is connecting to %s:%d\n", SERVER_ADDR, SERVER_PORT);
	peerInterface->Connect(SERVER_ADDR, SERVER_PORT, 0,0);

	// Handle messages
	while(true)
	{
		for (packet=peerInterface->Receive(); packet; peerInterface->DeallocatePacket(packet), packet=peerInterface->Receive())
		{
			switch (packet->data[0])
			{
			case ID_CONNECTION_REQUEST_ACCEPTED:
					printf("The connection to the server has been accepted.\n");
					break;
			}
		}
	}

	RakNet::RakPeerInterface::DestroyInstance(peerInterface);
	return 0;
}

Output:

Server:
Server is ready to receive connections.

Client:
Client is connecting to 127.0.0.1:12345
The connection to the server has been accepted.

Server:
A remote system has successfully connected.

Client:
*client exits*

Server:
A remote system lost the connection.

The archive containing the VC++ source code can be found here.

This entry was posted in C++ and tagged , , , , , , , , , , , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?