mingpingzhang
mingpingzhang
2016-05-30 11:56

关于Ping,总是得到0,不知是为什么

下面是我从网上抄的一段代码,网上代码我试过,工作很正常,但把它移植到我的程序中几乎没有改动一个字,只是为了适合我的程序,而改动了参数和循环,却结果很不正常,得到的值问题为零,请各位大侠帮帮我。

bool Network::ping(const char* address, int* time)
{
string str_dest_addr;
SOCKET sock_raw;
SOCKADDR_IN dest_addr;
SOCKADDR_IN from_addr;

char sendBuffer[1024] = {0};
char recvBuffer[1024] = {0};

WSADATA wsaData;

::WSAStartup(MAKEWORD(2,2), &wsaData);

sock_raw = ::socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

int timeout = 1000;

setsockopt(sock_raw, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
setsockopt(sock_raw, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));

dest_addr.sin_family = AF_INET;
memset(&dest_addr.sin_zero, 0, 8);

//while( cout << "ping>")
{
cin >> str_dest_addr;
int nSeq = 0;
int nPacketSent = 0;
int nPacketReceived = 0;
int nTotalRoundTime = 0;
int nMaxRoundTime = 0;
int nMinRoundTime = -1;
if((dest_addr.sin_addr.S_un.S_addr = inet_addr(str_dest_addr.c_str())) == INADDR_NONE)
{
struct hostent *hp = NULL;
if((hp = gethostbyname(str_dest_addr.c_str())) != NULL)
{
memcpy(&(dest_addr.sin_addr), hp->h_addr_list[0],hp->h_length);
}
else
{
cout << "can not find the host!" << endl;
*time = 2000;
return true;
}
}

    while( nPacketSent < 4)
    {
        nPacketSent++;
        ICMPHeader* pIcmpHeader = (ICMPHeader*)sendBuffer;

        pIcmpHeader->byType = 8;
        pIcmpHeader->byCode = 0;
        pIcmpHeader->nId = ::GetCurrentProcessId();
        pIcmpHeader->nChecksum = 0;

        pIcmpHeader->nSequence = htons(nSeq++);
        memset(sendBuffer + sizeof(ICMPHeader), '*', 32);
        pIcmpHeader->nChecksum = htons(checkSum(sendBuffer, sizeof(ICMPHeader) + 32));

        int nRet = sendto(sock_raw, sendBuffer, sizeof(ICMPHeader) + 32, 0, (SOCKADDR*)&dest_addr, sizeof(SOCKADDR_IN));

        if(nRet == SOCKET_ERROR)
        {
            cout << "send error:" << ::WSAGetLastError() << endl;
            *time = 2000;
            return false;
        }

        unsigned long dwSendTime = ::GetTickCount();

        int fromLen = sizeof(SOCKADDR_IN);

        nRet = recvfrom(sock_raw, recvBuffer, 1024, 0, (SOCKADDR*)&from_addr, &fromLen);

        if(nRet == SOCKET_ERROR)
        {
            if(::WSAGetLastError() == WSAETIMEDOUT)
            {
                qDebug() << "Request time out" << endl;
                *time = 2000;
            }
            return false;
        }
        qDebug() << "count:" << GetTickCount() - dwSendTime;

        IPheader* ipHdr = (IPheader*)recvBuffer;
        ICMPHeader* icmpHdrRet = (ICMPHeader*)(recvBuffer + sizeof(IPheader));

        if( icmpHdrRet->byCode == 0 && icmpHdrRet->nId == pIcmpHeader->nId && icmpHdrRet->nSequence == pIcmpHeader->nSequence)
        {

            nPacketReceived++;
            unsigned long dwRecvTime = ::GetTickCount();
            int nRoundTime = dwRecvTime - dwSendTime;
            nTotalRoundTime += nRoundTime;

            if(nMinRoundTime == -1)
            {
                nMinRoundTime = nRoundTime;
                nMaxRoundTime = nRoundTime;
            }

            if( nRoundTime < nMinRoundTime)
            {
                nMinRoundTime = nRoundTime;
            }

            if( nRoundTime > nMaxRoundTime)
            {
                nMaxRoundTime = nRoundTime;
            }

            *time = nRoundTime;
            qDebug() << "Reply from " << inet_ntoa(from_addr.sin_addr) << ": bytes = " 
                << nRet - sizeof(ICMPHeader) - sizeof(IPheader) 
                << ", time = " << nRoundTime << "ms, TTL = " << (int)ipHdr->byTtl << endl;
        }
        else
        {
            cout << "The echo reply is not correct!" << endl;
        }

        //Sleep(1000);
    }


    qDebug() << endl << "Ping statistics for " << inet_ntoa(from_addr.sin_addr) << ":" << endl 
        << "\t" << "Packets:sent = " << nPacketSent << ", Received = " << nPacketReceived 
        << ", Lost = " << (nPacketSent - nPacketReceived) << "(" 
        << ((float)(nPacketSent - nPacketReceived) /nPacketSent) * 100 << "% loss)" << endl;

    if(nPacketReceived)
    {
        cout << "\rApproximate round trip times in milli-seconds:" << endl << '\t' << "Minimum = " << nMinRoundTime <<
        "ms, Maximum = " << nMaxRoundTime << "ms, Average = " << (float)nTotalRoundTime /nPacketReceived << "ms" << endl;
    }

}

::closesocket(sock_raw);
::WSACleanup();

return true;

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

相关推荐