本文介绍了如何检查客户端是否通过Winsock在C ++中断开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查客户端是否通过Winsock在C ++中断开连接?

How can I check if a client disconnected through Winsock in C++?

推荐答案

如果您在阻塞模式下调用recv并返回0字节读取,套接字已断开连接,否则等待接收字节。

if you call recv in blocking mode and it returns with 0 bytes read, the socket has disconnected, else it wait for bytes to be received.

查看此

示例来自页面上的选择。

example from select on this page.

int nRet;

if(( nRet = select( 0, &fdread, NULL, NULL, NULL )) == SOCKET_ERROR )
{
	// Error condition
	// Check WSAGetLastError
}

if( nRet > 0 )
{
	// select() will return value 1 because i m using only one socket
	// At this point, it should be checked whether the
	// socket is part of a set.

	if( FD_ISSET( s, &fdread ))
	{
		// A read event has occurred on socket s
	}
}

这篇关于如何检查客户端是否通过Winsock在C ++中断开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:51
查看更多