zoukankan      html  css  js  c++  java
  • linux c 检测网络状态

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

    You want to look at the nodes in

    /sys/class/net/

    I experimented with mine:

    Wire Plugged in:

    eth0/carrier:1
    eth0/operstate:unknown

    Wire Removed:

    eth0/carrier:0
    eth0/operstate:down

    Wire Plugged in Again:

    eth0/operstate:up
    eth0/carrier:1

    GTK 程序 检测 网线是否连接 本地网络状态 C语言实现

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

    主程序创建一个进程, 每2秒查看一下网络状态,然后打印输出

             通过检查文件

            /sys/class/net/wlan0/operstate  (无线网络)

            /sys/class/net/eth0/operstate (有线网络)

          通过检查文件的内容   判断当前网络是否连接 

           值为up的时候,是连接 值为down的时候 是断开

           

    [cpp] view plain copy
     
    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <unistd.h>  
    4. #include <gtk/gtk.h>   
    5. #include <fcntl.h>  
    6. #include <sys/types.h>  
    7. #include <sys/stat.h>  
    8.   
    9.   
    10. #define WIRELESS  
    11.   
    12. #ifdef WIRELESS  
    13. #define INTERNET_STATUS_FILE "/sys/class/net/wlan0/operstate"  
    14. #else  
    15. #define INTERNET_STATUS_FILE "/sys/class/net/eth0/operstate"  
    16. #endif  
    17.   
    18. #include <sys/wait.h>  
    19.   
    20. static GtkWidget *fixed;   
    21. static GtkWidget *button1;   
    22. static GtkWidget *button2;   
    23. int running = 1;  
    24.   
    25.   
    26.   
    27. void ring()  
    28. {  
    29.     GtkWidget *window;  
    30.     GtkWidget *table;  
    31.     GtkWidget *button_cancel;  
    32.     GtkWidget *label;  
    33.   
    34.     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);  
    35.     gtk_window_set_default_size (GTK_WINDOW(window),100,100);  
    36.     gtk_window_set_position (GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);  
    37.     table = gtk_table_new(10,10,TRUE);  
    38.     gtk_container_add (GTK_CONTAINER (window),table);  
    39.     label = gtk_label_new("ring");  
    40.     button_cancel = gtk_button_new_with_label ("quit");  
    41.     gtk_table_attach_defaults (GTK_TABLE(table),button_cancel,2,4,7,9);  
    42.   
    43.   
    44.     gtk_widget_show_all(window);  
    45.   
    46.   
    47. }  
    48.   
    49.   
    50. void www_connect_check_real ()  
    51. {  
    52.     int ret = -2;  
    53.     while (1)  
    54.     {  
    55.         //一定要只读模式打开,读写模式打开不可以  
    56.         ret = open ("/sys/class/net/wlan0/operstate",O_RDONLY);  
    57.   
    58.         if (ret<0) {  
    59.         printf("open file operstate failure%d ",ret);  
    60.         return;  
    61.         }  
    62.         char status[3]="wl";  
    63.         //printf("%s",status);  
    64.         read (ret,status,2);  
    65.         status[3] = '';  
    66.         if (0== strcmp("up",status))  
    67.         {  
    68.             printf("on line now  ");  
    69.         }  
    70.         else if (0== strcmp("do",status))  
    71.         {  
    72.             printf("off off  ");  
    73.         }  
    74.         else   
    75.         printf("unknow error ");  
    76.         close (ret);  
    77.   
    78.         sleep (5);  
    79.     }  
    80.       
    81. }  
    82.   
    83.   
    84.   
    85.   
    86.   
    87.   
    88. int main(int argc,char* argv[])   
    89. {   
    90.     GtkWidget *window,*view;   
    91.     GtkWidget *vbox,*button,*label;  
    92.   
    93.   
    94.     /*线程初始化*/  
    95.     if (!g_thread_supported())   
    96.     g_thread_init(NULL);   
    97.   
    98.     gdk_threads_init();  
    99.   
    100.   
    101.   
    102.   
    103.   
    104.   
    105.     gtk_init(&argc,&argv);   
    106.     window=gtk_window_new(GTK_WINDOW_TOPLEVEL);   
    107.     gtk_window_set_title(GTK_WINDOW(window),"thread apllication");   
    108.   
    109.       
    110.   
    111.     vbox=gtk_vbox_new(FALSE,0);   
    112.     gtk_container_add(GTK_CONTAINER(window),vbox);   
    113.   
    114.     label=gtk_label_new("Notice! Button is moving");   
    115.     gtk_box_pack_start(GTK_BOX(vbox),label,FALSE,FALSE,0);   
    116.     view=gtk_viewport_new(NULL,NULL);   
    117.     gtk_box_pack_start(GTK_BOX(vbox),view,FALSE,FALSE,0);   
    118.     fixed=gtk_fixed_new();   
    119.     gtk_widget_set_usize(fixed,330,330);   
    120.     gtk_container_add(GTK_CONTAINER(view),fixed);   
    121.     button1=gtk_button_new_with_label("1");   
    122.     button2=gtk_button_new_with_label("2");   
    123.     gtk_fixed_put(GTK_FIXED(fixed),button1,10,10);   
    124.     gtk_fixed_put(GTK_FIXED(fixed),button2,40,40);   
    125.   
    126.   
    127.     GtkWidget *button_ring = gtk_button_new_with_label ("ring");  
    128.     gtk_box_pack_start(GTK_BOX(vbox),button_ring,FALSE,FALSE,5);   
    129.     g_signal_connect (button_ring,"clicked",G_CALLBACK(ring),NULL);  
    130.   
    131.     /*创建线程*/  
    132.     g_thread_create((GThreadFunc)www_connect_check_real,NULL,FALSE,NULL);     
    133.       
    134.   
    135.       
    136.   
    137.     gtk_widget_show_all(window);   
    138.     g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);   
    139.     gtk_container_set_border_width(GTK_CONTAINER(window),10);     
    140.     gdk_threads_enter();   
    141.     gtk_main();   
    142.     gdk_threads_leave();   
    143.   
    144.   
    145.   
    146.   
    147.     return FALSE;   
    148. }  

     注意:

              1,ubuntu 10.04上,当使用静态IP的时候,即使 连接着网络, 文件的值也不up 而是unknown, 这是驱动上的bug,ubuntu 11.10上就正常

     http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/5/html/5.7_Technical_Notes/kernel.html

              2,相关链接

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

    http://www.linuxforums.org/forum/programming-scripting/142431-how-write-program-c-detect-ethernet-cable.html

               3,也可以使用shell脚本来检测,网上例子很多

               4,也可以编写一个内核模块来检测,后期再说,先找工作

    http://stackoverflow.com/questions/7225888/how-can-i-monitor-the-nic-statusup-down-in-a-c-program-without-polling-the-ker

               5,

     [cpp] view plain copy

     
    1. //RFC 2863操作状态  
    2. unsigned char       operstate;  
    3. /* operstate的可能取值如下: 
    4. enum { 
    5.     IF_OPER_UNKNOWN, 
    6.     IF_OPER_NOTPRESENT, 
    7.     IF_OPER_DOWN, 
    8.     IF_OPER_LOWERLAYERDOWN, 
    9.     IF_OPER_TESTING, 
    10.     IF_OPER_DORMANT, 
    11.     IF_OPER_UP, 
    12. }; 
    13.  **/   
    14. //映射到RFC2863兼容状态的策略  
    15. unsigned char       link_mode;  
    16. /* link_mode的可能取值如下: 
    17. enum { 
    18.     IF_LINK_MODE_DEFAULT, 
    19.     IF_LINK_MODE_DORMANT, 
    20. }; 
    21.  **/  


    我们检测的文件内容,其实是这个成员变变量的值,返回值比较多,程序中只是判断了前两个字符

    http://blog.csdn.net/npy_lp/article/details/7056903


    linux c 检测网络状态

    转自:https://blog.csdn.net/linqiongjun86/article/details/48393807

    获取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 ",fp);  
            return;  
            }  
            
        memset(status,0,sizeof(status));
            ret = read (fp,status,10);   
        printf("status:%s ",status);
            if (NULL != strstr(status,"up"))  
            {  
                printf("on line now ");  
            }  
            else if (NULL != strstr(status,"down"))  
            {  
                printf("off off ");  
            }  
            else   
            printf("unknow error ");  
            close (fp);      

  • 相关阅读:
    03、使用字符串
    加载selenium2Library失败---robotframework环境搭建(site-packages下无selenium2library文件夹)
    python无法启动火狐浏览器且报错“selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities”
    移动H5前端性能优化指南
    appium+python 启动一个app步骤
    Appium_Python_Client介绍
    python自动化---各类发送邮件方法及其可能的错误
    python自动化--批量执行测试之生成报告
    揭秘webdriver实现原理【转】
    selenium 三种断言以及异常类型
  • 原文地址:https://www.cnblogs.com/liushui-sky/p/9236213.html
Copyright © 2011-2022 走看看