cAKK的方法怎么控制时间呢?
我是这样写的,不知你对TCP/IP熟不熟,需要用到ICMP,我有两个ICMP的头文件.
有感兴趣的我可以发MAIL或贴出来(有点长);
下面的代码你可以改为DELPHI的,需要LoadLibrary("ICMP.DLL"),函数声明如下:
HANDLE __stdcall(*IcmpCreateFile)(void);
bool __stdcall (*IcmpCloseHandle)(HANDLE IcmpHandle );
DWORD __stdcall (*IcmpSendEcho)(
HANDLE IcmpHandle,
IPAddr DestinationAddress,
LPVOID RequestData,
WORD RequestSize,
PIP_OPTION_INFORMATION RequestOptions,
LPVOID ReplyBuffer,
DWORD ReplySize,
DWORD Timeout
);
TStrings* __fastcall TMainFrm:
ingTest(TStrings *IPStrings) // daizhengli
{
PIP_OPTION_INFORMATION IPOpt; //IpOption
PICMP_ECHO_REPLY PIPecho;
PChar pReqData,pRevData;
DWORD FIPAddress;
DWORD FSize;
DWORD FTimeOut;
DWORD FBufsize;
AnsiString MyString;
int i,j;
LPVOID lpMsgBuf;
AnsiString ErroMsg;
FSize = 40;
FBufsize = sizeof(ICMP_ECHO_REPLY) + FSize;
PingMemo->Lines->Clear();
PIPecho = (PICMP_ECHO_REPLY)AllocMem(FBufsize); //Remeber del
IPOpt = (PIP_OPTION_INFORMATION)AllocMem(sizeof(IP_OPTION_INFORMATION));//rem del
pRevData = new char[FSize];
PIPecho->Data = pRevData;
FTimeOut = 4000;
MyString = StrEdit->Text;
pReqData = MyString.c_str();
//memset(&IPOpt,'0',sizeof(IP_OPTION_INFORMATION));
IPOpt->Ttl = 32;
for(i=0;i<IPStrings->Count;i++)
{
ifError = false;
MyErrorFlag = 0;
FIPAddress = inet_addr((IPStrings->Strings
).c_str());
PingMemo->Lines->Add(IPStrings->Strings);
try
{
for(j=0;j<3;j++)
{
CurrTime = TDateTime::CurrentDateTime();
if(IcmpSendEcho(hIcmp, FIPAddress, pReqData, MyString.Length(), IPOpt, PIPecho, FBufsize, FTimeOut)!=0)
{
PingMemo->Lines->Add(IntToStr((*PIPecho).DataSize)+" "+IntToStr((*PIPecho).RoundTripTime)+"/t"+AnsiString((*PIPecho).Options.Ttl)+" "+IntToStr((*PIPecho).Status)+" "+CurrTime.DateTimeString());
if((*PIPecho).Status!=IP_SUCCESS)
{
MyErrorFlag++;
MyErrorCode = (*PIPecho).Status;
}
}
else
{
int k = (*PIPecho).Status-IP_STATUS_BASE;
PingMemo->Lines->Add("IP地址未知或错误: "+ErrorStr[k-1]+CurrTime.DateTimeString());
ifError = true;
break;
}
}
if(MyErrorFlag>2)
HandleError(MyErrorCode);
else if(!ifError)
PingMemo->Lines->Add("状态正常");
}
catch(...)
{
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION);
SetLastError(0);
PingMemo->Lines->Add((LPCTSTR)lpMsgBuf);
LocalFree( lpMsgBuf );
IcmpCloseHandle(hIcmp);
SysFreeMem(PIPecho);
SysFreeMem(IPOpt);
delete pRevData;
break;
}
} //End for(ip)
SysFreeMem(PIPecho);
SysFreeMem(IPOpt);
delete pRevData;
return(PingMemo->Lines);
}