linux c 检测网络状态
转自: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);
}
linux c 检测网络状态的更多相关文章
- iOS开发——网络篇——数据安全(MD5),HTTPS,检测网络状态
一.数据安全 1.提交用户的隐私数据一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 ...
- iOS开发网络篇—Reachability检测网络状态
前言:当应用程序需要访问网络的时候,它首先应该检查设备的网络状态,确认设备的网络环境及连接情况,并针对这些情况提醒用户做出相应的处理.最好能监听设备的网络状态的改变,当设备网络状态连接.断开时,程序也 ...
- [iOS 多线程 & 网络 - 2.8] - 检测网络状态
A.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的:(1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能)(2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验 ...
- iOS 检测网络状态 自动判断 认为提示网络改变
检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是让用户了解自己的网络状态,防止一些误会(比如怪应用无能)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验WIFI\3G ...
- iOS开发 - 检测网络状态(WIFI、2G/3G/4G)
本文转载至 http://blog.csdn.net/wangzi11322/article/details/45580917 检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是 ...
- iOS网络4——Reachability检测网络状态
一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发. 其实在网络开发中还有比较常用的就是网络 ...
- iOS Reachability检测网络状态
一.整体介绍 前面已经介绍了网络访问的NSURLSession.NSURLConnection,还有网页加载有关的webview,基本满足通常的网络相关的开发.其实在网络开发中还有比较常用的就是网络状 ...
- iOS 检测网络状态
一般有两种方式,都是第三方的框架,轮子嘛,能用就先用着,后面再优化. 一:Reachability 1.首先在AppDelegate.h添加头文件"Reachability.h", ...
- Reachability 检测网络状态
-(void)viewWillAppear:(BOOL)animated { [IOSExcept JudgeNetwork];//联网 NSLog(@"检查网络 请稍后....." ...
随机推荐
- Android log 日志分析
一. Log 日志中 Bug 类型 程序异常强制关闭: Force Close ,Fatal 程序无响应: Application Not Response , ANR(应用无响应).一般是主线程超时 ...
- [转]:Android 5.0的调度作业JobScheduler
参考:http://blog.csdn.net/cuiran/article/details/42805057 增加 JobScheduler 的同时,去掉了几个广播, CONNECTIVITY_AC ...
- php正则表达式入门-常用语法格式
php正则表达式入门-常用语法格式 原文地址:http://www.jbxue.com/article/24467.html 分享下php正则表达式中的一些常用语法格式,用于匹配字母.数字等,个人感觉 ...
- Atitit spring 3.0 3.1 3.2 4.0 4.3 5.0 新特性
Atitit spring 3.0 3.1 3.2 4.0 4.3 5.0 新特性 Spring3.0的新特性及其分析 - 我的人生不甘于平庸! - ITeye技术网站.html Spring3.0带 ...
- 常用sql自定义函数
--把数字转为千分位格式的字符串显示,调用如 select dbo.f_splitstr(11111111111111) CREATE FUNCTION [dbo].[f_splitstr]( @st ...
- bitcoin双花
https://en.bitcoin.it/wiki/Irreversible_Transactions https://www.reddit.com/r/Bitcoin/comments/2e7bf ...
- 聊一聊 Spring 中的线程安全性
Spring与线程安全 Spring作为一个IOC/DI容器,帮助我们管理了许许多多的“bean”.但其实,Spring并没有保证这些对象的线程安全,需要由开发者自己编写解决线程安全问题的代码. Sp ...
- 【内核】内核链表使用说明,list.h注释
list_entry定义 /** * list_entry - get the struct for this entry * @ptr: the &struct list_head poin ...
- HttpClient request payload post请求
RequestEntity entity = new StringRequestEntity(str, "text/html", "utf-8"); post. ...
- maven pom scope 含义
maven pom scope 依赖范围控制哪些依赖在哪些classpath 中可用,哪些依赖包含在一个应用中.让我们详细看一下每一种范围: compile (编译范围) compile是默认的范围: ...