recv(winsock)函数挂起,但数据可用

时间:2014-02-13 06:59:18

标签: c++ sockets ping recv icmp

我正在使用winsock socket api发送一个端口号非常高的udp数据包,我希望它在目的地未被使用。我的目的是收到一个带有目的地不可达/端口不可达消息的icmp数据包。我创建了两个套接字,其中一个是我发送UDP数据包和其他套接字,我期待icmp数据包。发送成功。目的地也返回ICMP回复,我可以在wireshark上验证。现在,当我执行recv,接收数据时,recv函数挂起。我的目标是从recv函数中读取数据,但这不会发生。

任何有关低估/解决此行为的帮助都将受到高度赞赏。提前谢谢。

我在这里附上代码片段......

    void sendPacket(unsigned int socketFd, char *packet, char* remoteIP, char* pingType)
{
    int nsent = -1;
    int rc = -1;

    struct addrinfo hints, *res;

    memset(&hints, 0, sizeof(struct addrinfo));

    if (strcasecmp(pingType, "UDP")==0)
    {
        hints.ai_flags      = AI_CANONNAME;         /* always return canonical name */
        hints.ai_family     = AF_INET;              /* 0, AF_INET, AF_INET6, etc. */
        hints.ai_socktype   = SOCK_DGRAM;           /* 0, SOCK_STREAM, SOCK_DGRAM, etc. */
    }

    rc = getaddrinfo(remoteIP, NULL, &hints, &res);
    if ( rc != 0)
    {
        printf("... Function: %s\tError setting remote address. Exiting. ... \n", __FUNCTION__);    
        exit(-1);
    }

    if (strcasecmp(pingType, "UDP")==0)
    {
        ((struct sockaddr_in *)res->ai_addr)->sin_port = htons(34344);
        strcpy(packet, "TIMESTAMP");
    }

    do
    {
        if (strcasecmp(pingType, "UDP")==0)
        {
            nsent=sendto(socketFd, packet, strlen(packet), 0, (struct sockaddr *)res->ai_addr, (socklen_t)res->ai_addrlen);
            if (nsent < 0)
            {
                continue;
            }
        }
    }while(nsent < 0);

    return;
}


double receivePacket(int socketFd, struct timeval* tvSend, pingReply** lastReplyNode, char* pingType)
{
    ssize_t nRecv = -1;
    double rc = -1;
    char recvbuf[1024];

    do
    {
        nRecv = recv(socketFd, (char *)recvbuf, 1024, 0);
    }
    while(nRecv < 0);

    if (nRecv < 0)
    {
        return -1;
    }       

    rc = processPacket(recvbuf, nRecv, tvSend, lastReplyNode, pingType);

    if (rc == -1)
    {
        printf("... Function: %s\tReceiving error in Data/Protocol ...\n", __FUNCTION__);
        return -1;
    }

    return rc;
}

void createSocket(unsigned int *sendSocketFd, unsigned int *receiveSocketFd, char *pingType)
{
#ifdef _WIN32
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;

    /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
    wVersionRequested = MAKEWORD(2, 2);

    err = WSAStartup(wVersionRequested, &wsaData);
    if (err != 0) 
    {
        /* Tell the user that we could not find a usable */
        /* Winsock DLL.                                  */
        printf("WSAStartup failed with error: %d\n", err);
        exit(-1);
    }
#endif

#ifdef _WIN32
    if (strcasecmp(pingType, "UDP")==0)
    {
        int rc = -1;
        struct sockaddr_in src_address;
        unsigned long int length;
        int optval = 1;
        DWORD Length;
        OSVERSIONINFO     g_OSVersionInfo;
        BOOLEAN           g_IsWindowsLonghorn = TRUE;
        BOOLEAN           g_UseFtosToSetTos = TRUE;
        int  ret, iVal=0;
        unsigned int sz = sizeof(iVal);

        g_OSVersionInfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO);
        GetVersionEx( &g_OSVersionInfo );

        if( g_OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT )
        {
            if( g_OSVersionInfo.dwMajorVersion >= 6 )
            {
                g_IsWindowsLonghorn = TRUE;
                g_UseFtosToSetTos = TRUE;
            }
        }

        *receiveSocketFd = INVALID_SOCKET;
        *receiveSocketFd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
        if (*receiveSocketFd < 0)
        {
            printf("Function: %s\tReceiving Socket creation error.\tErrNo %d. ...\n", __FUNCTION__, WSAGetLastError());
            exit(-1);
        }

        src_address.sin_family=AF_INET;
        src_address.sin_addr.s_addr=inet_addr("x.x.x.x");
        src_address.sin_port=htons(0);

        rc = bind((SOCKET)*receiveSocketFd,(struct sockaddr *)&src_address,sizeof(src_address));
        if (rc < 0)
        {
            printf("Function: %s\tReceiving Socket bind error.\tErrNo %d. ...\n", __FUNCTION__, WSAGetLastError());
            exit(-1);
        }   

        iVal = 30000;   // in milliseconds
        ret = setsockopt(*receiveSocketFd, SOL_SOCKET, SO_RCVTIMEO, (char *)&iVal, sz);
        if (ret == SOCKET_ERROR)
        {
            printf("%d\n", WSAGetLastError());
            return; // Temporary
        }

        rc = WSAIoctl((SOCKET)*receiveSocketFd, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &length, NULL, NULL);
        if (rc == SOCKET_ERROR)
        {
            printf("Function: %s\tReceiving Socket ioctl error.\tErrNo %d. ...\n", __FUNCTION__, WSAGetLastError());
            exit(-1);
        }
        printf("Function: %s\treceiveSocketFd %d ...\n", __FUNCTION__, *receiveSocketFd);
    }
    else
    {
        *receiveSocketFd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
        if (*receiveSocketFd < 0)
        {
            printf("Function: %s\tReceiving Socket creation error.\tErrNo %d. ...\n", __FUNCTION__, WSAGetLastError());
            exit(-1);
        }
        printf("Function: %s\treceiveSocketFd %d ...\n", __FUNCTION__, *receiveSocketFd);
    }
#endif

#ifndef _WIN32
    unsigned int size = 1024;       /* OK if setsockopt fails */
    setsockopt(*receiveSocketFd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
#else
    char size[5] = "1024";      /* OK if setsockopt fails */
    setsockopt(*receiveSocketFd, SOL_SOCKET, SO_RCVBUF, size, sizeof(size));
#endif

    if (strcasecmp(pingType, "UDP")==0)
    {
        *sendSocketFd = socket(AF_INET, SOCK_DGRAM, 0);
        if (*sendSocketFd < 0)
        {
            printf("Send Socket creation error.");
            exit(-1);
        }
        printf("Function: %s\tsendSocketFd %d ...\n", __FUNCTION__, *sendSocketFd);
    }

    return;
}

1 个答案:

答案 0 :(得分:0)

你没有得到'目的地无法访问',除非套接字是连接的,在UDP意义上,然后你没有通过recv()得到它,你从一个可能的后续send()得到它的errno值