DataSend y DataRecv Client-Server help

Eduardo Tosca Avalos 1 Reputation point
2021-01-27T22:45:41.04+00:00

hello I have a great question

I have a launcher that I am creating,

in the launcher datasend added the following

bool CConnection::DataSend(BYTE* lpMsg,int size) // OK
{
    this->m_critical.lock();

    if(this->CheckState() == 0)
    {
        this->m_critical.unlock();
        return 0;
    }

    char tes[255];
    wsprintf(tes, "%d",(this->m_SendSize+size));

    BYTE buff[MAX_BUFF_SIZE];

    memcpy(buff,lpMsg,size);
    WORD headcode;
    if(buff[0] == 0xC1)
    {
        wsprintf(tes, "%d",(this->m_SendSize+size));

        int Encrypt = 0;

        size = buff[0+1];
                    buff[0+2] ^= Encrypt;
            headcode = buff[0+2];

        //PacketEncryptData(&buff[3],(size-3),buff[2]);
    }
    else
    {
        //PacketEncryptData(&buff[4],(size-4),buff[3]);
    }

    if(this->m_SendSize > 0)
    {
        if((this->m_SendSize+size) > MAX_BUFF_SIZE)
        {
            wsprintf(tes, "%d",(this->m_SendSize+size));
            MessageBox(0, tes, "Error!", MB_OK | MB_ICONERROR);
            this->Disconnect();
            this->m_critical.unlock();
            return 0;
        }
        else
        {
            memcpy(&this->m_SendBuff[this->m_SendSize],buff,size);
            this->m_SendSize += size;
            this->m_critical.unlock();
            return 1;
        }
    }

    int count=0,result=0;

    while(size > 0)
    {
        if((result = send(this->m_socket,(char*)&buff[count],size,0)) == SOCKET_ERROR)
        {
            if(WSAGetLastError() == WSAEWOULDBLOCK)
            {
                if((this->m_SendSize+size) > MAX_BUFF_SIZE)
                {
                    wsprintf(tes, "%d",(this->m_SendSize+size));
                    MessageBox(0, tes, "Error!", MB_OK | MB_ICONERROR);
                    this->Disconnect();
                    this->m_critical.unlock();
                    return 0;
                }
                else
                {
                    memcpy(&this->m_SendBuff[this->m_SendSize],&buff[count],size);
                    this->m_SendSize += size;
                    this->m_critical.unlock();
                    return 1;
                }
            }
            else
            {
                wsprintf(tes, "%d",(this->m_SendSize+size));
                MessageBox(0, tes, "Error!", MB_OK | MB_ICONERROR);
                this->Disconnect();
                this->m_critical.unlock();
                return 0;
            }
        }
        else
        {
            count += result;
            size -= result;
        }
    }

    this->m_critical.unlock();

    wsprintf(tes, "%d",(this->m_SendSize+size));
                MessageBox(0, tes, "Error!", MB_OK);

    return 1;

}


