检测文件描述符是否已关闭 [英] Detecting if a file descriptor is closed

查看:124
本文介绍了检测文件描述符是否已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚如何检测套接字是否关闭时遇到了一些麻烦。在使用read()/ write()之外。如果可能的话,我也不想将套接字设置为非阻塞。

我写了一个函数,它读取一个字符(或字节,无论如何)并希望在一个字符串中使用它模拟阻塞和非阻塞方式。

它适用于等待正确的超时,但由于我使用ioctl()来检测是否有要读取的数据,我实际上不会调用读取()方法会告诉我套接字已关闭。


我以为有某种方法可以检测到可以写或可以阅读或在没有进入阻塞状态的文件描述符上,但是如果有的话,我找不到它。

展开 | 选择 | 换行 | 行号

解决方案

我的记忆在这个主题上真的很模糊,但我想我过去通过设置一个计时器,然后调用read()来完成这样的事情。然后,如果计时器到期,您将获得一个中断,这会使读取失败,并正确设置errno。这样你就可以开始读取,如果套接字关闭你也会得到那个错误你也不必担心阻塞太长时间。


我可以''为了我的生活,我记得如何在C中完成计时器,而我现在常用的参考资料目前并不方便。


@Plater


如果你不想在套接字上使用read()调用(如果套接字仍然打开并连接,read()可能会成功,那么字节应放在哪里?)你可以调用对该单个文件/套接字描述符的选择;如果套接字未连接且未准备好读取,则errno变量设置为EBADF,select()函数将返回-1(如果我没有记错的话)。


亲切的问候,


Jos


我正在查看select函数,但它似乎有很多结构和开销,我不知道甚至可以肯定它会告诉我我想要的东西。

它还说这是一个阻塞的电话

编辑:

FD_ISSET没有'在开放和关闭之间改变fd


再次编辑:

好​​的,所以选择可以超时,它的行为很像Poll()在C#中做(我想Poll只调用select)

似乎检查可写/异常永远不会改变,但是如果套接字关闭,那么检查可读是否会被读取。 br />
由于我已经检查了是否有数据,我可以将它们结合起来查找。


感谢您的帮助!

I''m having a bit of trouble figuring out how to detect if a socket is closed or not. Outside of using read()/write(). I also don''t want to set the socket to non-blocking if at all possible.
I wrote a function that reads a single char(or byte, whatever) and wanted to use it in a simulated blocking and nonblocking way.
It works fine for waiting for the correct timeouts, but since I use ioctl() to detect if there is data to be read, I won''t actually call the read() method that would show me the socket is closed.

I was thinking there was some way to detect "can write" or "can read" on a file descriptor without going into a blocking state, but I can''t find it if there is one.

Expand|Select|Wrap|Line Numbers

解决方案

My memory is really fuzzy on this topic, but I think I''ve accomplished something like this in the past by setting a timer, then calling read(). Then if the timer expires you get an interrupt, which makes the read fail, setting errno appropriately. That way you can just start the read and if the socket is closed you''ll get that error too and you wont have to worry about blocking for too long.

I can''t for the life of me remember how to accomplish timers in C and my usual references aren''t handy at the moment.


@Plater
If you don''t want to use a read() call on the socket (the read() might succeed if the socket is still open and connected and where should the byte be put?) you can call a select on that single file/socket descriptor; if the socket is not connected and not ready for a read the errno variable is set to EBADF and the select() function will return -1 (if I''m not mistaken).

kind regards,

Jos


I was looking at the select function, but it seemed to have a lot of structs and overhead and I wasn''t even sure it would tell me what i wanted.
It also said it was a blocking call
EDIT:
FD_ISSET doesn''t change between open and closed fd

EDIT AGAIN:
Ok, so select can be given a timeout, and it behaves a lot like Poll() does in C# (I imagine Poll just calls select)
Seems that checking for writable/exceptional never changes, but checking for readable will trigger if their is data to be read of if the socket is closed.
And since I have another check already to see if there is data, I can combine them to find out.

Thanks for all the help!


这篇关于检测文件描述符是否已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