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;
circle-exclamation

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.

circle-info

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

💡In the code example above, g_eventSink is the object created with the structure below.

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.

💡NetClientInfo in C# plays the same role as CNetClientInfo in C++.

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

2. Preparation

3. Starting a client

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.

4. Disconnection


Usage

Utilization of Serverchevron-rightUtilization of Clientchevron-right

Last updated