- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
根据Linux man pages,select
支持三种唤醒事件:
将监视
readfds
,以查看是否有字符可用于读取writefds
将被监视以查看是否有空间可用于写入exceptfds
将被监视是否存在异常readfds
被使用。
readfs
集中收到了,而在
writefds
集中没有收到。为了避免阻塞写操作,我通常将套接字的fd设置为非阻塞模式。然后,如果
send
失败,我可以将数据排队到某个内部缓冲区中,然后将其发送出去(这意味着-下次当带
select()
的
readfs
唤醒时)。但这似乎很危险-如果下一次
readfs
唤醒要晚得多,并且要写入的数据只是放在我们的缓冲区中,从理论上讲永远等待怎么办?
writefds
:
Using Sockets and Socket Streams,请参阅“使用纯POSIX代码处理事件”一节,并引述:
Call select in a loop, passing two separate copies of that file descriptor set (created by calling FD_COPY) for the read and write descriptor sets.
writefds
就是因为它是“正确的官方方式”,或者还有其他方法可以不用writefds
来处理套接字写操作?苹果的建议对我来说似乎很可疑。如果我们从一开始就将套接字放入writefds
中,然后一段时间不对其进行写入,那么select()
不会仅因为套接字可写而立即唤醒(那是因为我们尚未对其进行写入)吗? exceptfds
-我尚未看到将其与TCP套接字一起使用的任何示例。我已经读过它用于带外数据。这是否意味着如果我仅处理主流的Internet通信(例如HTTP,音频/视频流,游戏服务器等),就可以忽略TCP套接字的exceptfds
? 最佳答案
Is Apple recommending to use writefds just because it's "the right official way" or maybe there are other approaches how to deal with socket writes without writefds?
If we put the socket into writefds from the very start and then don't write to it for some time, won't select() wake up immediately just because the socket is writable (and that's because we haven't written to it yet)?
About exceptfds -I haven't yet seen any examples using it with TCP sockets. I have read that it is used for out-of-band data.
Then if send fails, I could just queue the data into some internal buffer and send it out later (which means - next time when select() with readfs wakes up). But this seems dangerous - what if next readfs wakeup comes much later and the data to be written just sits in our buffer waiting, theoretically, forever?
关于c - select()-带有非阻塞TCP套接字的writefds和exceptfds的实际使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41985774/
select系统调用采用 3 个文件描述符集来监视文件描述符上的可读/可写和“异常”的 fds。 我的选择 man page没有太多关于 exceptfd 描述符集的说明。它是干什么用的;它可以并且将
我是一名优秀的程序员,十分优秀!