描述:
我想在asp页面上加一个按钮,当用户点击之后,服务器(客户端也行)就会去ping一个或一组ip,完毕后将结果返回到页面上。当然直接去执行ping命令可以解决问题,但是ping会返回一些无用信息且对一些复杂操作无能为力,所以我想写一个activex控件去模拟ping的功能,不知道该如何实现?以前都是直接用别人写的
,现在只好自己写了,而且很急,希望各位高手能指点一二。。
问题一:单纯用c语言写一个有此功能的exe,要用到那些函数?????
问题二:这一插件运行在web服务器和客户端在功能和写法上有何区别?在编写时应该注意哪些问题?
问题三:如上一问题不好回答,能否给推荐一本书或电子文档我自己去学习。
多谢了!!!
解决方案1:
下面的函数是使用多线程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 nZ />
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);
VC6.0+多线程