build.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #define ENET_IMPLEMENTATION
  2. #include <enet.h>
  3. int main() {
  4. if (enet_initialize () != 0) {
  5. printf("An error occurred while initializing ENet.\n");
  6. return 1;
  7. }
  8. ENetAddress address = {0};
  9. address.host = ENET_HOST_ANY; /* Bind the server to the default localhost. */
  10. address.port = 7777; /* Bind the server to port 7777. */
  11. #define MAX_CLIENTS 32
  12. /* create a server */
  13. ENetHost * server = enet_host_create(&address, MAX_CLIENTS, 2, 0, 0);
  14. if (server == NULL) {
  15. printf("An error occurred while trying to create an ENet server host.\n");
  16. return 1;
  17. }
  18. printf("Started a server...\n");
  19. ENetEvent event;
  20. /* Wait up to 1000 milliseconds for an event. (WARNING: blocking) */
  21. while (enet_host_service(server, &event, 1000) > 0) {
  22. switch (event.type) {
  23. case ENET_EVENT_TYPE_CONNECT:
  24. printf("A new client connected from %x:%u.\n", event.peer->address.host, event.peer->address.port);
  25. /* Store any relevant client information here. */
  26. event.peer->data = "Client information";
  27. break;
  28. case ENET_EVENT_TYPE_RECEIVE:
  29. printf("A packet of length %lu containing %s was received from %s on channel %u.\n",
  30. event.packet->dataLength,
  31. event.packet->data,
  32. event.peer->data,
  33. event.channelID);
  34. /* Clean up the packet now that we're done using it. */
  35. enet_packet_destroy (event.packet);
  36. break;
  37. case ENET_EVENT_TYPE_DISCONNECT:
  38. printf ("%s disconnected.\n", event.peer->data);
  39. /* Reset the peer's client information. */
  40. event.peer->data = NULL;
  41. break;
  42. case ENET_EVENT_TYPE_NONE: break;
  43. }
  44. }
  45. enet_host_destroy(server);
  46. enet_deinitialize();
  47. return 0;
  48. }