Skip to main content
To host a multiplayer game, one player must set up a server that listens for incoming connections. The SetupHost() function handles all server initialization.

How hosting works

1

Configure the server address

The server binds to all available network interfaces on the specified port:
main.cpp
ENetAddress address;
address.host = ENET_HOST_ANY;
address.port = SERVER_PORT;
ENET_HOST_ANY allows the server to accept connections on any network interface.
2

Create the ENet host

Create the server host with the following parameters:
main.cpp
serverHost = enet_host_create(
    &address,
    1, // max clients
    2, // max channels
    0, // incoming bandwidth (0 = unlimited)
    0  // outgoing bandwidth (0 = unlimited)
);
Parameters:
  • Max clients: 1 - Only one client can connect for 1v1 gameplay
  • Max channels: 2 - Two communication channels available
  • Bandwidth limits: 0 - No bandwidth restrictions
3

Verify host creation

Check if the host was created successfully:
main.cpp
if (serverHost == NULL){
    printf("failed to create server host");
    return false;
}
printf("Server hosting on port %d\n" , SERVER_PORT);
return true;
If creation fails, the function returns false and the game returns to the main menu.
4

Wait for connections

The game enters the HOSTING state and waits for a client to connect:
main.cpp
ENetEvent event;
while (enet_host_service(serverHost, &event, 0) > 0) {
    if (event.type == ENET_EVENT_TYPE_CONNECT) {
        printf("Client connected!\n");
        clientPeer = event.peer;
        connectionState = "CONNECTED";
        
        // Start the game
        state = "MULTIPLAYER";
    }
}
Once a client connects, the game transitions to the multiplayer gameplay state.

Complete SetupHost function

main.cpp
bool SetupHost(){
    ENetAddress address;
    address.host = ENET_HOST_ANY;
    address.port = SERVER_PORT;
    // making the server host
    serverHost = enet_host_create(
        &address,
        1, // max clients
        2, // max channels
        0, // incoming bandwidth (0 = unlimited)
        0
    );
    if (serverHost == NULL){
        printf("failed to create server host");
        return false;
    }
    printf("Server hosting on port %d\n" , SERVER_PORT);
    return true;
}

Network variables

The following global variables manage the host connection:
main.cpp
ENetHost* serverHost = nullptr; // for host server
ENetPeer* clientPeer = nullptr; // for host connection to client
When hosting, the server uses serverHost to manage the connection and stores the connected client in clientPeer.

Troubleshooting

If the server fails to start, ensure:
  • Port 7777 is not already in use by another application
  • Firewall settings allow incoming connections on port 7777
  • ENet has been properly initialized before calling SetupHost()

Build docs developers (and LLMs) love