如何判断socket断开连接

jiniang 2005-02-25 09:51:52


除了看recv返回值还有什么好的方法吗?
以前在windows下方法好多,甚至就连select();都能,getsockopt也行,可是在linux下我试过select不行.
还有别的方法吗,不知道getsockopt能判断吗,怎么判断。或者有更好的判断方法
...全文
476 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiniang 2005-03-03
  • 打赏
  • 举报
回复
那我到底怎么才能判断出sock关闭了啊 ,up
yanghuajia 2005-02-28
  • 打赏
  • 举报
回复
在linux下select 的第一个参数一定要填对, windows则不需要。
我曾经在这个问题上花了一天才弄明白。这是可能性最大的错误
tukey 2005-02-28
  • 打赏
  • 举报
回复
up
ringerxyz 2005-02-28
  • 打赏
  • 举报
回复
结果select返回1了,这没问题呀,
以下摘自<<Unix Network Programming Volume 1,Third Edition The Sockets Networking API>>
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
jiniang 2005-02-27
  • 打赏
  • 举报
回复
up
mwjx 2005-02-26
  • 打赏
  • 举报
回复
好像是读socket的信息,为0就表示断开了,随便找本UNIX网络编程的书上就有
jiniang 2005-02-26
  • 打赏
  • 举报
回复
我就是这样试的,当我关闭server端时,结果select返回1了,没返回错误!!
while(cmpp_recv_run)
{
FD_ZERO(&readfds);
FD_SET(sock,&readfds);
struct timeval tv;
tv.tv_sec=3; //3s timeout
tv.tv_usec=0;
int ret=select(sock+1,&readfds,NULL,NULL,&tv);
if(ret<0) //socket error
{
printf("sock close");
}
}
ringerxyz 2005-02-26
  • 打赏
  • 举报
回复
select为什么不行?你是怎么试的?
如果在写socket时连接已经关闭,会产生SIGPIPE信号,这个也可以来判断连接已经关闭
sharkhuang 2005-02-26
  • 打赏
  • 举报
回复
close一个select监听就是不退出。我遇到这问题还没解决。

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