在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
相关文档:
1. 关于/etc/host,主机名和IP配置文件
Hosts - The static table lookup for host name(主机名查询静态表)
Linux 的/etc/hosts是配置ip地址和其对应主机名的文件,这里可以记录本机的或其他主机的ip及其对应主机名。不同的linux版本,这个配置文件也可能不同。比如Debian的对应文件是/etc/hostname。
2. 配置文件 ......
GCC 支持了许多不同的语言,包括 C、C++、Ada、Fortran、Objective C,Perl、Python 和 Ruby,甚至还有Java。
Linux 内核和许多其他自由软件以及开放源码应用程序都是用 C 语言编写并使用 GCC 编译的。
编译C++程序:
-c 只编译不连接
g++ file1 -c -o file1.o
g++ file2 -c -o file2.o
g++ f ......
2)编译阶段
(Compiling)
第二步进行的是编译阶段,在这个阶段中,Gcc首先要检查代码的规范性、是否有语法错误等,以确定代码的实际要做的工作,在检查无误后,Gcc把代码翻译成汇编语言。用户可以使用”-S”选项来进行查看,该选项只进行编译而不进行汇编,生成汇编代码。
选项 -S
用法:[root]# gcc &ndash ......
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 ......
很久没有用linux了,由于要学习linux下的内核编程又要重新拾起linux,用了这么久的windows对linux都快忘完了。所以边看资料和操作来复习一下linux的基本操作。
一、linux的目录结构
首先,linux的文件系统即本身是由VFS即Virtual File System Switch(虚拟文件系统)来实文件管理的,VFS本身是一个文件档案管理系统的一个 ......