<div>The tutorials are more than enough usually (as you just experienced).<br></div><div>I think samples are a good idea, though. They would just need to be a lot more documented and clearer code imo :)</div><div><br></div>
<br><div class="gmail_quote">On Thu, Nov 11, 2010 at 1:41 PM, Shafqat Bhuiyan <span dir="ltr"><<a href="mailto:priomsrb@gmail.com">priomsrb@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Thanks.<br>
<br>
It'd be great if there was an examples page on the website or maybe a<br>
samples directory in the source distribution so it's easier for people<br>
to learn.<br>
<div><div class="h5"><br>
On Thu, Nov 11, 2010 at 11:28 PM, fuzzy spoon <<a href="mailto:fuzzyspoon@gmail.com">fuzzyspoon@gmail.com</a>> wrote:<br>
> Thanks for taking the time to make an example :)<br>
> Im sure it will help someone along the way.<br>
> On Thu, Nov 11, 2010 at 12:21 PM, Shafqat Bhuiyan <<a href="mailto:priomsrb@gmail.com">priomsrb@gmail.com</a>><br>
> wrote:<br>
>><br>
>> Hi,<br>
>><br>
>> I was looking around for some example programs that use enet<br>
>> but like others I couldn't find one. So I tried to make one myself.<br>
>><br>
>> This is a simple chat program. Multiple clients can join the server<br>
>> and write messages. Messages will be received by all clients<br>
>> (including the one that sent it).<br>
>><br>
>> Most of the code is a direct copy/paste from the enet tutorial. The<br>
>> code is definitely not perfect but is suitable for a beginner to look<br>
>> around in.<br>
>><br>
>><br>
>><br>
>> /* server.cpp */<br>
>> #include <stdio.h><br>
>> #include <enet/enet.h><br>
>><br>
>> int main (int argc, char *argv[])<br>
>> {<br>
>> ENetAddress address;<br>
>> ENetHost *server;<br>
>> ENetEvent event;<br>
>> int serviceResult;<br>
>><br>
>> puts ("Starting server");<br>
>><br>
>> if (enet_initialize () != 0)<br>
>> {<br>
>> puts ("Error initialising enet");<br>
>> exit (EXIT_FAILURE);<br>
>> }<br>
>><br>
>><br>
>> /* Bind the server to the default localhost. */<br>
>> /* A specific host address can be specified by */<br>
>> /* enet_address_set_host (& address, "x.x.x.x"); */<br>
>> address.host = ENET_HOST_ANY;<br>
>> /* Bind the server to port 1234. */<br>
>> address.port = 1234;<br>
>><br>
>> server = enet_host_create (&address,<br>
>> 32, /* number of clients */<br>
>> 2, /* number of channels */<br>
>> 0, /* Any incoming bandwith */<br>
>> 0); /* Any outgoing bandwith */<br>
>><br>
>> if (server == NULL)<br>
>> {<br>
>> puts ("Could not create server host");<br>
>> exit (EXIT_FAILURE);<br>
>> }<br>
>><br>
>><br>
>> while (true)<br>
>> {<br>
>> serviceResult = 1;<br>
>><br>
>> /* Keep doing host_service until no events are left */<br>
>> while (serviceResult > 0)<br>
>> {<br>
>> /* Wait up to 1000 milliseconds for an event. */<br>
>> serviceResult = enet_host_service (server, &event, 1000);<br>
>><br>
>> if (serviceResult > 0)<br>
>> {<br>
>><br>
>> switch (event.type)<br>
>> {<br>
>> case ENET_EVENT_TYPE_CONNECT:<br>
>> printf ("A new client connected from %x:%u.\n",<br>
>> event.peer -> address.host,<br>
>> event.peer -> address.port);<br>
>><br>
>> /* Store any relevant client information here. */<br>
>> event.peer->data = (void*)"Client information";<br>
>><br>
>> break;<br>
>><br>
>> case ENET_EVENT_TYPE_RECEIVE:<br>
>> printf ("A packet of length %u containing '%s' was "<br>
>> "received from %s on channel %u.\n",<br>
>> event.packet -> dataLength,<br>
>> event.packet -> data,<br>
>> event.peer -> data,<br>
>> event.channelID);<br>
>><br>
>> /* Tell all clients about this message */<br>
>> enet_host_broadcast (server, 0, event.packet);<br>
>><br>
>> break;<br>
>><br>
>> case ENET_EVENT_TYPE_DISCONNECT:<br>
>> printf ("%s disconected.\n", event.peer -> data);<br>
>><br>
>> /* Reset the peer's client information. */<br>
>><br>
>> event.peer -> data = NULL;<br>
>><br>
>> break;<br>
>> }<br>
>> }<br>
>> else if (serviceResult > 0)<br>
>> {<br>
>> puts ("Error with servicing the server");<br>
>> exit (EXIT_FAILURE);<br>
>> }<br>
>> }<br>
>><br>
>> }<br>
>><br>
>> enet_host_destroy (server);<br>
>> enet_deinitialize ();<br>
>><br>
>> return 0;<br>
>><br>
>> }<br>
>><br>
>><br>
>><br>
>> /************************************************/<br>
>><br>
>><br>
>><br>
>> /* client.cpp */<br>
>> #include <stdio.h><br>
>> #include <string.h><br>
>> #include <enet/enet.h><br>
>><br>
>><br>
>> int main (int argc, char* argv[]) {<br>
>> ENetHost *client;<br>
>> ENetAddress address;<br>
>> ENetPeer *peer;<br>
>> ENetEvent event;<br>
>> char message[1024];<br>
>> int serviceResult;<br>
>><br>
>> puts ("Starting client");<br>
>><br>
>> if (enet_initialize () != 0) {<br>
>> fprintf (stderr, "Error initialising enet");<br>
>> exit (EXIT_FAILURE);<br>
>> }<br>
>><br>
>> client = enet_host_create (NULL, /* create a client host */<br>
>> 1, /* number of clients */<br>
>> 2, /* number of channels */<br>
>> 57600 / 8, /* incoming bandwith */<br>
>> 14400 / 8); /* outgoing bandwith */<br>
>><br>
>> if (client == NULL) {<br>
>> fprintf (stderr, "Could not create client host");<br>
>> exit (EXIT_FAILURE);<br>
>> }<br>
>><br>
>><br>
>> enet_address_set_host (&address, "localhost");<br>
>> address.port = 1234;<br>
>><br>
>> peer = enet_host_connect (client,<br>
>> &address, /* address to connect to */<br>
>> 2, /* number of channels */<br>
>> 0); /* user data supplied to<br>
>> the receiving host */<br>
>><br>
>> if (peer == NULL) {<br>
>> fprintf (stderr, "No available peers for initiating an ENet "<br>
>> "connection.\n");<br>
>> exit (EXIT_FAILURE);<br>
>> }<br>
>><br>
>><br>
>> /* Try to connect to server within 5 seconds */<br>
>> if (enet_host_service (client, &event, 5000) > 0 &&<br>
>> event.type == ENET_EVENT_TYPE_CONNECT)<br>
>> {<br>
>> puts ("Connection to server succeeded.");<br>
>> }<br>
>> else<br>
>> {<br>
>> /* Either the 5 seconds are up or a disconnect event was */<br>
>> /* received. Reset the peer in the event the 5 seconds */<br>
>> /* had run out without any significant event. */<br>
>> enet_peer_reset (peer);<br>
>><br>
>> fprintf (stderr, "Connection to server failed.");<br>
>> exit (EXIT_FAILURE);<br>
>> }<br>
>><br>
>> while (true)<br>
>> {<br>
>> serviceResult = 1;<br>
>><br>
>> /* Keep doing host_service until no events are left */<br>
>> while (serviceResult > 0)<br>
>> {<br>
>> serviceResult = enet_host_service (client, &event, 0);<br>
>><br>
>> if (serviceResult > 0)<br>
>> {<br>
>> switch (event.type)<br>
>> {<br>
>> case ENET_EVENT_TYPE_CONNECT:<br>
>> printf ("A new client connected from %x:%u.\n",<br>
>> event.peer -> address.host,<br>
>> event.peer -> address.port);<br>
>><br>
>> event.peer->data = (void*)"New User";<br>
>> break;<br>
>><br>
>> case ENET_EVENT_TYPE_RECEIVE:<br>
>> printf ("A packet of length %u containing '%s' was "<br>
>> "received from %s on channel %u.\n",<br>
>> event.packet -> dataLength,<br>
>> event.packet -> data,<br>
>> event.peer -> data,<br>
>> event.channelID);<br>
>><br>
>> /* Clean up the packet now that we're done using it. */<br>
>> enet_packet_destroy (event.packet);<br>
>><br>
>> break;<br>
>><br>
>> case ENET_EVENT_TYPE_DISCONNECT:<br>
>> printf ("%s disconected.\n", event.peer -> data);<br>
>><br>
>> break;<br>
>> }<br>
>> }<br>
>> else if (serviceResult > 0)<br>
>> {<br>
>> puts ("Error with servicing the client");<br>
>> exit (EXIT_FAILURE);<br>
>> }<br>
>><br>
>> }<br>
>><br>
>><br>
>> printf ("Say> ");<br>
>> gets (message);<br>
>><br>
>> if (strcmp (message, "exit") == 0 ||<br>
>> strcmp (message, "quit") == 0) {<br>
>> break;<br>
>> }<br>
>><br>
>> if(strlen(message) > 0) {<br>
>> ENetPacket *packet = enet_packet_create (message, strlen<br>
>> (message) + 1, ENET_PACKET_FLAG_RELIABLE);<br>
>> enet_peer_send (peer, 0, packet);<br>
>> }<br>
>><br>
>> }<br>
>><br>
>> enet_peer_disconnect (peer, 0);<br>
>><br>
>> /* Allow up to 3 seconds for the disconnect to succeed */<br>
>> /* and drop any packets received packets */<br>
>> while (enet_host_service (client, & event, 3000) > 0)<br>
>> {<br>
>><br>
>> switch (event.type)<br>
>> {<br>
>> case ENET_EVENT_TYPE_RECEIVE:<br>
>> enet_packet_destroy (event.packet);<br>
>> break;<br>
>><br>
>> case ENET_EVENT_TYPE_DISCONNECT:<br>
>> puts ("Disconnection succeeded.");<br>
>> break;<br>
>> }<br>
>> }<br>
>><br>
>><br>
>> enet_host_destroy (client);<br>
>> enet_deinitialize ();<br>
>><br>
>> return 0;<br>
>><br>
>> }<br>
>> _______________________________________________<br>
>> ENet-discuss mailing list<br>
>> <a href="mailto:ENet-discuss@cubik.org">ENet-discuss@cubik.org</a><br>
>> <a href="http://lists.cubik.org/mailman/listinfo/enet-discuss" target="_blank">http://lists.cubik.org/mailman/listinfo/enet-discuss</a><br>
><br>
><br>
> _______________________________________________<br>
> ENet-discuss mailing list<br>
> <a href="mailto:ENet-discuss@cubik.org">ENet-discuss@cubik.org</a><br>
> <a href="http://lists.cubik.org/mailman/listinfo/enet-discuss" target="_blank">http://lists.cubik.org/mailman/listinfo/enet-discuss</a><br>
><br>
><br>
_______________________________________________<br>
ENet-discuss mailing list<br>
<a href="mailto:ENet-discuss@cubik.org">ENet-discuss@cubik.org</a><br>
<a href="http://lists.cubik.org/mailman/listinfo/enet-discuss" target="_blank">http://lists.cubik.org/mailman/listinfo/enet-discuss</a><br>
</div></div></blockquote></div><br>