Ideas:
The main program creates a process, checks the network status every 2 seconds, and then prints the output
Check the file
/Sys/class/NET/wlan0/operstate (Wireless Network)
/Sys/class/NET/eth0/operstate (wired network)
Check the file content to determine whether the current network is connected
When the value is up, it is disconnected when the connection value is 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); break (gtk_window (window ), 100,100); gtk_window_set_position (gtk_window (window), gtk_win_pos_center_always); table = gtk_table_new (, true); gtk_container_add (gtk_container (window), table ); label = gtk_label_new ("ring"); button_ca Ncel = require ("quit"); convert (gtk_table (table), button_cancel,); gtk_widget_show_all (window);} void www_connect_check_real () {int ret =-2; while (1) {// It must be enabled in read-only mode. It cannot be enabled in read/write mode. 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); ST ATUS [3] = '\ 0'; If (0 = strcmp ("up", status) {printf ("On line now \ n ");} else if (0 = strcmp ("do", status) {printf ("off \ n");} else printf ("unknow error \ n "); close (RET); sleep (5) ;}} int main (INT argc, char * argv []) {gtkwidget * window, * view; gtkwidget * vbox, * button, * label;/* thread initialization */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 = require ("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 ); gtkwidget * button_ring = ring ("ring"); gtk_box_pack_start (gtk_box (vbox), button_ring, false, false, 5); g_signal_connect (button_ring, "clicked", g_callback (ring ), null);/* Create thread */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 ;}
Note:
1. on Ubuntu 10.04, when a static IP address is used, even if the network is connected, the file value is not up but unknown. This is a drive bug and is normal on Ubuntu 11.10.
Http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/5/html/5.7_Technical_Notes/kernel.html
2. Related Links
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. You can also use shell scripts for detection. There are many examples on the Internet.
4. You can also write a kernel module for detection.
Http://stackoverflow.com/questions/7225888/how-can-i-monitor-the-nic-statusup-down-in-a-c-program-without-polling-the-ker
5,
// RFC 2863 operation status unsigned charoperstate;/* operstate may take the following values: Enum {failed, if_oper_notpresent, if_oper_down, failed, if_oper_testing, if_oper_dormant, if_oper_up ,}; ** // The Policy unsigned charlink_mode mapped to the rfc2863 compatibility state./* the possible values of link_mode are as follows: Enum {if_link_mode_default, if_link_mode_dormant ,};**/
The content of the file we checked is actually the variable value of this Member. There are many return values, and the program only judges the first two characters.
Http://blog.csdn.net/npy_lp/article/details/7056903