Irrlicht Server Tutorial
From PX Documentation
The following is complete source code for a simple server using NetDog. It just creates a listening connection, installs an event type, and continuously loops, calling the main NetDog Event Loop function NDEventLoop().
This particular example is meant to be used with the Irrlicht Client Tutorial but it's so basic it could be used for just about anything. The default server behavior is to accept any and all client connections, accept network events (in this case "move" events) from those clients, and broadcast the events to all other clients.
Server Tutorial
Include ND.h to use NetDog
#include "ND.h" #include <time.h> #include <stdio.h>
Define a unique integer for the "move" event type.
enum { kMovePlayerEvent = 1 };
int main(){
Create server connection with flags to allow unexpected connections (connections from arbitrary IP addresses/ports), connection keep alives, start session immediately (without waiting for all clients), and assigning owner IDs
int conn = NDCreateServerConn(56882, NDConnFlags::useKeepAlives |
NDConnFlags::startImmediately | NDConnFlags::assignOwnerIDs);
if (conn < 0){
printf("Error: Could not establish listening port\n");
return(0);
}
Install the "move" event type. The server doesn't actually need to install the event type since it doesn't install an event handler for it, but it's still good form to install all the same event types the client will use.
// This line isn't absolutely necessary but it makes debugging a little easier NDInstallEventType(kMovePlayerEvent, NULL, NULL, "move");
Execute the NetDog Event Loop which must be called frequently (at least every 100ms, <10ms for optimal results).
Also sleep so the CPU isn't fully occupied.
for (;;){
NDEventLoop();
NDSleepMS(1);
}
return(0);
}
