转自: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
转自:http://blog.csdn.net/a954423389/article/details/7327950
主程序创建一个进程, 每2秒查看一下网络状态,然后打印输出
通过检查文件
/sys/class/net/wlan0/operstate (无线网络)
/sys/class/net/eth0/operstate (有线网络)
通过检查文件的内容 判断当前网络是否连接
值为up的时候,是连接 值为down的时候 是断开
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <gtk/gtk.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #define WIRELESS
- #ifdef WIRELESS
- #define INTERNET_STATUS_FILE "/sys/class/net/wlan0/operstate"
- #else
- #define INTERNET_STATUS_FILE "/sys/class/net/eth0/operstate"
- #endif
- #include <sys/wait.h>
- static GtkWidget *fixed;
- static GtkWidget *button1;
- static GtkWidget *button2;
- int running = 1;
- void ring()
- {
- GtkWidget *window;
- GtkWidget *table;
- GtkWidget *button_cancel;
- GtkWidget *label;
- window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- gtk_window_set_default_size (GTK_WINDOW(window),100,100);
- gtk_window_set_position (GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);
- table = gtk_table_new(10,10,TRUE);
- gtk_container_add (GTK_CONTAINER (window),table);
- label = gtk_label_new("ring");
- button_cancel = gtk_button_new_with_label ("quit");
- gtk_table_attach_defaults (GTK_TABLE(table),button_cancel,2,4,7,9);
- gtk_widget_show_all(window);
- }
- void www_connect_check_real ()
- {
- int ret = -2;
- while (1)
- {
- //一定要只读模式打开,读写模式打开不可以
- ret = open ("/sys/class/net/wlan0/operstate",O_RDONLY);
- if (ret<0) {
- printf("open file operstate failure%d ",ret);
- return;
- }
- char status[3]="wl