BOOL RecvDataParse(_PER_IO_CONTEXT * lpIOContext, int uIndex)   
{
    unsigned char* recvbuf;
    int lOfs;
    WORD size;
    WORD headcode;

    // Check If Recv Data has More thatn 3 BYTES
    if ( lpIOContext->nSentBytes < 3 )
    {
        return TRUE;
    }

    // Initialize Variables
    lOfs=0;
    size=0;
    recvbuf = lpIOContext->Buffer;

    // Start Loop
    while ( true )
    {
                // Select packets with
        // C1 or C2 as HEader
        if ( recvbuf[lOfs] == 0xC1 ||
             recvbuf[lOfs] == 0xC3 )
        {
            size = recvbuf[lOfs+1];
            if(serverManager.Encrypt != 0)
                if(recvbuf[lOfs+2] != 0x04 && recvbuf[lOfs+2] != 0x05)
                    recvbuf[lOfs+2] ^= serverManager.Encrypt;
            headcode = recvbuf[lOfs+2];
        }
        else if ( recvbuf[lOfs] == 0xC2 ||
                  recvbuf[lOfs] == 0xC4 )
        {
            size = recvbuf[lOfs+1] * 256;
            size |= recvbuf[lOfs+2];
            if(serverManager.Encrypt != 0)
                if(recvbuf[lOfs+3] != 0x04 && recvbuf[lOfs+3] != 0x05)
                    recvbuf[lOfs+3] ^= serverManager.Encrypt;
            headcode = recvbuf[lOfs+3];
        }
        // If HEader Differs - Second Generation Protocols
        else
        {
            LogAdd("error-L1 : header error %d",recvbuf[lOfs]);
            return false;
        }

        // Check Size is leess thant 0
        if ( size <= 0 )
        {
            LogAdd("error-L1 : size %d",size);
            return false;
        }

        // Check if Size is On Range
        if ( size <= lpIOContext->nSentBytes )
        {
            ProtocolCore(uIndex, &recvbuf[lOfs], size);

            lOfs += size;
            lpIOContext->nSentBytes  -= size;

            if ( lpIOContext->nSentBytes <= 0 )
            {
                break;
            }
        }
        else if ( lOfs > 0 )
        {
            if ( lpIOContext->nSentBytes < 1 )
            {
                LogAdd("error-L1 : recvbuflen 1 %s %d", __FILE__, __LINE__);
                break;
            }

            if ( lpIOContext->nSentBytes < MAX_IO_BUFFER_SIZE ) 
            {
                memcpy(recvbuf, &recvbuf[lOfs], lpIOContext->nSentBytes);
                LogAdd("Message copy %d", lpIOContext->nSentBytes);
            }
            break;

        }
        else
        {
            break;
        }

    }
        /*
        memcpy(&size,&recvbuf[0],sizeof(size));
        memcpy(&headcode,&recvbuf[2],sizeof(headcode));
        size += 6;

        // Check Size is leess thant 0
        if ( size <= 0 && headcode)
        {
            LogAdd("error-L1 : size %d",
                size);

            return false;
        }
        // Check if Size is On Range
        if ( size <= lpIOContext->nSentBytes )
        {
            ProtocolCore(uIndex, recvbuf, size);

            lOfs += size;
            lpIOContext->nSentBytes  -= size;

            if ( lpIOContext->nSentBytes <= 0 )
            {
                break;
            }
        }
        else if ( lOfs > 0 )
        {
            if ( lpIOContext->nSentBytes < 1 )
            {
                LogAdd("error-L1 : recvbuflen 1 %s %d", __FILE__, __LINE__);
                break;
            }

            if ( lpIOContext->nSentBytes < MAX_IO_BUFFER_SIZE ) 
            {
                memcpy(recvbuf, &recvbuf[lOfs], lpIOContext->nSentBytes);
                LogAdd("Message copy %d", lpIOContext->nSentBytes);
                //break;
            }
            break;

        }
        else
        {
            break;
        }

    }*/

    return true;
}

the connectserver perfectly receives the data
here the detail is when it is the turn of the coneectserver to resend the corresponding data

datasend from connectserver

