Created
September 6, 2020 15:59
-
-
Save Subv/65062e3b4660fe835e27e36901fe7dcf to your computer and use it in GitHub Desktop.
enet test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <enet.h> | |
#include <stdio.h> | |
int main() { | |
if (enet_initialize () != 0) { | |
printf("An error occurred while initializing ENet.\n"); | |
return 1; | |
} | |
ENetAddress address = {0}; | |
address.host = ENET_HOST_ANY; /* Bind the server to the default localhost. */ | |
address.port = 7777; /* Bind the server to port 7777. */ | |
#define MAX_CLIENTS 32 | |
/* create a server */ | |
ENetHost * server = enet_host_create(&address, MAX_CLIENTS, 2, 0, 0); | |
if (server == NULL) { | |
printf("An error occurred while trying to create an ENet server host.\n"); | |
return 1; | |
} | |
printf("Started a server...\n"); | |
ENetEvent event; | |
/* Wait up to 1000 milliseconds for an event. (WARNING: blocking) */ | |
while (enet_host_service(server, &event, 1000) > 0) { | |
switch (event.type) { | |
case ENET_EVENT_TYPE_CONNECT: | |
printf("A new client connected from %x:%u.\n", event.peer->address.host, event.peer->address.port); | |
/* Store any relevant client information here. */ | |
event.peer->data = "Client information"; | |
break; | |
case ENET_EVENT_TYPE_RECEIVE: | |
printf("A packet of length %lu containing %s was received from %s on channel %u.\n", | |
event.packet->dataLength, | |
event.packet->data, | |
event.peer->data, | |
event.channelID); | |
/* Clean up the packet now that we're done using it. */ | |
enet_packet_destroy (event.packet); | |
break; | |
case ENET_EVENT_TYPE_DISCONNECT: | |
printf("%s disconnected.\n", event.peer->data); | |
/* Reset the peer's client information. */ | |
event.peer->data = NULL; | |
break; | |
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: | |
printf("%s disconnected due to timeout.\n", event.peer->data); | |
/* Reset the peer's client information. */ | |
event.peer->data = NULL; | |
break; | |
case ENET_EVENT_TYPE_NONE: | |
break; | |
} | |
} | |
enet_host_destroy(server); | |
enet_deinitialize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment