用c/c++实现linux下检测网络接口状态
2008-11-21 13:48
706 查看
要写个检测网络接口链接状态的东西,又不喜欢不断的ping别的地址,也不想调用其他命令行工具来做这个,于是在google了n多内容未果之后,搜刮到个检测工具的源代码,从里面抠出来一块儿就ok了,版权不是我的哦,哈哈……
以下代码在fedora 9 / CentOS 5.2下调试通过:)
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <errno.h>
#include <net/if.h>
struct ethtool_value {
__uint32_t cmd;
__uint32_t data;
};
int main(int , char* [])
{
struct ethtool_value edata;
int fd = -1, err = 0;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, "eth0");
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("Cannot get control socket");
return 70;
}
edata.cmd = 0x0000000a;
ifr.ifr_data = (caddr_t)&edata;
err = ioctl(fd, 0x8946, &ifr);
if (err == 0) {
fprintf(stdout, "Link detected: %s/n",
edata.data ? "yes":"no");
} else if (errno != EOPNOTSUPP) {
perror("Cannot get link status");
}
return 0;
}
以下代码在fedora 9 / CentOS 5.2下调试通过:)
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <errno.h>
#include <net/if.h>
struct ethtool_value {
__uint32_t cmd;
__uint32_t data;
};
int main(int , char* [])
{
struct ethtool_value edata;
int fd = -1, err = 0;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, "eth0");
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("Cannot get control socket");
return 70;
}
edata.cmd = 0x0000000a;
ifr.ifr_data = (caddr_t)&edata;
err = ioctl(fd, 0x8946, &ifr);
if (err == 0) {
fprintf(stdout, "Link detected: %s/n",
edata.data ? "yes":"no");
} else if (errno != EOPNOTSUPP) {
perror("Cannot get link status");
}
return 0;
}
相关文章推荐
- 用c/c++实现linux下检测网络接口状态
- 用c/c++实现linux下检测网络接口状态 .
- 用c/c++实现linux下检测网络接口状态
- 用C/C++实现linux下检测网络接口状态
- 用c/c++实现linux下检测网络接口状态
- 用c/c++实现linux下检测网络接口状态
- Linux下如何实现网络状态检测
- Linux C 怎么检测网络状态
- Linux下如何用C编程实现检测网卡与网线的连接状态
- arm linux c/c++实现ping网络测试
- GTK 程序 检测 网线是否连接 本地网络状态 C语言实现
- Linux ifstat --网络接口检测工具
- VC++实现检测网络状态所有的TCP与UDP通信
- C# ping网络IP 实现网络状态检测
- 网络接口link状态检测
- linux下C/C++网络编程基本:socket实现tcp和udp的例子
- Linux下如何实现状态检测
- VC++实现检测网络状态所有的TCP与UDP通信
- Linux下如何实现状态检测