void CFileServerInfoMake()
{
    PMSG_FILESERVERINFO pMsg={0};
    PHeadSetB((LPBYTE)&pMsg, 1, sizeof(pMsg));

    pMsg.Version[0] = serverManager.ClientV2;
    pMsg.Version[1] = serverManager.ClientV3;
    //pMsg.Version[2] = serverManager.ClientV3;

    // FTP
    strcpy( pMsg.IpAddress, serverManager.FTP.Address );
    strcpy( pMsg.FtpId,     serverManager.FTP.ID );
    strcpy( pMsg.FtpPass,   serverManager.FTP.Password );\
    strcpy( pMsg.Folder,    serverManager.FTP.Folder );

    pMsg.Port = serverManager.FTP.Port;

    BuxConvert(pMsg.IpAddress, 100);
    BuxConvert(pMsg.FtpId,   20);
    BuxConvert(pMsg.FtpPass, 20);
    BuxConvert(pMsg.Folder,  20);

    memcpy( serverManager.szFileServerInfoSendBuffer, (char*)&pMsg, sizeof(pMsg) );
    serverManager.nFileServerInfoBufferLen = sizeof( pMsg );

    // HTTP
    pMsg.h.headcode = 0x04;
    strcpy( pMsg.IpAddress, serverManager.http.Address );
    strcpy( pMsg.FtpId,     serverManager.http.ID );
    strcpy( pMsg.FtpPass,   serverManager.http.Password );
    strcpy( pMsg.Folder,    serverManager.http.Folder );    
    pMsg.Port = serverManager.http.Port;

    BuxConvert(pMsg.IpAddress, 100);
    BuxConvert(pMsg.FtpId,   20);
    BuxConvert(pMsg.FtpPass, 20);
    BuxConvert(pMsg.Folder,  20);

    memcpy( serverManager.szHTTPFileServerInfoSendBuffer, (char*)&pMsg, sizeof(pMsg) );
    serverManager.nHTTPFileServerInfoBufferLen = sizeof( pMsg );

DataSend(aIndex, (LPBYTE)serverManager.szFileServerInfoSendBuffer, serverManager.nFileServerInfoBufferLen);
}


