title: Broadcast TCP-Server with libevent
body: <p>I am looking for some sample code of a dead simple libevent-based TCP-Server which broadcasts incoming Messages to all connected clients. On the Web I only found TCP-Servers which echoes back messages.</p>
  
  <p>One echo example if found is on the bottom of this page <a href="http://www.wangafu.net/~nickm/libevent-book/Ref8_listener.html" rel="nofollow noreferrer">http://www.wangafu.net/~nickm/libevent-book/Ref8_listener.html</a></p>
  
  <p>I am sure that its not so difficult to change the code provided on this Site, so that messages are brodcast to all connected clients, but I don't know how.</p>
  
  <p>Could someone advise me?</p>
  
  <p>EDIT: Yes, some kind of a chat server. It seams i need to do sth like this:</p>
  
  <pre><code>void server_read_cb(struct bufferevent *bev, void *data)
  {
         struct evbuffer *input = bufferevent_get_input(bev);
         struct evbuffer *output = bufferevent_get_output(bev);
  
         char *mem = malloc(len); /* XXX: check for malloc failure */
  
         evbuffer_remove(input, mem, len);
         "for every connection conn" {
             /* XXX you'll need to implement a real connection list;
                the above isn't real C. */
                 evbuffer_add(bufferevent_get_output(conn), mem, len);
         }
         free(mem);
  }
  </code></pre>
  
  <p>But i can't put this to work.</p>