[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[Libevent-users] UDP Event Dispatch
- To: libevent-users@xxxxxxxx
- Subject: [Libevent-users] UDP Event Dispatch
- From: Varun Rapelly <varunrapelly@xxxxxxxxx>
- Date: Fri, 27 Sep 2013 05:33:06 +0530
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: libevent-users-outgoing@xxxxxxxx
- Delivered-to: libevent-users@xxxxxxxx
- Delivery-date: Thu, 26 Sep 2013 20:03:14 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=RrEB9vv+ChwQpSw8ePQGa86gNWkGTGsvqe8VjEzm5cs=; b=0jvaJTI5+3KOGrQV5Hjacaif4gTvqjJQ9cxQGPDIACMU2pehlHx/HslsouWhgUSeaZ M1uzUUxjy2b3R7Z3IIol0ZrsHJ6g+sXIDTJb1JLU5UhOowZ/Wn3jZmE8cCZT84HbU/Jo nhMck6RAb7/7R80D4Ab5K+bxhXh8IDSptLCnxguYK2GHutT3uk2m6r2DH2NWVtrz9Qly BoCB/uYF+E7mp5w35iaWL04N+zUEYuIoqDmhbZsPmvo8qDcmepr9Ln8qzQxwMaanHrXk S4VERJwp5xDv8UBP2JeNstmlLl9l4mLmYhIgk5RelljRRpHaVX9AigHOM/KrmJJh1WHb eJFQ==
- Reply-to: libevent-users@xxxxxxxxxxxxx
- Sender: owner-libevent-users@xxxxxxxxxxxxx
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;
}