BOOL DataSend(int aIndex, unsigned char* lpMsg, DWORD dwSize)
{
    int i = 0;
    unsigned char changedProtocol = 0;
    int aLen = sizeof(lpMsg);
    unsigned long SendBytes;
    _PER_SOCKET_CONTEXT * lpPerSocketContext;
    unsigned char * SendBuf;
    EnterCriticalSection(&criti);

    if ( ((aIndex < 0)? FALSE : (aIndex > serverManager._MaxConnections-1)? FALSE : TRUE )  == FALSE )
    {
        LogAdd("error-L2 : Index(%d) %x %x %x ", dwSize, lpMsg[0], lpMsg[1], lpMsg[2]);
        LeaveCriticalSection(&criti);
        return false;
    }

    SendBuf = lpMsg;

    if ( Obj[aIndex].Connected == false )
    {
        LeaveCriticalSection(&criti);
        return FALSE;
    }

    lpPerSocketContext= Obj[aIndex].PerSocketContext;

    if ( dwSize > sizeof(lpPerSocketContext->IOContext[0].Buffer))
    {
        LogAdd("Error : Max msg(%d) %s %d", dwSize, __FILE__, __LINE__);
        CloseClient(aIndex);
        LeaveCriticalSection(&criti);
        return false;
    }

    _PER_IO_CONTEXT  * lpIoCtxt;

    lpIoCtxt = &lpPerSocketContext->IOContext[1];

    if ( lpIoCtxt->nWaitIO > 0 )
    {
        if ( ( lpIoCtxt->nSecondOfs + dwSize ) > MAX_IO_BUFFER_SIZE-1 )
        {
            LogAdd("(%d)error-L2 MAX BUFFER OVER %d %d %d", aIndex, lpIoCtxt->nTotalBytes, lpIoCtxt->nSecondOfs, dwSize);
            lpIoCtxt->nWaitIO = 0;
            CloseClient(aIndex);
            LeaveCriticalSection(&criti);
            return true;
        }

        memcpy( &lpIoCtxt->BufferSecond[lpIoCtxt->nSecondOfs], SendBuf, dwSize);
        lpIoCtxt->nSecondOfs += dwSize;
        LeaveCriticalSection(&criti);
        return true;
    }

    lpIoCtxt->nTotalBytes = 0;

    if ( lpIoCtxt->nSecondOfs > 0 )
    {
        memcpy(lpIoCtxt->Buffer, lpIoCtxt->BufferSecond, lpIoCtxt->nSecondOfs);
        lpIoCtxt->nTotalBytes = lpIoCtxt->nSecondOfs;
        lpIoCtxt->nSecondOfs = 0;
    }

    if ( (lpIoCtxt->nTotalBytes+dwSize) > MAX_IO_BUFFER_SIZE-1 )
    {
        LogAdd("(%d)error-L2 MAX BUFFER OVER %d %d", aIndex, lpIoCtxt->nTotalBytes, dwSize);
        lpIoCtxt->nWaitIO = 0;
        CloseClient(aIndex);
        LeaveCriticalSection(&criti);
        return FALSE;
    }

    memcpy( &lpIoCtxt->Buffer[lpIoCtxt->nTotalBytes], SendBuf, dwSize);
    lpIoCtxt->nTotalBytes += dwSize;
    lpIoCtxt->wsabuf.buf = (char*)&lpIoCtxt->Buffer;
    lpIoCtxt->wsabuf.len = lpIoCtxt->nTotalBytes;
    lpIoCtxt->nSentBytes = 0;
    lpIoCtxt->IOOperation = 1;


    if ( WSASend( Obj[aIndex].m_socket, &lpIoCtxt->wsabuf , 1, &SendBytes, 0, &lpIoCtxt->Overlapped, NULL) == -1 )
    {

        if ( WSAGetLastError() != WSA_IO_PENDING )  
        {
            lpIoCtxt->nWaitIO = 0;


            //if ( lpIoCtxt->wsabuf.buf[0] == 0xC1 )
            //{
            //  LogAdd("(%d)WSASend(%d) failed with error [%x][%x] %d %s ", __LINE__, aIndex, (BYTE)lpIoCtxt->wsabuf.buf[0],
            //      (BYTE)lpIoCtxt->wsabuf.buf[2], WSAGetLastError(), Obj[aIndex].Ip_addr);
            //}
            //else if ( lpIoCtxt->wsabuf.buf[0] == 0xC2 )
            //{
            //  LogAdd("(%d)WSASend(%d) failed with error [%x][%x] %d %s ", __LINE__, aIndex, (BYTE)lpIoCtxt->wsabuf.buf[0],
            //      (BYTE)lpIoCtxt->wsabuf.buf[3], WSAGetLastError(), Obj[aIndex].Ip_addr);
            //}
            CloseClient(aIndex);
            LeaveCriticalSection(&criti);
            return false;
        }
    }
    else
    {
        lpPerSocketContext->dwIOCount ++;
    }


    lpIoCtxt->nWaitIO = 1;
    LeaveCriticalSection(&criti);
    return true;
}

the problem is that the code does not arrive correctly
I leave the launcher code here since the problem comes from here.

file

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,446 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,558 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Drake Wu - MSFT 991 Reputation points
    2021-01-29T02:50:05.127+00:00

    Hi @Eduardo Tosca Avalos , Although I cannot recreate your project, there may be a problem with the following lines:

         memcpy( &lpIoCtxt->Buffer[lpIoCtxt->nTotalBytes], SendBuf, dwSize);  
         lpIoCtxt->nTotalBytes += dwSize;  
         lpIoCtxt->wsabuf.buf = (char*)&lpIoCtxt->Buffer;  
         lpIoCtxt->wsabuf.len = lpIoCtxt->nTotalBytes;  
    

    According to the WSABUF:

    len

    The length of the buffer, in bytes.

    buf

    A pointer to the buffer.

    And also according to the Example in WSASend documentation, you should set the WSABUF as following:

         memcpy( &lpIoCtxt->Buffer[lpIoCtxt->nTotalBytes], SendBuf, dwSize);  
         lpIoCtxt->nTotalBytes += dwSize;  
         lpIoCtxt->wsabuf.buf = lpIoCtxt->Buffer;  
         lpIoCtxt->wsabuf.len = lpIoCtxt->nTotalBytes;  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments