;
CString strerr;
strerr.Format("err%d.wav",ipT+1);
sndPlaySound(strerr, SND_LOOP |SND_ASYNC );
HDC hdc= CreateDC("DISPLAY",0,0,0);
SelectObject(hdc,lpHost[ipT].hrgn);
InvertRgn(hdc,lpHost[ipT].hrgn);
DeleteDC(hdc);
bOK=FALSE;
}
else if(bOK) sndPlaySound("Bird0.wav", SND_ASYNC);
}
SetTimer(1, 30000,NULL);
CFormView::OnTimer(nIDEvent);
}
本程序设计为每10分钟对网络作一次自动测试。所以第一条语句须检查定时器中断是否已够20次(30秒*20=10分钟)。若够的话,就关断定时器,循环调用Autotest()对所有网络节点进行测试。若Autotest()的返回值为FALSE,说明该节点有问题,随即调用对应声波文件发出不停的报警声。在报警的同时,
程序继续往下运行,作余下网络节点的测试。若所有节点均正常,则调用BIRD0.WAV声波文件发出动听的鸟鸣声。全部节点测试完后,才用SetTimer()再次启动定时器。
报警声波文件的制作,可在WINDOWS的附件à娱乐à录音机,通过麦克风录下报警语音。按照在INFO.INI文件中各节点的顺序,将语音文件分别存为ERR1.WAV,ERR2.WAV……。这样,网络节点的报警声就能和出错节点正确对应。
测试时,若有2个以上网络节点有问题,前一个出错节点只会报警一次,最后一个出错节点则会发出循环报警声。为了便于用户观察出错情况,用InvertRgn()来反相显示出错节点的区域。InvalidateRect(NULL)函数用来使屏幕刷新,以便下一次测试的观察。
三、AUTOP.CPP的源码及说明
AUTO.CPP的源码如下:
//#include 头文件略
. . . . . . . . . . .
typedef struct _ihdr {
BYTE i_type, i_code;
u_short i_cksum,i_id, i_seq;
}IcmpHeader;
struct sockaddr_in saDestAddr;
CString Messtr,Tmpstr;
u_short checksum(u_short *buffer, int size) {
. . . . . . .(略)
}
BOOL Autotest(char far * szDestHost,int Ktest)
{
WSADATA wsaData;
SOCKET sockRaw;
struct sockaddr_in dest,from;
char icmp_data, rec
vbuf[100];
unsigned int addr=0;
int fromlen = sizeof(from);
int timeout = 1000; //ms
WSAStartup(MAKEWORD(2,1),&wsaData) ;
sockRaw = socket (AF_INET,SOCK_RAW,IPPROTO_ICMP);
setsockopt(sockRaw,SOL_SOCKET,SO_RCVTIMEO,
(char*)&timeout,sizeof(timeout) );
memset(&dest,0,sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_addr.s_addr= inet_addr(szDestHost);
memset(icmp_data,0,sizeof(icmp_data)); //clear icmp_data.
((IcmpHeader*)icmp_data)->i_type = 8; //ICMP_ECHO
((IcmpHeader*)icmp_data)->i_code = 0;
((IcmpHeader*)icmp_data)->i_id = (u_short)GetCurrentProcessId();
((IcmpHeader*)icmp_data)->i_seq = 0;
for(int k=0; k
{
((IcmpHeader*)icmp_data)->i_cksum = 0;
((IcmpHeader*)icmp_data)->i_seq ++;
((IcmpHeader*)icmp_data)->i_cksum=checksum((u_short*)icmp_data,8);
sendto(sockRaw,icmp_data,8,0,(struct sockaddr*)&dest,sizeof(dest));
int bread=recvfrom(sockRaw,recvbuf,1024,0,(struct sockaddr*)&from,
&fromlen);
if (bread == SOCKET_ERROR){
if(k==Ktest-1) goto ERR1 ;
else continue; //try again(3 times)
}
} &n