linux c 检测网络状态

时间:2021-12-24 15:18:10

http://*.com/questions/808560/how-to-detect-the-physical-connected-state-of-a-network-cable-connector

http://blog.csdn.net/a954423389/article/details/7327950

获取wifi网络状态

#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


void connect_check_real ()  
{  
        int ret;  
    int fp;
    char status[10];  
        //一定要只读模式打开,读写模式打开不可以  
    ///sys/class/net/wlan0/carrier  0:down 1:up
        fp = open ("/sys/class/net/wlan0/operstate",O_RDONLY);  
 
        if (fp<0) {  
        printf("open file operstate failure%d\n",fp);  
        return;  
        }  
        
    memset(status,0,sizeof(status));
        ret = read (fp,status,10);   
    printf("status:%s\n",status);
        if (NULL != strstr(status,"up"))  
        {  
            printf("on line now \n");  
        }  
        else if (NULL != strstr(status,"down"))  
        {  
            printf("off off \n");  
        }  
        else   
        printf("unknow error\n");  
        close (fp);      
}