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;
// Include the ProudNet.#include “include\ProudNetServer.h”// ProudNet is a namespace where all objects are grouped // under the name Proud.usingnamespace Proud;// port definitionint g_ServerPort =33334;
// Add to enable ProudusingNettention.Proud;// Define a port to use in advanceint 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 connectionp1.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 ...};
usingnamespaceNettention.Proud;// NetServer, StartServerParameter omitting namespacesNetServer netServer =newNetServer();netServer.ClientJoinHandler= (clientInfo) => { // When the client connects to the server};// Registering additional events...StartServerParameter p1 =newStartServerParameter();// 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 CNetServerClass CServerEventSink : public INetServerEvent { // When the Client's connection is complete, // a callback occurs. // Takes a CNetClientInfo object as an argument. Virtual voidOnClientJoin( CNetClientInfo *info) OVERRIDE { // Receive information from the Client // and process it. } // When the Client's connection is disconnected, // a callback occurs. Virtual voidOnClientLeave( 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.
// Set the parameters required to connect to the serverProud::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;
usingNettention.Proud;// Omitting the NetConnectionParam namespace// Set the parameters required to connect to the serverNetConnectionParam cp =newNetConnectionParam();// Same protocol version as the server, does not need to be enteredcp.protocolVersion.Set(version);// server addresscp.serverIP="localhost";// server portcp.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 serverm_netServer->OnClientJoin = [](CNetClientInfo*clientInfo){ // Logic to run on Client Join};// On the clientm_netClient->OnJoinServerComplete = [&](ErrorInfo*info,constByteArray&replyFromServer) { // Logic to run on completion of Server connection}
// On the servernetServer.ClientJoinHandler= (clientInfo) => { // Logic to run on Client Join};// On the clientnetClient.JoinServerCompleteHandler= (info, replyFromServer) => { // Logic to run on completion of server connection}
4. Disconnection
// Disconnect from the serverm_netClient->Disconnect();
// Disconnect from the servernetClient.Disconnect();
Usage
Last updated
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++.