I'm currently doing an assignment in which we are defined to use send()/recv() with two parts:
first send an int that indicates the length of the string (4 bytes) and then send the string itself.
When I try to run the client/server on my PC, only the first recv() returns success, while the next one fails.
When I run the same code on my partner's PC, it works well.
The error I'm receiving for recv() is 10054.
I am totally clueless, and have no idea how to approach this.
The receiving code is:
TransferResult_t ReceiveString(char** OutputStrPtr, SOCKET sd){
    /* Recv the the request to the server on socket sd */
    int TotalStringSizeInBytes;
    TransferResult_t RecvRes;
    char* StrBuffer = NULL;
    if ((OutputStrPtr == NULL) || (*OutputStrPtr != NULL))
    {
        printf("The first input to ReceiveString() must be "
            "a pointer to a char pointer that is initialized to NULL. For example:\n"
            "\tchar* Buffer = NULL;\n"
            "\tReceiveString( &Buffer, ___ )\n");
        return TRNS_FAILED;
    }
    /* The request is received in two parts. First the Length of the string (stored in
    an int variable ), then the string itself. */
    RecvRes = ReceiveBuffer(
        (char *)(&TotalStringSizeInBytes),
        (int)(sizeof(TotalStringSizeInBytes)), // 4 bytes
        sd);
    if (RecvRes != TRNS_SUCCEEDED) return RecvRes;
    StrBuffer = (char*)malloc(TotalStringSizeInBytes * sizeof(char));
    if (StrBuffer == NULL)
        return TRNS_FAILED;
    RecvRes = ReceiveBuffer(
        (char *)(StrBuffer),
        (int)(TotalStringSizeInBytes),
        sd);
    if (RecvRes == TRNS_SUCCEEDED)
    {
        *OutputStrPtr = StrBuffer;
    }
    else
    {
        free(StrBuffer);
    }
    return RecvRes;
}
The ReceiveBuffer function is:
TransferResult_t ReceiveBuffer(char* OutputBuffer, int BytesToReceive, SOCKET sd){
    char* CurPlacePtr = OutputBuffer;
    int BytesJustTransferred;
    int RemainingBytesToReceive = BytesToReceive;
    while (RemainingBytesToReceive > 0)
    {
        /* send does not guarantee that the entire message is sent */
        BytesJustTransferred = recv(sd, CurPlacePtr, RemainingBytesToReceive, 0);
        if (BytesJustTransferred == SOCKET_ERROR)
        {
            printf("recv() failed, error %d\n", WSAGetLastError());
            return TRNS_FAILED;
        }
        else if (BytesJustTransferred == 0)
            return TRNS_DISCONNECTED; // recv() returns zero if connection was gracefully disconnected.
        RemainingBytesToReceive -= BytesJustTransferred;
        CurPlacePtr += BytesJustTransferred; // <ISP> pointer arithmetic
    }
    return TRNS_SUCCEEDED;
}
ADDED: the sending function:
TransferResult_t SendString( const char *Str, SOCKET sd ){
    /* Send the the request to the server on socket sd */
    int TotalStringSizeInBytes;
    TransferResult_t SendRes;
    /* The request is sent in two parts. First the Length of the string (stored in 
    an int variable ), then the string itself. */
    TotalStringSizeInBytes = (int)( strlen(Str) + 1 ); // terminating zero also sent    
    SendRes = SendBuffer( 
        (const char *)( &TotalStringSizeInBytes ),
        (int)( sizeof(TotalStringSizeInBytes) ), // sizeof(int) 
        sd );
    if ( SendRes != TRNS_SUCCEEDED ) return SendRes ;
    SendRes = SendBuffer( 
        (const char *)( Str ),
        (int)( TotalStringSizeInBytes ), 
        sd );
    return SendRes;
}
And:
TransferResult_t SendBuffer( const char* Buffer, int BytesToSend, SOCKET sd ){
    const char* CurPlacePtr = Buffer;
    int BytesTransferred;
    int RemainingBytesToSend = BytesToSend;
    while ( RemainingBytesToSend > 0 )  
    {
        /* send does not guarantee that the entire message is sent */
        BytesTransferred = send (sd, CurPlacePtr, RemainingBytesToSend, 0);
        if ( BytesTransferred == SOCKET_ERROR ) 
        {
            printf("send() failed, error %d\n", WSAGetLastError() );
            return TRNS_FAILED;
        }
        RemainingBytesToSend -= BytesTransferred;
        CurPlacePtr += BytesTransferred; // <ISP> pointer arithmetic
    }
    return TRNS_SUCCEEDED;
}
 
    