套接字选择方法不会导致我期望的行为

套接字选择方法不会导致我期望的行为

本文介绍了套接字选择方法不会导致我期望的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我写了一些服务器代码,其中包含一种处理来自客户端的数据的方法.它具有一个select方法,用于检查要从套接字读取的数据,并且具有超时值.客户端每秒向我的服务器发送一条消息.如果我将代码设置为运行,然后停止客户端,则我希望select方法返回值-1.但这似乎没有发生.如果客户端关闭,返回值似乎是> 0,但是当调用recv方法时,接收到的字节为零.

我将代码放在下面,并附有评论和问题.可以请确切解释发生了什么.我可以让我的代码执行我想做的事情,但不能达到我的期望,所以我真的不明白为什么我的代码行得通!非常感谢.

Hi,

I have written some server code which has a method which handles the receiving of data from the client. It has a select method with checks for data to read from the socket and has a timeout value. The client sends a message to my server every second. If I set my code running, then stop the client, I would have expected the select method to return a value of -1. But this doesn''t appear to happen. If the client closes, the return value appears to be > 0 but then when the recv method is called, the bytes received is zero.

I have put the code below, with comments and questions. Could somebody please explain exactly what is going on please. I can get my code to do what i want, but not how I expected, so I really don''t understand why my code works! Many, many thanks.

//initialise the fd_set rxSet (file descriptor set) values
FD_ZERO(&rxSet);
//add m_clientSocket to the set of file descriptors in rxSet
FD_SET(m_clientSocket, &rxSet);
maxFD = m_clientSocket;//not needed, just for comaptibility with linux select()

//wait for the specified timeOutVal to see if there is any data to read
//on the m_clientSocket
sockVal = select(maxFD, &rxSet, NULL, NULL, &timeOutVal);

//see if there is some data to read on the socket
if(sockVal > 0)
{
	//read the data from the socket
	int bytesReceived = recv(m_clientSocket, recvBuf, recvBufLen - 1, 0);
	//add a null terminator at the end of the data to avoid storing an garbage
	recvBuf[bytesReceived] = '\0';
	//check if there was an error on the socket
	if(bytesReceived == -1)
	{
                //this does not get reached if the client disconnects
                //what would cause this code to be reached?
		printf("server socket error\n");
		break;
	}
	if(bytesReceived > 0)
	{
		//bytes were received
		//read the message, form the response and send response to controller
		RespondToAllMessagesFromController(recvBuf);

		noDataReceivedCount = 0;//reset the count
	}
	if(bytesReceived == 0)
	{
	        //this is reached if the client disconnects - but I don't understand 
                //why because i would have thought that sockVal would not be > 0 in
                //this case

		break;//leave while loop, leave Digitiser running		
	}
}
else if(sockVal < 0)
{
	//there was an error on the socket - but this doesn't get reached if the
        //controller disconnects. What would cause this to be reached?
	//even if the if(bytesReceived == 0){} above is removed.
	int test = 0;
}
else //if(sockVal == 0) because select timed out
{
       //this gets reached when the socket hasn't disconnected but no messages arrive,
       //as I would expect
}

推荐答案


这篇关于套接字选择方法不会导致我期望的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:17