user-0 提问: 如何用C语言调用ping命令

回复:
user: user-1
content: 引用 10 楼 philip_puma 的回复:[quote=引用 2 楼 mujiok2003 的回复:] #include <stdio.h> #include <stdlib.h> #define TARGET "www.baidu.com" int main( ) {    char   psBuffer[512];    FILE   *pPipe;     if( (pPipe = _popen( "ping -n 10 " TARGET, "rt" )) == NULL )       exit( 1 );     while(fgets(psBuffer, 512, pPipe))    {      printf("%s", psBuffer);    }    _pclose(pPipe); }    这个碉堡了好么？[/quote]我测试了，好象不行，我随意写了一个不存在的局域网地址，然后居然也通了

user: user-2
content: 引用 7 楼 zhao4zhong1 的回复:仅供参考#include <windows.h> #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=1;i<255;i++) {                 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=1;i<255;i++) {                 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; }   高人呀！

user: user-3
content: 仅供参考#include <windows.h> #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=1;i<255;i++) {                 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=1;i<255;i++) {                 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; }

user: user-4
content: 以前我想用自己的程序发邮件，找了好几个现成的库，都不太满足要求，于是直接去了解邮件协议，按照协议进行连接、通讯，最终成功发送了。

user: user-4
content: 你查查ping命令是如何通讯的，然后按照其方式自己编写程序进行ping就行了。

user: user-5
content: 我也不知道哎

user: user-6
content: 看看下边这个函数，可判断链路是否正常 netSt = get_netlink_status("eth1");//调用 // -1 -- error , details can check errno // 1 -- interface link up // 0 -- interface link down. int get_netlink_status(const char *if_name) {     int skfd;     struct ifreq ifr;     struct ethtool_value edata;      edata.cmd = ETHTOOL_GLINK;     edata.data = 0;      memset(&ifr, 0,sizeof(ifr));     strncpy(ifr.ifr_name, if_name,sizeof(ifr.ifr_name)- 1);     ifr.ifr_data =(char *) &edata;      if (( skfd= socket(AF_INET, SOCK_DGRAM, 0 )) < 0)         return -1;      if(ioctl( skfd, SIOCETHTOOL,&ifr ) == -1)     {         close(skfd);         return -1;     }      close(skfd);     return edata.data;   return 0; }

user: user-7
content: #include <stdio.h> #include <stdlib.h> #define TARGET "www.baidu.com" int main( ) {    char   psBuffer[512];    FILE   *pPipe;     if( (pPipe = _popen( "ping -n 10 " TARGET, "rt" )) == NULL )       exit( 1 );     while(fgets(psBuffer, 512, pPipe))    {      printf("%s", psBuffer);    }    _pclose(pPipe); }

user: user-7
content: 试试管道吧。

user: user-8
content: 引用 2 楼 mujiok2003 的回复:#include <stdio.h> #include <stdlib.h> #define TARGET "www.baidu.com" int main( ) {    char   psBuffer[512];    FILE   *pPipe;     if( (pPipe = _popen( "ping -n 10 " TARGET, "rt" )) == NULL )       exit( 1 );     while(fgets(psBuffer, 512, pPipe))    {      printf("%s", psBuffer);    }    _pclose(pPipe); }    这个碉堡了好么？

user: user-9
content: 你看一下ping程序的实现原理