following code is my own simple udp logger program, hope it will help somehow.
--code start here-- /* simple Libevent-based udp logger server * * Usage: udplogger port logfile */ #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <unistd.h> #include <netinet/in.h> #include <limits.h> #include <netdb.h> #include <arpa/inet.h> #include <sys/stat.h> #include <fcntl.h> #include <time.h> #include <sys/time.h> /* libevent support */ #include <event.h> #define DEFAULTPORT 13333 #define VERSION "0.1 build 20060711" #define BUFFERLEN 32768 void udplogger(const int fd, short which, void *arg) { struct sockaddr_in server; int recvd, structlength, i, *p; int sockfd, logfd; char buffer[BUFFERLEN+1]; char time_buffer[1024]; time_t now; p = (int *) arg; i = p[0]; if (fd != i) return; sockfd = fd; logfd = p[2]; structlength = sizeof(server); memset(buffer,0,sizeof(buffer)); memset(time_buffer,0,sizeof(time_buffer)); recvd = recvfrom(sockfd, &buffer, sizeof(buffer)-1, 0, (struct sockaddr *) &server, &structlength); if (recvd == -1) { return; } buffer[recvd]=0; now = time(NULL); strncpy(time_buffer, ctime(&now), sizeof(time_buffer)-1);; time_buffer[strlen(time_buffer)-1] = '\0'; /* strip last \n */ strcat(time_buffer, ": "); strcat(time_buffer, inet_ntoa(server.sin_addr)); strcat(time_buffer, " -> "); write(logfd, time_buffer, strlen(time_buffer)); write(logfd, buffer, recvd); return ; } int main(int argc, char **argv) { struct event ev_master; int port, sockfd, i, logfd, fd[2]; char file[BUFFERLEN]; struct sockaddr_in sin; umask(0022); if (argc > 1) strncpy(file, argv[1], sizeof(file)-1); else { printf("SIMPLE UDP LOGGER VER %s\n", VERSION); printf("Usage: udplogger file port\n port: bind port, default 13366\n file: log receive message to logfile\n"); return 1; } logfd = open(file, O_WRONLY|O_CREAT|O_NOFOLLOW|O_APPEND); if (logfd == -1) { printf("Can't write to %s because of %s.\n", file, strerror(errno)); return 1; } if (argc > 2 && strlen(argv[2])) port = atoi(argv[2]); else port = DEFAULTPORT; sockfd = socket(AF_INET, SOCK_DGRAM, 0); memset((char *) &sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(port); if (bind(sockfd, (struct sockaddr *) &sin, sizeof(sin))) { /*can't bind*/ printf("can't bind!\n"); return 1; } /* signal(SIGCHLD, SIG_IGN); signal(SIGHUP, SIG_IGN); signal(SIGTERM, freeipaddress); signal(SIGINT, SIG_IGN); */ printf("\nbind to 0.0.0.0:%d\tlog to %s\n", port, file); event_init(); fd[0] = sockfd; fd[1] = logfd; event_set(&ev_master, sockfd, EV_READ|EV_PERSIST, udplogger, &fd); event_add(&ev_master, 0); event_loop(0); //close open sockets close(sockfd); close(logfd); return 0; } --code end-- Regards, QHY On 8/25/06, Raja Subramanian <[EMAIL PROTECTED]> wrote:
Hi All, I'm writing an application that uses libevent for async io. I can handle connection oriented sockets (tcp) without any problems. The same app also needs to handle GRE sockets that are not connection oriented. I'm having trouble getting started with the latter. A working app which manages gre or udp connections using libevent would be fantastic. Can someone please point me towards code examples/apps that I can learn from? Thanks for any help/suggestions! - Raja _______________________________________________ Libevent-users mailing list Libevent-users@monkey.org http://monkey.org/mailman/listinfo/libevent-users
_______________________________________________ Libevent-users mailing list Libevent-users@monkey.org http://monkey.org/mailman/listinfo/libevent-users