gpt4 book ai didi

c - libevent 和非阻塞套接字

转载 作者:太空狗 更新时间:2023-10-29 17:25:20 26 4
gpt4 key购买 nike

据我所知,为了使用 libevent 监控套接字,首先应使用正确的参数调用 event_set()

libevent 文档指出 event_set() 的 event 参数可以是 EV_READ 或 EV_WRITE。并且此事件参数是要注意的事件。

但是EV_READ和EV_WRITE对应的是什么socket事件呢?我的意思是我将如何监视连接状态的变化,而不是监视传入消息?

最佳答案

我找到了 this site在 libevent 的文档方面表现出色。在 the page dealing with events ,对不同事件标志的实际含义有一个很好的概述。从该链接:

  • EV_READ : This flag indicates an event that becomes active when the provided file descriptor is ready for reading.

  • EV_WRITE : This flag indicates an event that becomes active when the provided file descriptor is ready for writing.

  • EV_SIGNAL : Used to implement signal detection.

  • EV_PERSIST : Indicates that the event is persistent.

  • EV_ET : Indicates that the event should be edge-triggered, if the underlying event_base backend supports edge-triggered events. This affects the semantics of EV_READ and EV_WRITE.

因此,明确地回答您的问题:EV_READ 对应于可以从套接字或 bufferevent 中读取数据,据我所知,它们是 libevent 套接字等价物。 EV_WRITE 对应于准备好将数据写入其中的套接字/缓冲区事件。您可以设置读/写回调以使用 cb 参数实际执行数据读写


结构事件 *event_new(struct event_base *base, evutil_socket_t fd,
简而言之,event_callback_fn cb, void *arg);

但是,如果您正在使用 libevent 进行套接字 IO,您可能真的要考虑使用 buffer events - 它们是我在我的一个项目中使用的,snot_mon,which you can check out over on github .

关于c - libevent 和非阻塞套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6239801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com