select模式改成epoll模式
本帖最后由 rookie睿 于 2014-11-11 17:44 编辑截取原函数如下#ifdef _WIN32
if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) == JB_INVALID_SOCKET)
#else
if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
#endif
{
return(JB_INVALID_SOCKET);
}
/* wait for connection to complete */
FD_ZERO(&wfds);
FD_SET(fd, &wfds);
tv->tv_sec= 30;
tv->tv_usec = 0;
log_error(LOG_LEVEL_GPC, "int connect() : before select");
/* MS Windows uses int, not SOCKET, for the 1st arg of select(). Wierd! */
if (select((int)fd + 1, NULL, &wfds, NULL, tv) <= 0)
{
log_error(LOG_LEVEL_GPC, "in select");
close_socket(fd);
return(JB_INVALID_SOCKET);
}我改为epoll 如下
#ifdef _WIN32
if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) == JB_INVALID_SOCKET)
#else
if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0)
#endif
{
return(JB_INVALID_SOCKET);
}
epollfd = epoll_create(1024);
ev.data.fd = fd;
ev.events = EPOLLIN | EPOLLET;
int epollctl_value = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
log_error(LOG_LEVEL_GPC, "epollctl_value == %d", epollctl_value);
tv->tv_sec= 30;
tv->tv_usec = 0;
if( epoll_wait(epollfd, events, 1024, -1) <= 0)
{
log_error(LOG_LEVEL_GPC, "in epoll_wait epollfd == %d", epollfd);
close_socket(fd);
return(JB_INVALID_SOCKET);
}select情况下 if (select((int)fd + 1, NULL, &wfds, NULL, tv) <= 0)这句是可以监听到文件描述符的
但是在epoll情况下 if( epoll_wait(epollfd, events, 1024, -1) <= 0) 监听不到 一直处于阻塞状态求解epoll_wait为什么监听不到? 你的程序在创建socket后怎么没有bind和listen?
你不listen别人就接入不进来呀,那socket fd就没的事件呀。
回复 2# Tinnal
这位是客户端的部分 中间我没有贴上来connect的部分 问题症结是 我只修改了源码中select( )中部分 select可以监听到 但是epoll_wait( )却不行:dizzy:
页:
[1]