how to use WSAConnect and WSAAccept for sending receving init Data before accept a request

僤鯓⒐⒋嵵緔 提交于 2019-12-04 23:16:44

lpCallerData is always NULL in your WSAAccept() callback because TCP/IP does not support exchanging caller/callee data during connection establishment. This is clearly stated in the WSAConnect() documentation:

Note Connect data is not supported by the TCP/IP protocol in Windows. Connect data is supported only on ATM (RAWWAN) over a raw socket.

The WSAAccept() documentation also states:

If no caller identification or caller data is available, the corresponding parameters will be NULL. Many network protocols do not support connect-time caller data.

...

The lpCalleeData->len initially contains the length of the buffer allocated by the service provider and pointed to by lpCalleeData->buf. A value of zero means passing user data back to the caller is not supported.

With that said, even if it were supported, you are not managing the WSABUF structures correctly anyway. Your server is not checking for NULL or len overflows, and your client code is not allocating any memory for the WSABUF::buf fields.

Your code would need to look more like this instead:

Server side:

int CALLBACK ConditionAcceptFunc(
    LPWSABUF lpCallerId,
    LPWSABUF lpCallerData,
    LPQOS pQos,
    LPQOS lpGQOS,
    LPWSABUF lpCalleeId,
    LPWSABUF lpCalleeData,
    GROUP FAR * g,
    DWORD_PTR dwCallbackData
    )
{
    //printf( "test1\n" );

    //if ((lpCallerData) && (lpCallerData->len >= 8) && (memcmp(lpCallerData->buf, "quyen194", 8) == 0)) 
    if ((lpCallerData) && (lpCallerData->len > 0) && (lpCallerData->buf[0] == 'q')) 
    {
        //if ((lpCalleeData) && (lpCalleeData->len > 0)) {
        //  memcpy( lpCalleeData->buf, "OK", 2 );
        //  lpCalleeData->len = 2;
        //}

        return CF_ACCEPT;
    }
    else
    {
        //printf( "Reject request: \n" );
        //if ((lpCallerData) && (lpCallerData->len > 0)) {
        //  printf( "Buf: %*s", lpCallerData->len, lpCallerData->buf );
        //  printf( "\nLen: %d\n", lpCallerData->len );
        //}
        //if ((lpCalleeData) && (lpCalleeData->len > 0)) {
        //  memcpy( lpCalleeData->buf, "NOT", 3 );
        //  lpCalleeData->len = 3;
        //}

        return CF_REJECT;
    }
}

Client side:

void Connect2Server()
{
    ...

    WSABUF CallerData;
    WSABUF CalleeData;

    char CallerBuf = 'q';
    CallerData.buf = &CallerBuf;
    CallerData.len = 1;

    char CalleeBuf[12] = {0};
    CalleeData.buf = CalleeBuf;
    CalleeData.len = 12;

    printf( "Source: \n" );
    printf( "Buf: %*s", CallerData.len, CallerData.buf );
    printf( "\nLen: %d\n", CallerData.len );

    ...

    ReturnValue = WSAConnect( C_Socket, (SOCKADDR *)&C_Channel, sizeof(C_Channel), &CallerData, &CalleeData, NULL, NULL );

    //printf( "Result: \n" );
    //printf( "Buf: %*s", CalleeData.len, CalleeData.buf );
    //printf( "Len: %d\n", CalleeData.len );

    ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!