在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
相关文档:
本不想写这些玩意儿,其实我并不喜欢它们,只不过没办法,谁叫我是靠这些家伙糊口的呢!
若干年前,我自认为很喜欢Linux;若干年后,我发现我当初是何等的天真。
恩,一分钱,一分货!我坚信这一点!所以我现在坚持用MS的Vista,正版的哦!
言归正传,前段时间因为工作,需要在Linux下操作鼠标,当然啦,肯定不会是在X-w ......
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 ......
一、svn客户端安装及环境配置.
果所在的linux机器上没有安装svn客户端,则首先安装svn客户端:
1. subversion-1.4.3.tar.bz2 subversion-deps-1.4.3.tar.bz2
2. 使用
tar xvfj subversion-1.4.3.tar.bz2
t ......