#include <stdio.h>
#include <string.h>
char YN(int k) {
FILE *f
char fn[40]
char ln[80]
char yn
薯弊岩 int n
数御yn='N'
sprintf(fn,"d:\\ping%d.txt",k)
f=fopen(fn,"r")
if (NULL!=f) {
n=0
while (1) {
if (NULL==fgets(ln,80,f)) break//
if (strstr(ln,"ms ")) {
yn='Y'
break//
}
n++
if (n>=4) break//
}
fclose(f)
}
return yn
}
void main(int argc,char **argv) {
char cmdstr[256]
int i
int IP[3]
char c
if (argc<2) {
USAGE:
printf("Usage example:\n %s 192.168.60.\nto test 192.168.60.1-254\n",argv[0])
return
}
if (4==sscanf(argv[1],"%d.%d.%d%c",&IP[0],&IP[1],&IP[2],&c)) {
if (0<=IP[0] && IP[0]<=255
卜此 && 0<=IP[1] && IP[1]<=255
&& 0<=IP[2] && IP[2]<=255
&& '.'==c) {
for (i=1i<255i++) {
sprintf(cmdstr,"cmd /c ping %s%d -n 1 -w 1000 >d:\\ping%d.txt",argv[1],i,i)
WinExec(cmdstr,SW_HIDE)
}
Sleep(3000)
for (i=1i<255i++) {
printf("%c %s%d\n",YN(i),argv[1],i)
}
Sleep(3000)
WinExec("cmd /c del /q d:\\ping*.txt",SW_HIDE)
} else goto USAGE
} else goto USAGE
}
/含袜者/关键代码如下:#include "icmpapi.h"
#include "iphlpapi.h"
#pragma comment(lib,"谈薯iphlpapi.lib")
bool CPINGDlg::startping(char* IP) //成功好孝返回true, 失败返回false
{
HANDLE hIcmpFile = NULL
unsigned long ipaddr = INADDR_NONE
DWORD dwRetVal = 0
char SendData[] = "Data Buffer\0"
LPVOID ReplyBuffer = NULL
DWORD ReplySize = 0
// PICMP_ECHO_REPLY reply
ipaddr = inet_addr(IP)
ReplySize = sizeof(ICMP_ECHO_REPLY)+sizeof(SendData)
ReplyBuffer = (void*)malloc(ReplySize)
hIcmpFile = IcmpCreateFile()
if (hIcmpFile == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("Ping 内部代码错误"))
return false
}
dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 1000)
// CloseHandle(hIcmpFile)
// free(ReplyBuffer)
if (dwRetVal != 0)
{
return true
}
else
{
return false
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)