轉自: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的時候 是斷開
- #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\n",ret);
- return;
- }
- char status[3]="wl\0";
- //printf("%s",status);
- read (ret,status,2);
- status[3] = '\0';
- if (0== strcmp("up",status))
- {
- printf("on line now \n");
- }
- else if (0== strcmp("do",status))
- {
- printf("off off \n");
- }
- else
- printf("unknow error\n");
- close (ret);
- sleep (5);
- }
- }
- int main(int argc,char* argv[])
- {
- GtkWidget *window,*view;
- GtkWidget *vbox,*button,*label;
- /*線程初始化*/
- if (!g_thread_supported())
- g_thread_init(NULL);
- gdk_threads_init();
- gtk_init(&argc,&argv);
- window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
- gtk_window_set_title(GTK_WINDOW(window),"thread apllication");
- vbox=gtk_vbox_new(FALSE,0);
- gtk_container_add(GTK_CONTAINER(window),vbox);
- label=gtk_label_new("Notice! Button is moving");
- gtk_box_pack_start(GTK_BOX(vbox),label,FALSE,FALSE,0);
- view=gtk_viewport_new(NULL,NULL);
- gtk_box_pack_start(GTK_BOX(vbox),view,FALSE,FALSE,0);
- fixed=gtk_fixed_new();
- gtk_widget_set_usize(fixed,330,330);
- gtk_container_add(GTK_CONTAINER(view),fixed);
- button1=gtk_button_new_with_label("1");
- button2=gtk_button_new_with_label("2");
- gtk_fixed_put(GTK_FIXED(fixed),button1,10,10);
- gtk_fixed_put(GTK_FIXED(fixed),button2,40,40);
- GtkWidget *button_ring = gtk_button_new_with_label ("ring");
- gtk_box_pack_start(GTK_BOX(vbox),button_ring,FALSE,FALSE,5);
- g_signal_connect (button_ring,"clicked",G_CALLBACK(ring),NULL);
- /*創建線程*/
- g_thread_create((GThreadFunc)www_connect_check_real,NULL,FALSE,NULL);
- gtk_widget_show_all(window);
- g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
- gtk_container_set_border_width(GTK_CONTAINER(window),10);
- gdk_threads_enter();
- gtk_main();
- gdk_threads_leave();
- return FALSE;
- }
注意:
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,相關鏈接
3,也可以使用shell腳本來檢測,網上例子很多
4,也可以編寫一個內核模塊來檢測,后期再說,先找工作
5,
[cpp] view plain copy
- //RFC 2863操作狀態
- unsigned char operstate;
- /* operstate的可能取值如下:
- enum {
- IF_OPER_UNKNOWN,
- IF_OPER_NOTPRESENT,
- IF_OPER_DOWN,
- IF_OPER_LOWERLAYERDOWN,
- IF_OPER_TESTING,
- IF_OPER_DORMANT,
- IF_OPER_UP,
- };
- **/
- //映射到RFC2863兼容狀態的策略
- unsigned char link_mode;
- /* link_mode的可能取值如下:
- enum {
- IF_LINK_MODE_DEFAULT,
- IF_LINK_MODE_DORMANT,
- };
- **/
我們檢測的文件內容,其實是這個成員變變量的值,返回值比較多,程序中只是判斷了前兩個字符
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);
}