转自:http://*.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);
}