转自: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的时候 是断开

  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. #define WIRELESS
  9. #ifdef WIRELESS
  10. #define INTERNET_STATUS_FILE "/sys/class/net/wlan0/operstate"
  11. #else
  12. #define INTERNET_STATUS_FILE "/sys/class/net/eth0/operstate"
  13. #endif
  14. #include <sys/wait.h>
  15. static GtkWidget *fixed;
  16. static GtkWidget *button1;
  17. static GtkWidget *button2;
  18. int running = 1;
  19. void ring()
  20. {
  21. GtkWidget *window;
  22. GtkWidget *table;
  23. GtkWidget *button_cancel;
  24. GtkWidget *label;
  25. window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  26. gtk_window_set_default_size (GTK_WINDOW(window),100,100);
  27. gtk_window_set_position (GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);
  28. table = gtk_table_new(10,10,TRUE);
  29. gtk_container_add (GTK_CONTAINER (window),table);
  30. label = gtk_label_new("ring");
  31. button_cancel = gtk_button_new_with_label ("quit");
  32. gtk_table_attach_defaults (GTK_TABLE(table),button_cancel,2,4,7,9);
  33. gtk_widget_show_all(window);
  34. }
  35. void www_connect_check_real ()
  36. {
  37. int ret = -2;
  38. while (1)
  39. {
  40. //一定要只读模式打开,读写模式打开不可以
  41. ret = open ("/sys/class/net/wlan0/operstate",O_RDONLY);
  42. if (ret<0) {
  43. printf("open file operstate failure%d\n",ret);
  44. return;
  45. }
  46. char status[3]="wl\0";
  47. //printf("%s",status);
  48. read (ret,status,2);
  49. status[3] = '\0';
  50. if (0== strcmp("up",status))
  51. {
  52. printf("on line now \n");
  53. }
  54. else if (0== strcmp("do",status))
  55. {
  56. printf("off off \n");
  57. }
  58. else
  59. printf("unknow error\n");
  60. close (ret);
  61. sleep (5);
  62. }
  63. }
  64. int main(int argc,char* argv[])
  65. {
  66. GtkWidget *window,*view;
  67. GtkWidget *vbox,*button,*label;
  68. /*线程初始化*/
  69. if (!g_thread_supported())
  70. g_thread_init(NULL);
  71. gdk_threads_init();
  72. gtk_init(&argc,&argv);
  73. window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
  74. gtk_window_set_title(GTK_WINDOW(window),"thread apllication");
  75. vbox=gtk_vbox_new(FALSE,0);
  76. gtk_container_add(GTK_CONTAINER(window),vbox);
  77. label=gtk_label_new("Notice! Button is moving");
  78. gtk_box_pack_start(GTK_BOX(vbox),label,FALSE,FALSE,0);
  79. view=gtk_viewport_new(NULL,NULL);
  80. gtk_box_pack_start(GTK_BOX(vbox),view,FALSE,FALSE,0);
  81. fixed=gtk_fixed_new();
  82. gtk_widget_set_usize(fixed,330,330);
  83. gtk_container_add(GTK_CONTAINER(view),fixed);
  84. button1=gtk_button_new_with_label("1");
  85. button2=gtk_button_new_with_label("2");
  86. gtk_fixed_put(GTK_FIXED(fixed),button1,10,10);
  87. gtk_fixed_put(GTK_FIXED(fixed),button2,40,40);
  88. GtkWidget *button_ring = gtk_button_new_with_label ("ring");
  89. gtk_box_pack_start(GTK_BOX(vbox),button_ring,FALSE,FALSE,5);
  90. g_signal_connect (button_ring,"clicked",G_CALLBACK(ring),NULL);
  91. /*创建线程*/
  92. g_thread_create((GThreadFunc)www_connect_check_real,NULL,FALSE,NULL);
  93. gtk_widget_show_all(window);
  94. g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
  95. gtk_container_set_border_width(GTK_CONTAINER(window),10);
  96. gdk_threads_enter();
  97. gtk_main();
  98. gdk_threads_leave();
  99. return FALSE;
  100. }

