Server and Client

Start a server

To start the server, you first need to get a server object, as shown in the example below. As soon as you create the server, it will not start communicating with clients or create a thread pool right away, so you will need to call Start on the created object to run the server.

1. Creating a server

m_netServer = Proud::CNetServer::Create();
// Healed objects can be removed with the delete operator. 
delete m_netServer;

Before starting the server, be sure to check the Server's UDP port type.

2. Preparation

// Include the ProudNet.
#include “include\ProudNetServer.h”
  
// ProudNet is a namespace where all objects are grouped 
// under the name Proud.
using namespace Proud;
  
// port definition
int g_ServerPort = 33334;

3. Starting a server

First, we create a server object and then call the SetEventSink function. This is the process of registering an object to receive callbacks for events that happen on the server. You inherit from the INetServerEvent object and pass a pointer to the object you created, and the server will callback events through this object.

Starting with C++11, it is possible to register events using Lambda instead of SetEventSink.

// Please note that this is only for C++ 11 and earlier.
// g_eventSink is an object that inherits from INetServerEvent.
CNetServer* srv = 
         Proud::CNetServer::Create();
srv->SetEventSink(
         &g_eventSink);
  
// Set the parameters required to start the server.
CStartServerParameter p1;
 
// Port to receive the client's connection
p1.m_tcpPort = 33334;  
  
srv->Start(p1);

// To create an object to receive events from CNetServer
Class CServerEventSink 
         : public INetServerEvent 
{
       // When the Client's connection is complete, 
       // a callback occurs.
       // Takes a CNetClientInfo object as an argument. 
       Virtual void OnClientJoin(
           CNetClientInfo *info) 
           OVERRIDE
       {
           // Receive information from the Client  
           // and process it.
       }
       // When the Client's connection is disconnected, 
       // a callback occurs.
           Virtual void OnClientLeave(
               CNetClientInfo *info) 
               OVERRIDE
       {
               // Receive information from the Client  
               // and process it.
       }
       // Omit the rest of the Event
}

Receives a CNetClientInfo object as an argument in the event that is callbacked. The CNetClientInfo object contains the connected client information, and the CNetClientInfo member m_HostID is the ID value that distinguishes each host.

4. Disconnection

Functions
Description

Stop

Server stopped. Disconnect all connections.

CloseConnection (Client's HostID)

Disconnect the corresponding client.

5. Start receiving client connections

In order to receive client connections on the server, we need to prepare a server-side Listening Port and thread pool. To do this, we need to create a Server object and call the Start methods.

Start a client

Like the server, the client can connect to the server after the object is created.

1. Creating a client

m_netClient = Proud::CNetClient::Create();

2. Preparation

// Set the parameters required to connect to the server
Proud::CNetConnectionParam cp;

// You must enter the same protocol version as the server. You may not enter any at all.
cp.m_protocolVersion = g_version;
cp.m_closeNoPingPongTcpConnections=false;
cp.m_serverIP = _PNT("localhost");
cp.m_serverPort = 33334;	

3. Starting a client

// Use the parameters created in the preparation example above.
m_netClinet->Connect(cp);

Events that occur while a client is connecting to the server

  • When Connect is executed, the server receives a OnConnectionRequest, where it can reject clients attempting to connect.

  • Accepting the client connection in OnConnectionRequest completes the connection process, the client and server receive codes like below example.

// On the server
m_netServer->OnClientJoin = [](CNetClientInfo *clientInfo){
    // Logic to run on Client Join
};

// On the client
m_netClient->OnJoinServerComplete = [&](ErrorInfo *info, const ByteArray &replyFromServer) {
    // Logic to run on completion of Server connection
}

4. Disconnection

// Disconnect from the server
m_netClient->Disconnect();


Usage

Last updated