用C/C++實現linux下檢測網絡接口狀態(tài)
本文實例為大家分享了使用C/C++實現linux下檢測網絡接口狀態(tài),供大家參考,具體內容如下
要寫個檢測網絡接口鏈接狀態(tài)的東西,又不喜歡不斷的ping別的地址,也不想調用其他命令行工具來做這個,于是在google了n多內容未果之后,搜到個檢測工具的源代碼。
以下代碼在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;
};
/*return 1:has cable; return 0:no cable*/
int detect_eth_cable(char *ifname)
{
struct ethtool_value edata;
struct ifreq ifr;
int fd = -1, err = 0;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, ifname);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
//perror("Cannot get control socket");
return -1;
}
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(edata.data==1 ? 1:0);
}
int main(int argc, char**argv)
{
detect_eth_cable("p1p1");
return 0;
}
其他代碼:
int get_netportstatus(const char *interface) {
char cmd[1024];
char *tt;
FILE *fp;
int devflag;
devflag=get_netflag(interface);
if (devflag==DEV_DOWN) {
sprintf(cmd,"ifconfig %s up",interface);
system(cmd);
}
sprintf(cmd,"ethtool %s | grep \"Link detected\" > /tmp/eth.temp",interface);
system(cmd);
if (devflag==DEV_DOWN) {
sprintf(cmd,"ifconfig %s down",interface);
system(cmd);
}
fp=fopen("/tmp/eth.temp","r");
if (fp==NULL) {
system("rm -rf /tmp/eth.temp");
return -1;
}
fgets(cmd,1024,fp);
fclose(fp);
system("rm -rf /tmp/eth.temp");
tt=strstr(cmd,"no");
if (tt!=NULL) return LINK_DOWN;
tt=strstr(cmd,"yes");
if (tt!=NULL) return LINK_UP;
return -1;
}
#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;
}
#include <net if.h=""> // IFF_RUNNING
//如果網卡已臉上網線,返回0,否則返回-1.
int check_nic(char *nic)
{
struct ifreq ifr;
int skfd = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, nic_name);
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
{
return -1;
}
if(ifr.ifr_flags & IFF_RUNNING)
return 0; // 網卡已插上網線
else return -1;
}
</net>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
上一篇:C++檢測鍵盤某鍵是否按下的方法
欄 目:C語言
本文標題:用C/C++實現linux下檢測網絡接口狀態(tài)
本文地址:http://m.jygsgssxh.com/a1/Cyuyan/743.html
您可能感興趣的文章
- 04-02c語言函數調用后清空內存 c語言調用函數刪除字符
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言用函數寫分段 用c語言表示分段函數
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段函數
- 04-02c語言調用函數求fibo C語言調用函數求階乘
- 01-10數據結構課程設計-用棧實現表達式求值的方法詳解
- 01-10使用OpenGL實現3D立體顯示的程序代碼
- 01-10HDOJ 1443 約瑟夫環(huán)的最新應用分析詳解
- 01-10深入理解C++中常見的關鍵字含義


閱讀排行
本欄相關
- 04-02c語言函數調用后清空內存 c語言調用
- 04-02func函數+在C語言 func函數在c語言中
- 04-02c語言的正則匹配函數 c語言正則表達
- 04-02c語言用函數寫分段 用c語言表示分段
- 04-02c語言中對數函數的表達式 c語言中對
- 04-02c語言編寫函數冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數 round c語言
- 04-02c語言分段函數怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數 c語言中怎
- 04-02c語言調用函數求fibo C語言調用函數求
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 01-11ajax實現頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