注意:

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\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);      
}

linux c 检测网络状态的更多相关文章

  1. iOS开发——网络篇——数据安全(MD5),HTTPS,检测网络状态

    一.数据安全 1.提交用户的隐私数据一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 ...

  2. iOS开发网络篇—Reachability检测网络状态

    前言:当应用程序需要访问网络的时候,它首先应该检查设备的网络状态,确认设备的网络环境及连接情况,并针对这些情况提醒用户做出相应的处理.最好能监听设备的网络状态的改变,当设备网络状态连接.断开时,程序也 ...

  3. [iOS 多线程 & 网络 - 2.8] - 检测网络状态

    A.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的:(1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能)(2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验 ...

  4. iOS 检测网络状态 自动判断 认为提示网络改变

    检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是让用户了解自己的网络状态,防止一些误会(比如怪应用无能)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验WIFI\3G ...

  5. iOS开发 - 检测网络状态(WIFI、2G/3G/4G)

    本文转载至 http://blog.csdn.net/wangzi11322/article/details/45580917 检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是 ...

  6. iOS网络4——Reachability检测网络状态

    一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发. 其实在网络开发中还有比较常用的就是网络 ...

  7. iOS Reachability检测网络状态

    一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发.其实在网络开发中还有比较常用的就是网络状 ...

  8. iOS 检测网络状态

    一般有两种方式,都是第三方的框架,轮子嘛,能用就先用着,后面再优化. 一:Reachability 1.首先在AppDelegate.h添加头文件"Reachability.h", ...

  9. Reachability 检测网络状态

    -(void)viewWillAppear:(BOOL)animated { [IOSExcept JudgeNetwork];//联网 NSLog(@"检查网络 请稍后....." ...

随机推荐

  1. 两条命令在Linux主机之间建立信任关系

    ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa //生成当前用户密钥 ssh-copy-id -i /root/.ssh/id_rsa.pub r ...

  2. 设计模式-观察者模式(Observer Pattern)

    观察者模式(Observer Pattern):定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使他们能够自动更新自己. 观察者 ...

  3. Atitit html5.1 新特性attilax总结

    Atitit html5.1 新特性attilax总结 9. 嵌入 header 和 footer1 7. 校验表单1 6. 浏览器的上下文菜单2 1. 响应式图像2 Attilax觉得还不错的心特性 ...

  4. performance Counter

    Logman https://technet.microsoft.com/en-us/library/bb490956.aspx http://blogs.technet.com/b/askperf/ ...

  5. c++并行计算库TBB和PPL的基本用法

    并行库充分利用多核的优势,通过并行运算提高程序效率,本文主要介绍c++中两个知名的并行库,一个是intel开发的TBB,一个是微软开发的PPL.本文只介绍其基本的常用用法:并行算法和任务. TBB(I ...

  6. 每日英语:Missing at Mobile World Congress: Innovation

    The hottest showcase for new technology at this year's Mobile World Congress wasn't in the event's c ...

  7. table 中 文字长度大于td宽度,导致文字换行 解决方案

    1.TD不换行 nowrap属性 表格table的td单元格中,文字长了往往会撑开单元格,但是如果table都不够宽了,就换行了好像(不要较真其他情况,我只说会换行的情况).换行后的表格显得乱糟糟,不 ...

  8. idea 同project添加多个module maven支持

    选中新项目的pom.xml  会识别maven项目

  9. win7+python3.6+word_cloud 安装出现Microsoft Visual C++ 14.0 is required

    说明 环境: 已安装Anaconda3 (64-bit) 4.4.0(Python 3.6.1).其中,代码调试在Spyder 3.1.4中进行,安装包则直接打开Anaconda Prompt调用cm ...

  10. python pandas ---Series,DataFrame 创建方法,操作运算操作(赋值,sort,get,del,pop,insert,+,-,*,/)

    pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 pandas 也是围绕着 Series 和 DataFrame 两个核心数据结构展开的, 导入如下: from panda ...