[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] UDP Event Dispatch



Hi,

I'm newbie to libevent.

Was trying to add multiple socket fds to event add. I'm able to read from only one socket.

Following is pseudo code:

    struct event ev, ev2;

    event_init();
    event_set(&ev, sock, EV_READ | EV_PERSIST, readEv, &ev);
    event_add(&ev, NULL);
    event_set(&ev2, sock, EV_READ | EV_PERSIST, readEv2, &ev2);
    event_add(&ev2, NULL);
    event_dispatch();

Attached the file.


Please help me to understand this.
--
Thanks & Regds
------------------------------------------------
Varun Rapelly,
9620637186



#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>

/* sockets */
#include <sys/types.h>
#include <sys/socket.h>

/* for non blocking */
#include <fcntl.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <pthread.h>

/* for libevent */
#include <event.h>

// #define MAKE_UDP 1

void readEv(int fd, short event, void* arg) {
  int len;
  char *buf = malloc(255);
  struct event *ev = arg;
#ifdef MAKE_UDP
  int l = sizeof(struct sockaddr);
  struct sockaddr_in cAddr;
#endif

  printf("DNS_read called with %s fd: %d, event: %d\n",
      event_get_method(), fd, event);

#ifdef MAKE_UDP
  len = recvfrom(fd, buf, 254, 0, (struct sockaddr*)&cAddr, &l);
#else
  len = recv(fd, buf, 254, 0);
#endif

  if (len == -1) {
#ifdef MAKE_UDP
    perror("recvfrom()");
#else
    perror("recv");
#endif
    return;
  } else if (len == 0) {
    printf("Connection Closed\n");
    return;
  }

  buf[len] = '\0';

  printf("READ: %s\n", buf);

  sendto(fd, buf, len, 0,  (struct sockaddr*)&cAddr, l);
}
void readEv2(int fd, short event, void* arg) {
  int len;
  char *buf = malloc(255);
  struct event *ev = arg;
#ifdef MAKE_UDP
  int l = sizeof(struct sockaddr);
  struct sockaddr_in cAddr;
#endif

  printf("DNS_read called with %s fd: %d, event: %d\n",
      event_get_method(), fd, event);

#ifdef MAKE_UDP
  len = recvfrom(fd, buf, 254, 0, (struct sockaddr*)&cAddr, &l);
#else
  len = recv(fd, buf, 254, 0);
#endif

  if (len == -1) {
#ifdef MAKE_UDP
    perror("recvfrom()");
#else
    perror("recv");
#endif
    return;
  } else if (len == 0) {
    printf("Connection Closed\n");
    return;
  }

  buf[len] = '\0';

  printf("READ: %s\n", buf);

  sendto(fd, buf, len, 0,  (struct sockaddr*)&cAddr, l);
}

int main() {
  int sock, sock2;
  int yes = 1;
  int len = sizeof(struct sockaddr);
  struct sockaddr_in addr;

#ifdef MAKE_UDP
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
#else
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
#endif
      perror("socket");
      return 1;
    }

#ifdef MAKE_UDP
  if ((sock2 = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
#else
    if ((sock2 = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
#endif
      perror("socket");
      return 1;
    }

    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
      perror("setsockopt");
      return 1;
    }
    if (setsockopt(sock2, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
      perror("setsockopt");
      return 1;
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(5555);
    addr.sin_addr.s_addr = INADDR_ANY;
    bzero(&(addr.sin_zero), 8);

    if (bind(sock, (struct sockaddr*)&addr, len) < 0) {
      perror("bind");
      return 1;
    }
    addr.sin_family = AF_INET;
    addr.sin_port = htons(5556);
    addr.sin_addr.s_addr = INADDR_ANY;
    bzero(&(addr.sin_zero), 8);

    if (bind(sock2, (struct sockaddr*)&addr, len) < 0) {
      perror("bind");
      return 1;
    }

#ifndef MAKE_UDP
    if (listen(sock, 16) < 0) {
      perror("listen");
      return 1;
    }
#endif
#ifndef MAKE_UDP
    if (listen(sock2, 16) < 0) {
      perror("listen");
      return 1;
    }
#endif

    printf("Listening now...\n");

    struct event ev, ev2;

    event_init();

    event_set(&ev, sock, EV_READ | EV_PERSIST, readEv, &ev);
    event_add(&ev, NULL);
    event_set(&ev2, sock, EV_READ | EV_PERSIST, readEv2, &ev2);

    event_add(&ev2, NULL);
    //event_add(&ev2, NULL);

    event_dispatch();

    printf("Done!\n");
    return 1;
  }