如何从另一个线程关闭阻塞的recv套接字? [英] How to close a blocking recv socket from another thread?

查看:114
本文介绍了如何从另一个线程关闭阻塞的recv套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我创建了一个服务器应用程序,许多客户端连接到它。当客户端硬件出现故障或电源突然熄灭时,客户端的套接字不会关闭套接字,服务器端的对应套接字被阻塞(被调用),并且不会关闭服务器资源。我通过测量时间来检测这些空闲插座,但我不知道如何关闭它们。我搜索谷歌,但我失败了。任何帮助,示例代码或链接赞赏。

mr.abzadeh

Hello, guys.
I have created a server application with many client connecting to it. when a client hardware fails or power suddenly goes off, the socket at client side does't close socket, and the correspoiding socket at server side is blocked(recv called), and will not close, taking server resources. I detect these idle sockets by measuring time, but I don't know how to close them. I searched google, but I failed. any help, sample code or link appreciated.
mr.abzadeh

推荐答案

我熟悉连接在没有套接字的情况下中断的情况被通知,让它处于你所描述的状态。

在你的情况下,你应该能够通过调用 ioctlsocket()将套接字改为非阻塞模式然后在退出 recv()时关闭它。



I am familiar with scenarios where the connection is interrupted without the socket being notified, leaving it in a state as you describe.
In your case, you should be able to change the socket into non-blocking mode by calling ioctlsocket() then close it upon exiting the recv() call.

//-------------------------
// Set the socket I/O mode: In this case FIONBIO
// enables or disables the blocking mode for the 
// socket based on the numerical value of iMode.
// If iMode = 0, blocking is enabled; 
// If iMode != 0, non-blocking mode is enabled.
int iResult;
u_long iMode = 0;

iResult = ioctlsocket(m_socket, FIONBIO, &iMode);



Soren Madsen


Soren Madsen


如果关闭套接字, recv()函数将立即返回零数据字节。当使用阻塞winsock套接字时,这是一个不错的小技巧。
If you close the socket, the recv() function will return immediately with zero data bytes. This is a decent little trick when using a blocking winsock socket.


这篇关于如何从另一个线程关闭阻塞的recv套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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