首先,先贴一个简单的使用libevent实现的定时器:
#include <stdio.h>
#include <event.h>
void onTime(int sock, short event, void *arg)
{
printf("Time Bomb!\n");
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
event_add((struct event*)arg, &tv);
}
int main()
{
event_init();
struct event evTimer;
evtimer_set(&evTimer, onTime, &evTimer);
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
event_add(&evTimer, &tv);
event_dispatch();
return 0;
}
再来一个简单的客户端和服务器通信的例子:
//客户端
#define _GNU_SOURCE
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <signal.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <event.h>
#include <event2/util.h>
#define BUFFSIZE 1024
int tcp_connect(char *ipaddress, char *port);
void socket_info(int, short, void*);
void cmd_read(int ,short, void *);
static int pipefd[2]; //用于终端输入
int main(int ac, char *av[])
{
if(ac != 3)
{
fprintf(stderr, "Usage: %s addr port\n",av[0]);
exit(1);
}
int listenfd = tcp_connect(av[1], av[2]);
if(listenfd < 0)
{
perror("Connecting Error");
exit(1);
}
struct event_base *base = event_init();
struct event evlisten;
event_set(&evlisten, listenfd, EV_READ | EV_PERSIST, socket_info, (void*)&evlisten);
event_base_set(base, &evlisten);
event_add(&evlisten, NULL);
if(pipe(pipefd) < 0)
{
perror("pipe error");
exit(1);
}
struct event cmd;
event_set(&cmd, STDIN_FILENO, EV_READ | EV_PERSIST, cmd_read, (void*)&listenfd);
event_base_set(base, &cmd);
event_add(&cmd, NULL);
event_base_dispatch(base);
return 0;
}
//客户端连接服务器
int tcp_connect(char *ipaddress, char *po)
{
char *ip = ipaddress;
int port = atoi(po);
int ret;
int save_errno;
struct sockaddr_in addr;
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_pton(AF_INET, ip, &addr.sin_addr);
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
return -1;
ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
if(ret < 0)
{
save_errno = errno;
close(sock);
errno = save_errno;
return -1;
}
evutil_make_socket_nonblocking(sock);
return sock;
}
//客户端接收到服务器的响应
void socket_info(int fd, short events, void *arg)
{
char buf[BUFFSIZE];
int ret;
struct event *ev = (struct event*)arg;
while(1)
{
bzero(buf, sizeof(buf));
ret = recv(fd, buf, BUFFSIZE, 0);
if(ret < 0)
{
if(errno == EAGAIN || errno == EWOULDBLOCK)
break;
else
{
perror("recv error");
return;
}
}
else if(ret == 0)
{
printf("server closed the connection\n");
close(fd);
event_del(ev);
exit(0);
}
else
printf("get %d bytes from the server :\n %s",ret,buf);
}
}
//客户端从终端读取数据发送到服务器
void cmd_read(int fd, short events, void *arg)
{
int lisfd = *(int *)arg;
splice(fd, NULL, pipefd[1], NULL, 32768, SPLICE_F_MOVE | SPLICE_F_MORE);
splice(pipefd[0], NULL, lisfd, NULL, 32768, SPLICE_F_MOVE | SPLICE_F_MORE);
}
//服务器端
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <signal.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <event.h>
#include <event2/util.h>
#define BUFFSIZE 1024
int tcp_listen(char *, char *);
void tcp_accept(int fd, short events, void *arg);
void sock_info(int fd, short events, void *arg);
int main(int ac, char *av[])
{
if(ac != 3)
{
fprintf(stderr, "Usage: %s addr port\n",av[0]);
exit(1);
}
int listenfd = tcp_listen(av[1], av[2]);
if(listenfd < 0)
{
perror("listen error");
exit(1);
}
struct event_base *base = event_init();
struct event ev_lis;
event_set(&ev_lis, listenfd, EV_READ | EV_PERSIST, tcp_accept, (void*)base);
event_base_set(base, &ev_lis);
event_add(&ev_lis, NULL);
event_base_dispatch(base);
return 0;
}
int tcp_listen(char *i, char *po)
{
char *ip = i;
int port = atoi(po);
int ret;
int save_errno;
struct sockaddr_in addr;
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_pton(AF_INET, ip, &addr.sin_addr);
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
return -1;
ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
if(ret < 0)
{
save_errno = errno;
close(sock);
errno = save_errno;
return -1;
}
ret = listen(sock, 10);
if(ret < 0)
{
save_errno = errno;
close(sock);
errno = save_errno;
return -1;
}
return sock;
}
//有新的连接
void tcp_accept(int fd, short events, void *arg)
{
struct event_base *base = (struct event_base *)arg;
int connfd;
struct sockaddr_in cli_addr;
socklen_t cli_len = sizeof(cli_addr);
connfd = accept(fd, &cli_addr, &cli_len);
if(connfd < 0)
{
perror("accept error");
return;
}
evutil_make_socket_nonblocking(connfd);
struct event *ev_conn = (struct event*)malloc(sizeof(struct event));
event_set(ev_conn, connfd, EV_READ | EV_PERSIST, sock_info, (void*)ev_conn);
event_base_set(base, ev_conn);
event_add(ev_conn, NULL);
}
//客户有信息发来
void sock_info(int fd, short events, void *arg)
{
struct event *evconn = (struct event*)arg;
char buf[BUFFSIZE];
int bytes = 0;
int ret;
printf("client send somthing to me!\n");
while(1)
{
memset(buf, '\0', BUFFSIZE);
ret = recv(fd, buf, BUFFSIZE, 0);
if(ret < 0)
{
if(errno == EAGAIN)
break;
else
{
perror("recv error");
return;
}
}
else if(ret == 0)
{
printf("client has closed the connecction\n");
close(fd);
event_del(evconn);
return;
}
else
{
bytes += ret;
printf("get %d bytes from the client : %s",ret,buf);
}
}
memset(buf, '\0', BUFFSIZE);
snprintf(buf, BUFFSIZE, "I have recevied your %d bytes.\n", bytes);
ret = strlen(buf);
if(send(fd, buf, strlen(buf), 0) != ret)
{
perror("send error");
return;
}
}