libev TCP echo server example?

Chris Dew cmsdew at
Sat Mar 27 20:21:52 CET 2010

Here's some rather hastily hacked together code.  It can be compiled and run
on Ubuntu 9.10 AMD64 with the command:

gcc udp_echo.c -lev -o udp_echo && ./udp_echo

You can test it with netcat:

nc -u 3333

All the best,



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ev.h>

#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>

#define DEFAULT_PORT    3333
#define BUF_SIZE        4096

// Lots of globals, what's the best way to get rid of these?
int sd; // socket descriptor
struct sockaddr_in addr;
int addr_len = sizeof(addr);
char buffer[BUF_SIZE];

// This callback is called when data is readable on the UDP socket.
static void udp_cb(EV_P_ ev_io *w, int revents) {
    puts("udp socket has become readable");
    int bytes = recvfrom(sd, buffer, sizeof(buffer) - 1, 0, (struct
sockaddr*) &addr, &addr_len);

    // add a null to terminate the input, as we're going to use it as a
    buffer[bytes] = '\0';

    printf("udp client said: %s", buffer);

    // Echo it back.
    // WARNING: this is probably not the right way to do it with libev.
    // Question: should we be setting a callback on sd becomming writable
here instead?
    sendto(sd, buffer, bytes, 0, (struct sockaddr*) &addr, sizeof(addr));

int main(void) {
    int port = DEFAULT_PORT;
    puts("udp_echo server started...");

    // Setup a udp listening socket.
    sd = socket(PF_INET, SOCK_DGRAM, 0);
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = INADDR_ANY;
    if (bind(sd, (struct sockaddr*) &addr, sizeof(addr)) != 0)

    // Do the libev stuff.
    struct ev_loop *loop = ev_default_loop(0);
    ev_io udp_watcher;
    ev_io_init(&udp_watcher, udp_cb, sd, EV_READ);
    ev_io_start(loop, &udp_watcher);
    ev_loop(loop, 0);

    // This point is never reached.
    return EXIT_SUCCESS;

On 26 March 2010 08:57, Chris Dew <cmsdew at> wrote:

> Hi Christophe, I previously got UDP working fine with libev - if you don't
> get an example by the weekend, I'll spend twenty minutes putting together an
> example UDP echo server for you.
> All the best,
> Chris.
> On 26 March 2010 08:41, Christophe Meessen <christophe at> wrote:
>> Chris Dew a écrit :
>>  Could anyone point me at a simple TCP echo server example, using libev,
>>> that will work on Ubuntu 9.10?
>> I'm looking also for a similar example but using UDP.
>> Ch. Meessen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the libev mailing list