ENet Tutorial

Introduction

ENet is a smart and powerful library to send packets using UDP. It can send those package even reliably. It relieves you from creating sockets to do communication. Nothing in this tutorial is new, that is not explained in the standard tutorial of enet.bespin.org or linuxjournal. First I could not figure out how to combine different parts of the tutorial to create a client and a server. When I read its usage on some mailing list it became clear to me how it would work. This tutorial will try to explain a simple chat application I found there.

Getting Started

However you approach later, you still need to have some of the basic constructs right before sending and receiving packets. This applies to both client and server.

a. Initialize enet using enet_initialize function.b. Create a host using enet_host_create function.
This function takes NULL as the first parameter if it is a client and ENetAddress if it is a server. These objects are structures, because we don’t have classes in c. In case of server you can define number of clients as second parameter. That number is going to be 1 if it is a client code. Other two parameters are incoming and outgoing bandwidth. If you choose this value as 0 you send or receive unlimited data. In case of client you can restrict incoming to 56k using 57600/8 and outgoing to 14k using using 14400/8 values. You can bind hosts to specific address and port using enet_address_set_host function. In case of client it becomes necessary to specify where you want to connect to, but for server you can accept any address using ENET_HOST_ANY as value for host property of address structure, before creating a host using enet_host_create function.c. Connect and use service.
The core function for establishing communication is enet_host_service. This function takes three parameters i.e ENetHost, ENetEvent and time in milliseconds to wait for event. We use ENetEvent structure to get packet, address and port of peer, channelID etc. After calling enet_host_service we get a return type of 0 if no event was called. This value can be used to terminate the service. The type field of event structure contains different values that signifies what type of event occured. Typical values are ENET_EVENT_TYPE_NONE (It is 0 for no data), ENET_EVENT_TYPE_CONNECT (When connection with either server or client happened), ENET_EVENT_TYPE_RECEIVE (when data is received), ENET_EVENT_TYPE_DISCONNECT (when connection could not be established with other party).

d. Send some data
Sending data can be done by wrapping it in ENetPacket object. We create a packet using enet_packet_create and send it using enet_peer_send function. The function enet_packet_create can take parameters like ENET_PACKET_FLAG_RELIABLE which specifies we want a reliable connection even over UDP.

We can use gets(char *) method to fill in a character buffer and call method enet_packet_create as

Here we are sending our peer a packet that contains the message we wanted to send.

e. If we want to destroy the packet and start over again, we can do so using enet_packet_destroy(event.packet). We can call enet_host_service as many times we want, because it pauses only the specified amount of time we provide as the third parameter to fill in our event structure. We can use this event structure to get all the information and data we need from other part of communicating host.

Note: One difference this code has from the tutorial is that it only uses one enet_host_service function. If the server has to send individual message it too has to create packet and send as the client did, however we have used enet_host_broadcast method to broadcast everything to the client. One thing that makes this application a little of the track is that the server sends last filled message to the client who is currently interacting with it. I will surely work on it so that server sends timely message and all the client capture that. This way we have have standard chat application.

Conclusion
Through this tutorial we saw how to use enet library to send and receive message. Good Luck on the tutorial.

When with the above code I try to compile the .c code from linux I am not able to initialize enet and also not able to create enet server host.
The file which I got after compile is having this errors : “An error occured while initializing ENet” and “An error occured while trying to create an ENet server host”.

I don’t have any problems getting it to work. E.g I installed enet library and assuming pkg-config is installed, everything works as it should. E.g Here is my compilation and execution message (both server and client)

I guess you have already tried using -lenet or pkg-config command as above and you have successfully compiled the code. Above that, I can’t really say where the problem lies in case of execution of the program.

To install enet, you should use the package manager of the distribution you are using. In case of ubuntu (I think ubuntu 12.04 and above) the command to install enet is

sudo apt-get install libenet-dev

From your comments, it seems you are trying to install using the source code. Unless, you learn more about linking and installing library using specific parameters, use above command to install enet.

Now, lets talk about your command to compile the code.

With -c switch you are only creating object files rather than the final binary. Also, you are using -I switch to include the headers while compilation. It has nothing to do with how the executable code is going to be produced. If I inspect server file (binary) after compilation (gcc -o server server.c -lenet), I get following output.

Hi .
Thank you for that example code .
I made some mods on it , to allow a single program to be client or server with the command line.
But the most important change ( and fix ? ) is about your offtrack use of broadcast(…) .
After some tries , i had success with : ( on windows , vstudio9)