C:recv中的套接字在一个字节后失败

时间:2018-01-24 22:38:45

标签: c sockets winsock2 recv

我目前正在执行一项任务,我们将其定义为使用send() / recv()两部分:

  

首先发送一个指示字符串长度的int(4个字节),然后发送字符串本身。

当我尝试在我的PC上运行客户端/服务器时,只有第一个recv()返回成功,而下一个失败。

当我在合作伙伴的PC上运行相同代码时,它运行良好。

我收到的recv()错误是10054。

我完全无能为力,并且不知道如何处理这个问题。

接收代码是:

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;
}

ReceiveBuffer功能是:

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:发送功能:

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;
}

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;
}

0 个答案:

没有答案