在Linux下面的调用ping
我们在网络编程的时候经常要检测系统是否得到了有效的IP,是否可以正确联通到网络里面。通常的检查方法就是使用PING命令。
而在程序里面如何自动让程序知道哩?有两种办法,
一种是利用系统的能力,直接调用系统的shell,如方法一就可以在linux系统中很好使用。
另外一种是直接在代码里面实现ping命令,通过调用函数的形式来获取系统的状态。
还有更好的办法么,如有请回复。
方法一、
#include<stdlib.h>
void main()
{
int i;
int count=0;
while(1)
{
i=system("ping -c 1 10.27.60.122");
printf("\ni=%d",i);
count++;
if(i==0)
{
i=system("settop install;settop odtest.out");
break;
}
if(count>3)
{
printf("\n error cannot reach 10.27.60.122");
system("cd /root;ls;settop install;settop odtest.out");
break;
}
}
}
方法二、
/*
* P I N G . C
*
* Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
* measure round-trip-delays and packet loss across network paths.
*
* Author -
* Mike Muuss
* U. S. Army Ballistic Research Laboratory
* December, 1983
* Modified at Uc Berkeley
*
* Changed argument to inet_ntoa() to be struct in_addr instead of u_long
* DFM BRL 1992
*
* Status -
* Public Domain. Distribution Unlimited.
*
* Bugs -
* More statistics could always be gathered.
* This program has to run SUID to ROOT to access the ICMP socket.
*/
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h&g
相关文档:
一、概要
详细介绍:http://www.ibm.com/developerworks/cn/linux/l-ipc/part2/index1.html
信号是UNIX和Linux系统响应结条件而产生的一个事件。接收到信号的进程会相应地采取一些行动。
(1) 在系统 ......
1. 关于/etc/host,主机名和IP配置文件
Hosts - The static table lookup for host name(主机名查询静态表)
Linux 的/etc/hosts是配置ip地址和其对应主机名的文件,这里可以记录本机的或其他主机的ip及其对应主机名。不同的linux版本,这个配置文件也可能不同。比如Debian的对应文件是/etc/hostname。
2. 配置文件 ......
4)链接阶段
(Link)
在成功编译之后,就进入了链接阶段。
无选项链接
用法:[root]# gcc hello.o –o hello.exe
作用:将编译输出文件hello.o链接成最终可执行文件hello.exe。
[root]# ls
hello.c hello.exe hello.i hello.o hello.s
运行该可执行文件,出现正确的结果如下。
[roo ......
Unix基础
登录和退出
登陆:可以用Neterm、SecureCRT等终端登陆工具〔192.168.25.12 常用软件下有〕或者
telnet、ftp命令。
1. telnet 远程终端访问
命令格式:telnet [hostname|IP address]
退出:ctrl+d or logout or exit
2. ftp 远程文件传输
......