ProudNet.Eng
WebsiteProud ConsoleLanguage
  • 🌐ProudNet
    • ProudNet Introduction
    • Download and Install
      • How to verify your ProudNet license
      • AMI
    • Project Settings
      • C++
      • C#
      • Mac Xcode
      • Linux
      • Unity3D
        • iOS Build
      • Unreal Engine 4
      • Running the PIDL Compiler
    • Using ProudNet
      • Server and Client
        • Utilization of Server
        • Utilization of Client
      • RMI
        • Utilization of RMI
      • PIDL
        • Utilization of PIDL
      • Event handling
      • Communication messages
      • P2P Communication
        • Using P2P communication
    • Utilization of ProudNet
      • How to use
      • Tips for performance
    • Using DB in ProudNet
      • DB Cache System ver.2
        • DB Cache Theory and Understanding
        • Install DB Cache and Set Up Network
        • DB Cache Server and Client
        • DB Cache usage and application
          • Utilization of DB Cache
      • ADO API
      • ODBC API
    • ProudNet Utility
  • ProudNet Note
    • Technical Notes
      • Main Loop
      • Setting up a server firewall
      • Encryption and decryption
      • What to do in case of an error
      • List of error messages
      • Synchronizing Character Position
      • Client-Server Communication
      • MiniDump (Error Dump System)
      • [Version 1.6] Server-to-Server LAN Communicator
    • Glossary
    • Sample examples
  • 🌐Proud Service
    • Guide for Console
    • ProudChat
      • Download SDK
        • C++
        • C#
        • Unity3D
        • Unreal Engine 4
      • Features in Console
Powered by GitBook
On this page
  • Start a server
  • 1. Creating a server
  • 2. Preparation
  • 3. Starting a server
  • 4. Disconnection
  • 5. Start receiving client connections
  • Start a client
  • 1. Creating a client
  • 2. Preparation
  • 3. Starting a client
  • 4. Disconnection
  • Usage
  1. ProudNet
  2. Using ProudNet

Server and Client

Last updated 1 year ago

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;
Nettention.Proud.NetServer netServer = new Nettention.Proud.NetServer();

Before starting the server, be sure to check the .

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;
// Add to enable Proud
using Nettention.Proud;

// Define a port to use in advance
int 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);
// You can use it by registering logic to run as a Lambda on specific events.
srv->OnClientJoin = [...](CNetClientInfo* clientInfo) {
        // my event handler
        ...
};
using namespace Nettention.Proud;
// NetServer, StartServerParameter omitting namespaces

NetServer netServer = new NetServer();

netServer.ClientJoinHandler = (clientInfo) => {
    // When the client connects to the server
};

// Registering additional events
...

StartServerParameter p1 = new StartServerParameter();
// You don't need to set the protocolVersion to use it.
p1.protocolVersion = "Same as the client protocolVersion";
// Enter the port number you registered above or enter it manually.
p1.tcpPorts.Add(serverPort);

netServer.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
}
// In C#, you can use event handlers without creating a separate event object.

// Runs when a client connects to the server.
netServer.ClientJoinHandler = (clientInfo) =>
{
    Console.Write("Client {0} connected.\n", clientInfo.hostID);
};

// Runs when the client server connection is lost.
netServer.ClientLeaveHandler = (clientInfo, errorInfo, comment) =>
{
    Console.Write("Client {0} disconnected.\n", clientInfo.hostID);
};

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();
Nettention.Proud.NetClient netClient = new Nettention.Proud.NetClient();

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;	
using Nettention.Proud;
// Omitting the NetConnectionParam namespace

// Set the parameters required to connect to the server
NetConnectionParam cp = new NetConnectionParam();

// Same protocol version as the server, does not need to be entered
cp.protocolVersion.Set(version);
// server address
cp.serverIP = "localhost";
// server port
cp.serverPort = 33334;

3. Starting a client

// Use the parameters created in the preparation example above.
m_netClinet->Connect(cp);
// Use the parameters created in the preparation example above.
netClient.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
}
// On the server
netServer.ClientJoinHandler = (clientInfo) => {
    // Logic to run on Client Join
};

// On the client
netClient.JoinServerCompleteHandler = (info, replyFromServer) => {
    // Logic to run on completion of server connection
}

4. Disconnection

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


Usage

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

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

🌐
💡
💡
Utilization of Server
Utilization of Client
Server's UDP port type