gethostbyaddr()扫描不存在IP时的等待时间可否改变? gethostbyaddr()扫描不存在IP时的等待时间可否改变?第三个参数有哪些取值? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的思路是:用gethostbyaddr()扫描局域网IP,通过gethostbyaddr()对指定的IP进行探测,若返回值为NULL,则说明该IP不存在,否则,将该IP添加到列表框中,在探测下一个IP.但是如果该IP不存在,整个程序要等几十秒才能开始探测下一个IP,我能不能自己控制gethostbyaddr()的等待时间,比如说0.2秒后无反应就探测下一个IP。 gethostbyaddr() 如何调整超时我不知道, 想来也不是很方便, 倒不如考虑考虑建一个表, 然后一次扫描一批IP地址, 等待返回即可.时间和效率上恐怕都要比你的 0.2 秒要好. 类似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个角了:),赚点分 设置SELECT语句时的一点疑惑 cedit限定输入最大值 错误弹出界面 用MFC做的ActiveX控件,怎样打印控件中的内容. 如何实现:文本框右侧有两个并排的小按钮,一个是向上的箭头,一个是向下的箭头 请问CSocket的Attach,Detach有什么作用,在什么场合下用比较合适,急.... 程序员上午考试,我的情况 怎样得到一个字节的低四位?(初学者) vc++ 作图的问题???? 如何动态修改网页内容? 编写的动态库项目调用另一动态库函数时出现的错误,请大家帮忙看一下。 有没有人做过自定义文件夹? 我的Event接口的out参数long型怎么没用,每次返回的都是0?
但是如果该IP不存在,整个程序要等几十秒才能开始探测下一个IP,我能不能自己控制gethostbyaddr()的等待时间,比如说0.2秒后无反应就探测下一个IP。
开始扫描
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个角了:),赚点分