build.c 2.0 KB

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