本文目錄一覽:
- 1、用C語言如何實現PING指定IP速度
- 2、請教如何用C語言實現ping命令
- 3、如何使用C語言來判斷ping命令是否能ping通,求代碼。 要c的不要c++或c#的。
- 4、如何用C語言調用ping命令
用C語言如何實現PING指定IP速度
windows編程比較煩
讀取記事本,讀入IP
system(“ping xx.xx.xx.xx text.txt”);
再讀取text.txt,分析裡面的速度值,取平均值,再跟剛才讀到的IP拼成字元串輸出。
linux上會簡單很多, sed+ping 就可以搞定了
請教如何用C語言實現ping命令
如果你想獲取到Ping的結果
那麼直接system 調用ping或者popen調用即可。
如果想自己實現,就需要用socket自行發Ping包,並獲取回應
這個就很麻煩了。 建議可以看一下gnu ping的源碼,或者busybox的ping部分代碼。
如何使用C語言來判斷ping命令是否能ping通,求代碼。 要c的不要c++或c#的。
代碼在 MAC OS 下運行良好,在 Linux 下得話需要稍作修改
#include stdio.h
#include fcntl.h
#include string.h
#include stdlib.h
#include unistd.h
int main(void)
{
char host[256], cmd[256];
printf(“please input dest_host:”);
scanf(“%s”, host);
strncpy(cmd, “ping -c5 “, 9);
strncat(cmd, host, strlen(host));
strncat(cmd, ” ping.txt”, 11);
pid_t pid = fork();
if(pid 0)
{
printf(“fork error\n”);
exit(-1);
}
if(pid==0)
{
if(execlp(“/bin/sh”, “sh”, “-c”, cmd, (char *)0) 0)
printf(“execlp error\n”);
exit(0);
}
if(waitpid(pid, NULL, 0) 0)
printf(“waitpid error\n”);
int fd = open(“ping.txt”, O_RDWR);
int n;
char buf[1024];
n = read(fd, buf, sizeof(buf));
if(n = 0)
{
printf(“read error\n”);
exit(-1);
}
if(strstr(buf, “100.0%”) == NULL)
printf(“can reach %s”, host);
else
printf(“can’t reach %s”, host);
close(fd);
return 0;
}
如果你想要 ping 程序,剛好我最近寫了一個,要的話私信
如何用C語言調用ping命令
#includestdio.h
#includeWindows.h
int main(void)
{
system(“ping 192.168.0.1
);
/*
這裡ping的是我自己電腦的ip地址(舉個例子),你可以換成你想要的,或者某個網站地址均可
*/
return 0;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/180363.html