当我使用MSG_WAITALL标志时,套接字编程中的recv函数失败

时间:2017-06-19 06:48:11

标签: sockets timeout winsock

我为套接字的客户端编写了一个非常简单的代码。 这是我的接收功能:

    int n32RecvLength = 10;  

    do {   

        n32RecvLength = recv(m_socketConnectSocket, m_szRecvBuffer, m_n32BufferLength, MSG_WAITALL);    

        if ( n32RecvLength > 0 )
        {
            printf("\nBytes received: %d\n", n32RecvLength);
            //for(int i = 0; i< n32RecvLength; i++)
                //putchar(m_szRecvBuffer[i]);

            if (evRecPacket != NULL)
            {
                evRecPacket(static_cast<void*>(m_ptrvDerivedClass),reinterpret_cast<unsigned char*>(m_szRecvBuffer) , n32RecvLength); 
            }
        }
        else if ( n32RecvLength == 0 ) 
        {  
            printf("Connection closed!!!!!!!\n");
            if(evDisconnected != NULL) 
                evDisconnected(static_cast<void*>(m_ptrvDerivedClass));  
            break;
         }

        else
        {
            int errorNum = WSAGetLastError();  

            if (errorNum == WSAETIMEDOUT)
            {
                printf ("\n timeOut\n");   
                n32RecvLength = 1;
            }
            else
            {
                printf("recv failed with error: %d\n", errorNum);
                if(evDisconnected != NULL) 
                    evDisconnected(static_cast<void*>(m_ptrvDerivedClass));  
                break;
            }
        } 

    } while( n32RecvLength > 0 );   

我在这个函数中初始化了它:

   WSADATA swsaData;
struct addrinfo *result = NULL;
struct addrinfo hints;

unsigned int n32CheckCondition = 0; 

n32CheckCondition = WSAStartup(MAKEWORD(2,2), &swsaData);
if (n32CheckCondition != 0) {
    printf("WSAStartup failed with error: %d\n", n32CheckCondition);
    return;
}


ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

char szPortNumber[100];
itoa(m_n32PortNumber,szPortNumber , 10);



n32CheckCondition = getaddrinfo(m_szIpServerAddr, szPortNumber, &hints, & m_ptrsResult);
if ( n32CheckCondition != 0 ) {
    printf("getaddrinfo failed with error: %d\n", n32CheckCondition);
    WSACleanup();
    return;
}   

m_socketConnectSocket = socket( m_ptrsResult->ai_family,  m_ptrsResult->ai_socktype,  m_ptrsResult->ai_protocol);
if (m_socketConnectSocket == INVALID_SOCKET)
{
    printf("socket failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
    return;
}   

// to set timeout feature to your system, uncomment the two following lines and adjust it
DWORD timeout =  10000;  // mili second!
setsockopt(m_socketConnectSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));

我正在使用带有MSG_WAITALL标志的recv函数。我设置了超时= 10000毫秒 我设置了m_n32BufferLength = 8.当我发送一个8字节的数据时,它工作正常,但是当我发送9个字节时它显示前8个字节并且在几秒钟后(超时值),recv(...)返回零。我知道当recv返回零时,表示套接字已经关闭。但我没有关闭套接字。我也丢失了数据的第9个字节。 你能告诉我我该做什么吗?

0 个答案:

没有答案