linux c 检测网络状态

摘要:
转移自:http://stackoverflow.com/questions/808560/how-to-detect-the-physical-connected-state-of-a-network-cable-connectorYouwanttolookatthenodesin/sys/class/net/Iexperimentedwithmine:WirePluggedin:eth0/car

转自: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

免责声明:内容来源于网络,仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇深入跟踪MFC程序的执行流程Jmeter代理服务器设置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

linux性能评估-磁盘io概念实战篇

1.文件系统和磁盘的 IO 问题的分析(如何找出狂打日志的“内鬼”?) 2.磁盘IO延迟很高,该怎么办? 3.当数据库出现性能问题时,该如何分析和定位它的瓶颈呢? 4.Redis响应严重延迟,该如何分析和定位? 1.文件系统和磁盘的 IO 问题的分析(如何找出狂打日志的“内鬼”?) 预先安装 docker、sysstat 等工具,如 apt ins...

Linux 各种软件的安装-mediawiki + wordpress篇

php apache mysql 三剑客安装好后,可以愉快地安装一些成熟的web应用啦,比如wordpress可以当做自己的笔记本,mediawiki整理知识库。 首先是mediawiki,网上说不错,但我觉得有点慢,而且wiki写词条的方式开始有点不习惯。 #cd /var/www/html #wget http://releases.wikimedia...

Linux实时查看日志,访问前10IP 和相关命令

Nginx日志分析可以获得很多有用的信息,现在来试试最基本的,获取最多访问的前10个IP地址及访问次数。 既然是统计,那么awk是必不可少的,好用而高效。 命令如下: awk '{a[$1] += 1;} END {for (i in a) printf("%d %s ", a[i], i);}' 日志文件 | sort -n | tail 首先用awk统...

Linux /tmp目录下执行脚本失败提示Permission denied

Linux上执行具有可执行权限Shell脚本失败提示Permission denied问题的分析过程。 问题现象Linux /tmp目录下,执行./test.sh运行失败,提示Permission denied。 问题分析1、检查test.sh脚本是否有可执行权限。执行ls -al test.sh,权限为777。具有可执行权限。 2、尝试使用sh test...

linux安装jdk脚本

使用Shell远程给Linux安装JDK  #1.下载JDK(不同版本JDK更换链接即可) wget命令下载的文件会在当前目录下,所以如果在下载前最好先 cd 到想把JDK安装的位置,如果没有,可以先创建相应目录 1 wget --no-check-certificate --no-cookies --header "Cookie: oraclelic...

linux添加用户,修改用户密码,修改用户权限,设置root用户操作

1、添加普通用户 [root@server ~]# useradd chenjiafa   //添加一个名为chenjiafa的用户[root@server ~]# passwd chenjiafa    //修改密码Changing password for user chenjiafa.New UNIX password:              ...