在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) 在系统 ......
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 ......
在Linux下进行C语言
编程,必然要采用GNU GCC来编译C源代码生成可执行程序。
一、GCC快速
入门
Gcc指令的一般格式为:Gcc [选项] 要编译的文件 [选项] [目标文件]
其中,目标文件可缺省,Gcc默认生成可执行的文件名为:编译文件.out
我们来看一下经典
入门程序"Hello World!"
# vi hello.c
#include ......
Unix基础
登录和退出
登陆:可以用Neterm、SecureCRT等终端登陆工具〔192.168.25.12 常用软件下有〕或者
telnet、ftp命令。
1. telnet 远程终端访问
命令格式:telnet [hostname|IP address]
退出:ctrl+d or logout or exit
2. ftp 远程文件传输
......
用户系统相关命令
用户切换
用户切换: su [选项] [使用者]
用户管理
增加用户账号: useradd [选项] 用户名
设置用户账号属性: usermod [选项] 属性值
删除对应用户账号: userdel [选项] 用户名
增加组账号: ......