/* PING.c: Client ping the server Compiler: BC++ 3.1, Turbo C++ 1.01 (3.01) MSVC 1.52 Compile mode: large Project: PING.c ..\..\..\..\Lib\uPAC5000.lib ..\..\..\Lib\tcp_dm32.lib Description ICMP (Internet Control Message Protocol) is normally used to communicate error messages between IP nodes, both routers and hosts,but it is occasionally used by applications. 0--------7 8----------15 16------------31 type code checksum Ping type: 8 echo request type: 0 echo reply TimeStamp type: 13 timestamp request type: 14 timestamp reply Example: ping1 192.168.30.11 (remote host ip) [Dec 30, 2011] by Liam */ #include #include "..\..\..\..\lib\upac5000.h" #include "..\..\..\lib\Tcpip32.h" int Ethernet_Init(void) { /* Return: 0: ok. <0: error */ int iRet; iRet=NetStart(); if(iRet!=NoError) return -1; return 0; } void main(int argn, char *argv[]) { int i, err, stat, conno, recsiz=64; long li1; unsigned long *ulp, ul1; unsigned char *cp; static char buff[MAXBUF]; char *pname; struct pingm { struct ICMPhdr hdr; unsigned long ul[MAXBUF/4]; } pingm, *pp; InitLib(); // Initial the Ethernet adapter err=Ethernet_Init(); if(err==NoError) Print("Inint Ethernet ok.\r\n"); else { Print("Inint Ethernet error.\r\n"); return; } if(argn==1) { Print("syntax: ping1 n.n.n.n\r\n"); return; } for(i=1; ii) { Print("largest usable size %d\r\n", i); return; } SOCKET_RXTOUT(conno, 0); for(li1=1; ; li1++) { stat=0; Nwrite(conno, (char *) &pingm, recsiz+8); for(ul1=TimeMS(); TimeMS()-ul1<1000; ) { YIELD(); i=Nread(conno, buff, sizeof(buff)); if(i<=0) continue; stat=1; pp=(struct pingm *) buff; if(pp->hdr.type!=IC_EREP) { Print("funny response\r\n"); return; } cp=SOCKET_IPADDR(conno).c; Print("%4ld reply from %d.%d.%d.%d\r\n", li1, cp[0], cp[1], cp[2], cp[3]); } if(stat==0) Print("%4ld no reply\r\n", li1); if(Kbhit() && Getch()=='q') break; // press the 'q' key to quit } Nclose(conno); Nterm(); }