Skip to content

Creating A Server

BenApplegate edited this page Oct 27, 2021 · 3 revisions

Creating A Server


  1. Make sure to add the library as a reference to your C# project
  2. Include a using tag to the library at the top of any classes that use it
using com.benjaminapplegate.EasyNetworking;
  1. Create an instance of the EasyServer class, it is recommended to make it accessible to other classes in your project so you can send messages to clients from anywhere in your project.
public static EasyServer server

static void Main(string[] args){

    server = new EasyServer(maxConnections, port);

}

The maxConnections property specifies the number of clients that are allowed to connect to your server at one time and the port property specifies the port that the server will be hosted on.

  1. Define what happens when the server receives a packet

Every packet in this library has a type attached to it, when a packet is sent, the client or server specifies what type it is. On our server we define a function that gets called whenever a specific type of packet is received

public static EasyServer server

static void Main(string[] args){

    server = new EasyServer(maxConnections, port);
    server.AddPacketHandler(packetType, handlerFunction);

}

public static void handlerFunction(int clientId, Packet packet){
    //handle data here
}

The packetType parameter is an integer that is the type of packet the client sent, these are defined by the client that sent the packet. The handlerFunction parameter is a delegate to a function that takes an integer that represents the id of the client that sends the packet and the packet itself. We will cover Reading and Writing to and from a packet later in this tutorial.

  1. Start the server
public static EasyServer server

static void Main(string[] args){

    server = new EasyServer(maxConnections, port);
    server.AddPacketHandler(packetType, handlerFunction);
    server.StartServer();
}
  1. Stop the server when it is no longer needed
public static EasyServer server

static void Main(string[] args){

    server = new EasyServer(maxConnections, port);
    server.AddPacketHandler(packetType, handlerFunction);
    server.StartServer();
    while(condition){
        //Run other code while the server is online
    }
    server.Stop();
}

Sending a Packet to clients

The server class has many functions to send a packet to clients

server.SendPacketToId(packet, clientId);

This function sends the specified packet to a specific client using the integer ID that the server has assigned to it

server.SendPacketToTcpClient(packet, tcpClient);

This function sends the specified packet to a specific client using the TCPClient object that is connected to it. This is most common to use when you need to let a client know that the server is full

server.SendPacketToAll(packet);

This function sends the specified packet to all clients that are currently connected to the server.

server.SendPacketToAllButOne(packet, clientId);

This function sends the specified packet to all clients connected to the server except the client that has been assigned the specified integer ID.


Handling Connections

Handling a successful connection

public static EasyServer server;

public static void OnClientConnection(TcpClient client, int clientId){
    //Handle client connection
}

static void Main(string[] args){

    server = new EasyServer(maxConnections, port);
    server.clientConnection = OnClientConnection;
    server.StartServer();
}

The clientConnection variable on the server class holds a delegate to a function that gets called when a client successfully connects to the server. While the Server class has a public dictionary of client ids and their respective TcpClients, it may be nice to keep your own records of these as well as sending a welcome message back to the client. This function is the place to do these things.

Handling a connection when the server is full

public static EasyServer server;

public static void OnServerFullConnection(TcpClient client, int clientId){
    //Handle client connection
}

static void Main(string[] args){

    server = new EasyServer(maxConnections, port);
    server.serverFullConnection = OnServerFullConnection;
    server.StartServer();
}

The serverFullConnection variable on the server class holds a delegate to a function that gets called when a client connects to the server, but the server is full. This is where you should send a packet back to the client stating that the server is full. Note that the clientId here will always be -1


Reading and Writing to a packet

The packet class has simple functions to read and write most primitive objects, these include:

  • int
  • float
  • double
  • bool
  • char
  • short
  • long
  • string

The read functions return the datatype specified by reading that data from the packet, and then preparing the packet to read the next data in the packet. Note that packets must be read in the order in which they were written, For Example: if a string was written into a packet and then an int, you should not read an int then a string, you must read the string first and the int second.

The write functions do not return anything and they take the data to be written as a parameter.

Examples

Reading an integer

int something = packet.ReadInt(); 

Writing a string

packet.WriteString("Text to write");

See the Example Programs to see a fully functional server and client that makes use of all these features