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
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
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 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
Once a client connects, the game transitions to the multiplayer gameplay state.

Complete SetupHost function

main.cpp

Network variables

The following global variables manage the host connection:
main.cpp
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()