【问题标题】:libevent cannot read an open socket descriptorlibevent 无法读取打开的套接字描述符
【发布时间】:2013-03-21 07:10:43
【问题描述】:

最近,我有一个使用 libevent 的小型套接字服务器程序。

总之,它做了以下工作。

void read_function(int fd, short event, void* arg) {
    printf("callback is called!\n");

    // read from fd, and send a reply to fd!
}

void accept_thread_function() {
    int fd = accept(...);
    struct event* ev_read = new struct event();
    memset(ev_read, 0, sizeof(struct event));
    event_set(ev_read, fd, EV_READ|EV_PERSIST,read_function,ev_read);
    event_add(ev_read, 0);
}

int main() {
    event_init();
    THREAD a = start 'accept_thread_function' as a thread;
    event_dispatch();
    THREAD::join(a);
}

问题是,read_function 永远不会被调用。

传入连接被正确接受。 (叹气)

我正在等待您对这个问题的友好评论。

提前谢谢你。

【问题讨论】:

    标签: libevent


    【解决方案1】:

    首先,看看 Nick Mathewson 撰写的关于 libevent 的(免费)精彩书籍:

    http://www.wangafu.net/~nickm/libevent-book/Ref1_libsetup.html#_locks_and_threading

    然后,您需要确保在调用 event_dispatch() 之前添加事件。

    【讨论】:

    • 谢谢雷米。这是一个很棒的评论。
    • 无论如何,我已经转向使用 libev 而不是 libevent。 libev 给了我很多可靠的结果,而且很容易使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 2016-05-08
    相关资源
    最近更新 更多