简体   繁体   中英

WINSOCK2 WSAAsyncSelect deprecated issue

Hi I am using winsock2 and I am trying to get async communication. I tried with TCP server msdn which is waiting for accept.

I tried with WSAAsyncSelect before the listen function.

WSAAsyncSelect(ListenSocket,
        m_hWnd,
        WM_SOCKET,
        (FD_CLOSE | FD_ACCEPT | FD_READ));

and is showing the following error ..

Error   C4996   'WSAAsyncSelect': Use WSAEventSelect() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings 

How can I solve this .. Thanks in advance ..

To get rid of that warning you can define #define _WINSOCK_DEPRECATED_NO_WARNINGS at the top of your source file, which will disable there types of warnings.

That said, it might be worth listening to this warning and using WSAEventSelect instead. Since WSAEventSelect signals an event instead of posting a message to the Window's message queue which is what WSAAsyncSelect does. Posting into the message queue is slower and adds additional processing that isn't really needed.

I have an example laying around which i'll post here for you:
... I may have gone overboard... but it's all useful and relevant.

Setup Listening Socket

if ( socket_name == INVALID_SOCKET )
{
    P_ERR( "Could not create socket ( Id: %d ): %d\n", id, WSAGetLastError() );
    return INVALID_SOCKET;
}

rerror = bind( socket_name, (SOCKADDR*)&sock_addr, sizeof( sock_addr ) );
if ( rerror != SOCKET_ERROR )
{
    rerror = listen( socket_name, MAX_LISTEN_QUEUE );

    if ( rerror != SOCKET_ERROR )
    {
        /* Selects the events that will trigger the `socket_event` windows event. */
        /* socket_event was created using 'CreateEvent( NULL, TRUE, FALSE, NULL );' */
        WSAEventSelect( socket_name, socket_event, FD_ALL_EVENTS );

        if ( !startStatusThread() )
        {
            rerror = 1;
            P_ERR( "Status thread failed: %d\n", id );
        }
    }
    else
    {
        P_ERR( "listen() error %d : Error %d\n", id, WSAGetLastError() );
        closesocket( socket_name );
    }
}
else
{
    P_ERR( "bind() error ( Id: %d ): %d\n", id, WSAGetLastError() );
    closesocket( socket_name );
}

Process Events From Socket

    /* waits 10ms for events in the event array ( in this case just 1 event as socket_event ). */
    rerror = WSAWaitForMultipleEvents( 1, &socket_event, FALSE, 10, FALSE );
    if ( rerror == WSA_WAIT_TIMEOUT )
    {
        continue; /* this block of code runs in a while loop. */
    }

    index = rerror - WSA_WAIT_EVENT_0; /* get the smallest index of a triggered event */
    if ( rerror != WSA_WAIT_TIMEOUT && rerror != WSA_WAIT_FAILED )
    {
        /* returns a list of the events that occured. */
        rerror = WSAEnumNetworkEvents( socket_name, socket_event, &events );
        if ( rerror == SOCKET_ERROR )
        {
            P_ERR( "WSAEnumNetworkEvents Error %d: Id: %d\n", WSAGetLastError(), pThis->id );
            continue;
        }
        /* look below for this function. */
        handleNetworkEvents( events, index );
    }

Handling the Events.

void
handleNetworkEvents( WSANETWORKEVENTS e, const int socket_index )
{
    int rerror = 0;
    /* on accept. */
    if ( e.lNetworkEvents & FD_ACCEPT )
    {
        if ( e.iErrorCode[FD_ACCEPT_BIT] == 0 )
        {
            onAccept();
        }
        else
        {
            P_ERR( "Unknown network event error %d\n", id );
        }
    }
    /* on connect */
    if ( e.lNetworkEvents & FD_CONNECT )
    {
        if ( e.iErrorCode[FD_CONNECT_BIT] == 0 )
        {
            sendRead(); /* send first read request */
        }
        else
        {
            P_ERR( "Unknown network event error %d\n", id );
        }
    }
    /* on successful read */
    if ( e.lNetworkEvents & FD_READ )
    {
        sendRead(); /* get read data and queue another request. */
        callback( id, inBuffer.buf, lastReadSize ); /* process data. */
    }
    /* on close. */
    if ( e.lNetworkEvents & FD_CLOSE )
    {
        /* close the current event and make a new one ready for a new connection. */
        onClose( socket_index );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM