【网络开源框架之libev使用实例】弓背霞明剑照霜,秋风走马出咸阳。这篇文章主要讲述网络开源框架之libev使用实例相关的知识,希望能为你提供帮助。
// libev.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include < stdio.h>
#include < sys/epoll.h>
#include < sys/socket.h>
#include < netinet/in.h>
#include < arpa/inet.h>
#include < netdb.h>
#include < sys/types.h>
#include < signal.h>
#include < unistd.h>
#include < fcntl.h>
#include < string.h>
#include < errno.h>
#include < stdlib.h>
#include < pthread.h>
#include < ev.h>
#define MAXLEN 1023
#define PORT 8003
#define ADDR_IP "127.0.0.1"
int socket_init();
void accept_callback(struct ev_loop *loop, ev_io *w, int revents);
void recv_callback(struct ev_loop *loop, ev_io *w, int revents);
void write_callback(struct ev_loop *loop, ev_io *w, int revents);
int main(int argc, char** argv)
int listen;
ev_io ev_io_watcher;
listen = socket_init();
struct ev_loop *loop = ev_loop_new(EVBACKEND_EPOLL);
ev_io_init(& ev_io_watcher, accept_callback, listen, EV_READ);
ev_io_start(loop, & ev_io_watcher);
ev_loop(loop, 0);
ev_loop_destroy(loop);
return 0;
int socket_init()
struct sockaddr_in my_addr;
int listener;
if ((listener = socket(AF_INET, SOCK_STREAM, 0)) == -1)
perror("socket");
exit(1);
else
printf("SOCKET CREATE SUCCESS!\\n");
//setnonblocking(listener);
int so_reuseaddr = 1;
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, & so_reuseaddr, sizeof(so_reuseaddr));
bzero(& my_addr, sizeof(my_addr));
my_addr.sin_family = PF_INET;
my_addr.sin_port = htons(PORT);
my_addr.sin_addr.s_addr = inet_addr(ADDR_IP);
if (bind(listener, (struct sockaddr *) & my_addr, sizeof(struct sockaddr)) == -1)
perror("bind error!\\n");
exit(1);
else
printf("IP BIND SUCCESS,IP:%s\\n", ADDR_IP);
if (listen(listener, 1024) == -1)
perror("listen error!\\n");
exit(1);
else
printf("LISTEN SUCCESS,PORT:%d\\n", PORT);
return listener;
void accept_callback(struct ev_loop *loop, ev_io *w, int revents)
int newfd;
struct sockaddr_in sin;
socklen_t addrlen = sizeof(struct sockaddr);
ev_io* accept_watcher = malloc(sizeof(ev_io));
while ((newfd = accept(w-> fd, (struct sockaddr *)& sin, & addrlen)) < 0)
if (errno == EAGAIN || errno == EWOULDBLOCK)
//these are transient, so dont log anything.
continue;
else
printf("accept error.[%s]\\n", strerror(errno));
break;
ev_io_init(accept_watcher, recv_callback, newfd, EV_READ);
ev_io_start(loop, accept_watcher);
printf("accept callback : fd :%d\\n", accept_watcher-> fd);
void recv_callback(struct ev_loop *loop, ev_io *w, int revents)
char buffer[1024] =0 ;
int ret = 0;
//ev_io write_event;
loop:
ret = recv(w-> fd, buffer, MAXLEN, 0);
if (ret > 0)
printf("recv message :%s\\n", buffer);
else if (ret == 0)
printf("remote socket closed!socket fd: %d\\n", w-> fd);
close(w-> fd);
ev_io_stop(loop, w);
free(w);
return;
else
if (errno == EAGAIN || errno == EWOULDBLOCK)
goto loop;
else
printf("ret :%d ,close socket fd : %d\\n", ret, w-> fd);
close(w-> fd);
ev_io_stop(loop, w);
free(推荐阅读
- 网络开源框架之libevent使用实例
- google开源库glog源码实现分析
- win32下多线程同步方式之临界区,互斥量,事件对象,信号量
- ACE之Proactor模式使用实例
- ACE之Reactor模式使用实例
- C++使用ICE实现两台主机通信实例
- #导入Word文档图片# Linux下信号处理
- opencv 绘图及交互(python)
- linux下遍历目录和文件,删除过期天数目录或文件shell脚本