当前位置: 技术问答>linux和unix
如何判断socket断开连接
来源: 互联网 发布时间:2015-09-11
本文导语: 除了看recv返回值还有什么好的方法吗? 以前在windows下方法好多,甚至就连select();都能,getsockopt也行,可是在linux下我试过select不行. 还有别的方法吗,不知道getsockopt能判断吗,怎么判断。或者有更好的判断方法 | ...
除了看recv返回值还有什么好的方法吗?
以前在windows下方法好多,甚至就连select();都能,getsockopt也行,可是在linux下我试过select不行.
还有别的方法吗,不知道getsockopt能判断吗,怎么判断。或者有更好的判断方法
|
结果select返回1了,这没问题呀,
以下摘自
A socket is ready for reading if any of the following four conditions is true:
b.The read half of the connection is closed (i.e., a TCP connection that has received a FIN). A read operation on the socket will not block and will return 0 (i.e., EOF).
A socket is ready for writing if any of the following four conditions is true:
b.The write half of the connection is closed. A write operation on the socket will generate SIGPIPE
以下摘自
A socket is ready for reading if any of the following four conditions is true:
b.The read half of the connection is closed (i.e., a TCP connection that has received a FIN). A read operation on the socket will not block and will return 0 (i.e., EOF).
A socket is ready for writing if any of the following four conditions is true:
b.The write half of the connection is closed. A write operation on the socket will generate SIGPIPE
|
好像是读socket的信息,为0就表示断开了,随便找本UNIX网络编程的书上就有
|
select为什么不行?你是怎么试的?
如果在写socket时连接已经关闭,会产生SIGPIPE信号,这个也可以来判断连接已经关闭
如果在写socket时连接已经关闭,会产生SIGPIPE信号,这个也可以来判断连接已经关闭
|
close一个select监听就是不退出。我遇到这问题还没解决。
|
up
|
在linux下select 的第一个参数一定要填对, windows则不需要。
我曾经在这个问题上花了一天才弄明白。这是可能性最大的错误
我曾经在这个问题上花了一天才弄明白。这是可能性最大的错误