用C/C++實(shí)現(xiàn)linux下檢測(cè)網(wǎng)絡(luò)接口狀態(tài)
本文實(shí)例為大家分享了使用C/C++實(shí)現(xiàn)linux下檢測(cè)網(wǎng)絡(luò)接口狀態(tài),供大家參考,具體內(nèi)容如下
要寫個(gè)檢測(cè)網(wǎng)絡(luò)接口鏈接狀態(tài)的東西,又不喜歡不斷的ping別的地址,也不想調(diào)用其他命令行工具來(lái)做這個(gè),于是在google了n多內(nèi)容未果之后,搜到個(gè)檢測(cè)工具的源代碼。
以下代碼在fedora 9 / CentOS 5.2下調(diào)試通過(guò):)
#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 //如果網(wǎng)卡已臉上網(wǎng)線,返回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; // 網(wǎng)卡已插上網(wǎng)線 else return -1; } </net>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語(yǔ)言
本文標(biāo)題:用C/C++實(shí)現(xiàn)linux下檢測(cè)網(wǎng)絡(luò)接口狀態(tài)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/743.html
您可能感興趣的文章
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用函數(shù)刪除字符
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言用函數(shù)寫分段 用c語(yǔ)言表示分段函數(shù)
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段函數(shù)
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求階乘
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10HDOJ 1443 約瑟夫環(huán)的最新應(yīng)用分析詳解
- 01-10深入理解C++中常見(jiàn)的關(guān)鍵字含義


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文