设置的Winsock选择超时准确(Setting winsock select timeout ac

2019-09-18 01:08发布

我试图让我接受某一特定的时间段后叫暂停,我试图在这里下列建议:

Winsock的接受超时

在这种情况下,我通过的timeval结构时,我把它叫做选择,问题是,当我设置tv.tv_usec约40分钟左右说,选择通话时间马上出来,而不是等待40分钟,我确定。 MSDN指出,对于选择超时是最大的时间,它会等待,我怎么让这样选择或接受为此事等待超时之前的特定时间段?

#define WIN32_LEAN_AND_MEAN

#include <Windows.h>

#include <WinSock2.h>
#include <Ws2tcpip.h>

#include <cstdio>
#include <tchar.h>

VOID _tmain( int argc, TCHAR *argv[] )
{
    WSADATA wsaData = { 0 };
    ADDRINFOA hINTs = { 0 };
    PADDRINFOA pResult = NULL;
    SOCKET hServerSocket = INVALID_SOCKET,
           hClientSocket = INVALID_SOCKET;
    TIMEVAL tv = { 0 };
    INT iReturnStatus = -1;
    DWORD dwRecvTimeout = 30000, // Milliseconds
          dwSendTimeout = 30000; // Milliseconds
    fd_set readFDs = { 0 };

    if ( WSAStartup( MAKEWORD( 2, 2 ), &wsaData ) )
    {
        _tprintf_s( TEXT( "WSAStartup Failed\n" ) );
        return;
    }

    ZeroMemory( &hINTs, sizeof( hINTs ) );
    hINTs.ai_family = AF_INET;
    hINTs.ai_socktype = SOCK_STREAM;
    hINTs.ai_protocol = IPPROTO_TCP;
    hINTs.ai_flags = AI_PASSIVE;
    if ( getaddrinfo( NULL, TEXT( "9001" ), &hINTs, &pResult ) )
    {
        WSACleanup();
        _tprintf_s( TEXT( "getaddrinfo Failed\n" ) );
        return;
    }

    if ( ( hServerSocket = socket( pResult -> ai_family, pResult -> ai_socktype, pResult -> ai_protocol ) ) == INVALID_SOCKET )
    {
        freeaddrinfo( pResult );
        WSACleanup();
        _tprintf_s( TEXT( "socket Failed\n" ) );
        return;
    }

    int iResult = bind( hServerSocket, ( pResult -> ai_addr ), pResult -> ai_addrlen );
    if ( iResult == SOCKET_ERROR )
    {
        freeaddrinfo( pResult );
        closesocket( hServerSocket );
        WSACleanup();
        _tprintf_s( TEXT( "bind Failed\n" ) );
        return;
    }
    freeaddrinfo( pResult );

    if ( listen( hServerSocket, SOMAXCONN ) )
    {
        closesocket( hServerSocket );
        WSACleanup();
        _tprintf_s( TEXT( "listen Failed\n" ) );
        return;
    }

    hClientSocket = INVALID_SOCKET;

    for ( ;; )
    {
        tv.tv_usec = 2400000000; // microseconds
        FD_ZERO( &readFDs );
        FD_SET( hServerSocket, &readFDs );
        _tprintf( "select()\n" );
        iReturnStatus = select( 0, &readFDs, NULL, NULL, &tv );

        // Select Error
        if ( iReturnStatus == SOCKET_ERROR )
        {
            _tprintf( "select Failed\n" );
        }
        // Select Success
        else if ( iReturnStatus )
        {
            // Connection Established On Server Socket
            if ( FD_ISSET( hServerSocket, &readFDs ) )
            {
                // Accept Client Connection
                hClientSocket = accept( hServerSocket, NULL, NULL );
                if ( hClientSocket == INVALID_SOCKET )
                {
                    _tprintf( "accept Failed\n" );
                }
                else
                {
                    // Set Recv Timeout
                    setsockopt( hClientSocket, SOL_SOCKET, SO_RCVTIMEO, ( const char * ) &dwRecvTimeout, sizeof( dwRecvTimeout ) );

                    // Set Send Timeout                 
                    setsockopt( hClientSocket, SOL_SOCKET, SO_SNDTIMEO, ( const char * ) &dwSendTimeout, sizeof( dwSendTimeout ) );

                    // Process Client Request(s)
                    // HandleConnection( ClientSocket );
                }
            }
            // Connection Established On Unknown Socket
            else
            {
                _tprintf( "Invalid Socket Returned\n" );
            }
        }
        // Select Timeout
        else
        {
            _tprintf( "select Timeout\n" );
        }

    }
    if ( hServerSocket != INVALID_SOCKET )
        closesocket( hServerSocket );

    return;
}

Answer 1:

为什么会这样?

这是每确定指标。

逐字从MSDN :

tv_sec的时间间隔(秒)。

tv_usec的时间间隔,以微秒。 该值在组合使用与tv_sec构件来表示不属于秒的多个时间间隔的值



Answer 2:

问题是与tv.tv_usec; tv_usec按照手册页的类型为“长”。 值(24亿), 超出的范围长 ,这是你得到这样的选择系统行为的原因。

如果你想选择等待40分钟,然后确保你正在使用tv.tv_sec。



文章来源: Setting winsock select timeout accurately