gethostbyaddr()扫描不存在IP时的等待时间可否改变?第三个参数有哪些取值?

解决方案 »

  1.   

    我的思路是:用gethostbyaddr()扫描局域网IP,通过gethostbyaddr()对指定的IP进行探测,若返回值为NULL,则说明该IP不存在,否则,将该IP添加到列表框中,在探测下一个IP.
    但是如果该IP不存在,整个程序要等几十秒才能开始探测下一个IP,我能不能自己控制gethostbyaddr()的等待时间,比如说0.2秒后无反应就探测下一个IP。
      

  2.   

    gethostbyaddr() 如何调整超时我不知道, 想来也不是很方便, 倒不如考虑考虑建一个表, 然后一次扫描一批IP地址, 等待返回即可.时间和效率上恐怕都要比你的 0.2 秒要好.
      

  3.   

    类似ping命令,我下面有个程序,是多线程的。你看看
    开始扫描
    UINT PingThread( LPVOID pParam )
    {
    CPingThreadInfo* pThreadInfo = (CPingThreadInfo*)pParam;
    HWND m_hWnd = pThreadInfo->m_hwndNotifyWnd;

    SOCKET   rawSocket;
    struct    sockaddr_in saDest;
    struct    sockaddr_in saSrc;
    unsigned int tempIP=0;
    int nRet;
    DWORD   dwTimeSent,dwElapsed;
    u_char    cTTL;
    LPHOSTENT lpHost;
    HOSTENT HostInfo;
    int nZoneID=0;

    WORD wVersionRequested;

    WSADATA wsaData;
    int err;

    wVersionRequested = MAKEWORD( 2, 2 );

    err = WSAStartup( wVersionRequested, &wsaData );

    rawSocket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);

    tempIP=pThreadInfo->uintIP[0];
    pThreadInfo->nRetries=1;
    while (tempIP<=pThreadInfo->uintIP[1])
    {
    if (WaitForSingleObject(pThreadInfo->m_hEventKillPing,0)== WAIT_OBJECT_0)
    break;
    saDest.sin_addr.S_un.S_addr =htonl(tempIP);
    unsigned int ip1=0,ip2=0;
    ip2=htonl(tempIP);
    saDest.sin_family = AF_INET;
    saDest.sin_port = 0;
    for (int nLoop = 0; nLoop < pThreadInfo->nRetries; nLoop++)
    {
    if (WaitForSingleObject(pThreadInfo->m_hEventKillPing,0)== WAIT_OBJECT_0)
    break;
    // Send ICMP echo request
    SendEchoRequest(rawSocket, &saDest);
    ::PostMessage(pThreadInfo->m_hwndNotifyWnd,WM_MSG_SEARCH_DISPLAY,0,(LPARAM)ip2);
    nRet=WaitForEchoReply(rawSocket);
    if (!nRet)
    {
    }
    else
    {
    if (WaitForSingleObject(pThreadInfo->m_hEventKillPing,0)== WAIT_OBJECT_0)
    break;
    // Receive reply
    dwTimeSent = RecvEchoReply(rawSocket, &saSrc, &cTTL);
    // Calculate elapsed time
    dwElapsed = GetTickCount() - dwTimeSent;
    ::PostMessage(pThreadInfo->m_hwndNotifyWnd,WM_MSG_SEARCH_FIND,0,(LPARAM)ip2);
    //
    }
    ::Sleep(20);
    }
    tempIP++;

    }
    nRet = closesocket(rawSocket);

    WSACleanup();
    ::PostMessage(pThreadInfo->m_hwndNotifyWnd,WM_MSG_SEARCH_PINGSTOP,0,0);
    return 1;
    }typedef struct CPingThreadInfo
    {
    //IP Zone
    unsigned int uintIP[2];
    //Retry count
    int nRetries;
    int nThreshold;
    HANDLE m_hEventKillPing;
    HWND m_hwndNotifyWnd;
    } CPingThreadInfo;
    下面是调用的过程         CPingThreadInfo m_ThreadInfo; UpdateData(true); m_ipfrom.GetAddress(ipfrom);
    m_ipto.GetAddress(ipto);
    if(ipfrom > ipto)
    {
    AfxMessageBox("Input error!");
    return;
    }
    m_ThreadInfo.uintIP[0]=ipfrom;
    m_ThreadInfo.uintIP[1]=ipto;
    m_ThreadInfo.m_hwndNotifyWnd = this->GetSafeHwnd();
    m_Thread= AfxBeginThread(PingThread,&m_ThreadInfo);结束扫描SetEvent(m_ThreadInfo.m_hEventKillPing);如果好用就给分,马上到3个角了:),赚点分