WINXP與 WIN7 32位下,客戶端使用的UDP模式,
nTimeout = 10;
nErr = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char*)&nTimeout, sizeof(nTimeout));
在UDP接收的時候
CCpuTimer T0;
T0.Start(); 開始計時器
//----UDP socket 設定為阻塞模式接收
nRet = recvfrom(g_stNet2Can.sockDevice, (char*)byteBuffer, nBytes, 0, (struct sockaddr*)&sin, &nLen);
if (nRet != nBytes)
{
nRet = WSAGetLastError();
//if (nRet != 10035)
{
TRACE(_T("UDP recvfrom error: %d - tm: %d us \n"), nRet,T0.Getus());
}
return -1;
}
在沒有連接的時候,或者服務器斷線的時候,
UDP recvfrom error: 10060 - tm: 510144 us
這個超時都在500ms以上,無論設定多少
但是如果把超時設定為 1000ms,這個超時也會跟著變成1000ms以上
uj5u.com熱心網友回復:
SO_RCVTIMEO and SO_SNDTIMEO do not work on all socket operations, you should use non-blocking mode and select.
The behaviour may change on different operating system configurations. On my system the connect timeouts after two times the value I set in SO_RCVTIMEO. A quick hack like setting SO_RCVTIMEO to x/2 before a connect and x after it works, but the proper solution is using select.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/119447.html
標籤:網絡編程
