| 1 |
|
| 2 |
/* client.cpp */ |
| 3 |
#include <stdio.h> |
| 4 |
#include <string.h> |
| 5 |
#include <enet/enet.h> |
| 6 |
|
| 7 |
int main(int argc, char *argv[]) |
| 8 |
{ |
| 9 |
ENetHost *client; |
| 10 |
ENetAddress address; |
| 11 |
ENetPeer *peer; |
| 12 |
ENetEvent event; |
| 13 |
char message[1024]; |
| 14 |
int serviceResult; |
| 15 |
|
| 16 |
puts("Starting client"); |
| 17 |
|
| 18 |
if (enet_initialize() != 0) |
| 19 |
{ |
| 20 |
fprintf(stderr, "Error initialising enet"); |
| 21 |
exit(EXIT_FAILURE); |
| 22 |
} |
| 23 |
|
| 24 |
client = enet_host_create(NULL, /* create a client host */ |
| 25 |
1, /* number of clients */ |
| 26 |
2, /* number of channels */ |
| 27 |
57600 / 8, /* incoming bandwith */ |
| 28 |
14400 / 8); /* outgoing bandwith */ |
| 29 |
|
| 30 |
if (client == NULL) |
| 31 |
{ |
| 32 |
fprintf(stderr, "Could not create client host"); |
| 33 |
exit(EXIT_FAILURE); |
| 34 |
} |
| 35 |
|
| 36 |
enet_address_set_host(&address, "localhost"); |
| 37 |
address.port = 1234; |
| 38 |
|
| 39 |
peer = enet_host_connect(client, |
| 40 |
&address, /* address to connect to */ |
| 41 |
2, /* number of channels */ |
| 42 |
0); /* user data supplied to |
| 43 |
the receiving host */ |
| 44 |
|
| 45 |
if (peer == NULL) |
| 46 |
{ |
| 47 |
fprintf(stderr, |
| 48 |
"No available peers for initiating an ENet " |
| 49 |
"connection.\n"); |
| 50 |
exit(EXIT_FAILURE); |
| 51 |
} |
| 52 |
|
| 53 |
/* Try to connect to server within 5 seconds */ |
| 54 |
if (enet_host_service(client, &event, 5000) > 0 && |
| 55 |
event.type == ENET_EVENT_TYPE_CONNECT) |
| 56 |
{ |
| 57 |
puts("Connection to server succeeded."); |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
/* Either the 5 seconds are up or a disconnect event was */ |
| 62 |
/* received. Reset the peer in the event the 5 seconds */ |
| 63 |
/* had run out without any significant event. */ |
| 64 |
enet_peer_reset(peer); |
| 65 |
|
| 66 |
fprintf(stderr, "Connection to server failed."); |
| 67 |
exit(EXIT_FAILURE); |
| 68 |
} |
| 69 |
|
| 70 |
while (true) |
| 71 |
{ |
| 72 |
serviceResult = 1; |
| 73 |
|
| 74 |
/* Keep doing host_service until no events are left */ |
| 75 |
while (serviceResult > 0) |
| 76 |
{ |
| 77 |
serviceResult = enet_host_service(client, &event, 0); |
| 78 |
|
| 79 |
if (serviceResult > 0) |
| 80 |
{ |
| 81 |
switch (event.type) |
| 82 |
{ |
| 83 |
case ENET_EVENT_TYPE_CONNECT: |
| 84 |
printf("A new client connected from %x:%u.\n", |
| 85 |
event.peer->address.host, |
| 86 |
event.peer->address.port); |
| 87 |
|
| 88 |
event.peer->data = (void *)"New User"; |
| 89 |
break; |
| 90 |
|
| 91 |
case ENET_EVENT_TYPE_RECEIVE: |
| 92 |
printf( |
| 93 |
"A packet of length %u containing '%s' was " |
| 94 |
"received from %s on channel %u.\n", |
| 95 |
event.packet->dataLength, |
| 96 |
event.packet->data, |
| 97 |
event.peer->data, |
| 98 |
event.channelID); |
| 99 |
|
| 100 |
/* Clean up the packet now that we're done using it. |
| 101 |
> */ |
| 102 |
enet_packet_destroy(event.packet); |
| 103 |
|
| 104 |
break; |
| 105 |
|
| 106 |
case ENET_EVENT_TYPE_DISCONNECT: |
| 107 |
printf("%s disconnected.\n", event.peer->data); |
| 108 |
|
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
else if (serviceResult > 0) |
| 113 |
{ |
| 114 |
puts("Error with servicing the client"); |
| 115 |
exit(EXIT_FAILURE); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
printf("Say> "); |
| 120 |
#ifdef _WIN32 |
| 121 |
gets_s(message, 1024); |
| 122 |
#else |
| 123 |
fgets(message, 1024, stdin); |
| 124 |
#endif |
| 125 |
if (strcmp(message, "exit") == 0 || |
| 126 |
strcmp(message, "quit") == 0) |
| 127 |
{ |
| 128 |
break; |
| 129 |
} |
| 130 |
|
| 131 |
if (strlen(message) > 0) |
| 132 |
{ |
| 133 |
ENetPacket *packet = enet_packet_create(message, strlen(message) + 1, ENET_PACKET_FLAG_RELIABLE); |
| 134 |
enet_peer_send(peer, 0, packet); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
enet_peer_disconnect(peer, 0); |
| 139 |
|
| 140 |
/* Allow up to 3 seconds for the disconnect to succeed */ |
| 141 |
/* and drop any packets received packets */ |
| 142 |
while (enet_host_service(client, &event, 3000) > 0) |
| 143 |
{ |
| 144 |
switch (event.type) |
| 145 |
{ |
| 146 |
case ENET_EVENT_TYPE_RECEIVE: |
| 147 |
enet_packet_destroy(event.packet); |
| 148 |
break; |
| 149 |
|
| 150 |
case ENET_EVENT_TYPE_DISCONNECT: |
| 151 |
puts("Disconnection succeeded."); |
| 152 |
break; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
enet_host_destroy(client); |
| 157 |
enet_deinitialize(); |
| 158 |
|
| 159 |
return 0; |
| 160 |
} |