套接字选择方法不会导致我期望的行为 [英] socket select method not causing behaviour i would expect

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

问题描述



我写了一些服务器代码,其中包含一种处理来自客户端的数据的方法.它具有一个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
}

推荐答案



当通读它时,似乎您在选择中确实做错了.因为在(我假设)win32中不需要maxFD值,所以最好将其保留为0或1或类似的值.您当前正在将其设置为客户端套接字的地址,因为它被忽略了(或者应该被忽略),但我看不出它为什么会出错.

另一条建议;您正在使用recv的返回字节数在数组中设置"\ 0",并在其下一行得出以下结论:该值可能会比0低一点,这自然是有效声明.真可惜它实际上会发生,因为这对数组前面的内存的影响不是很好.

希望这会有所帮助,

AT
Hi,

when reading through it, it seems that you are really doing something wrong with the select. As the maxFD value is not required in (i assume) win32 you can better leave it at 0 or 1 or something like that; you are currently setting it to the address of the client socket, as it is(or should be) ignored I fail to see why it would go wrong though.

one other piece of advice; you are using the returned byte count of the recv to set a ''\0'' in an array and one line below that you come to the conclusion that the value may turn out to be a bit lower than 0, which naturally is a valid statement. It would be a shame it it would actually happen though as that has a not so nice effect on the the memory preceding your array.

Hope this helps,

AT


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

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