Linux中c语言实现获取wifi状态

Linux中c语言实现获取wifi状态

    获取wifi信息有两种方案,参考ifconfig程序使用c语言实现,或者使用命令抓取关键词。

1、c语言实现

    废话不多说直接上代码。

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/wireless.h>
#include <string.h> 
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>

#define NET_PORT 53
#define NET_IP "8.8.8.8" //谷歌DNS

int main()
{
	int sockfd;
	struct iwreq wreq;
	struct iw_statistics stats;
	char buffer[32];
	int in_len=0;
  	struct sockaddr_in servaddr;
	memset(buffer, 0, 32);
	memset(&stats, 0, sizeof(stats));
	memset(&wreq, 0, sizeof(wreq));
	strcpy(wreq.ifr_name, "wlan0");
	
	if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) //建立socket,用于接收来自ioctl函数的消息。由于是sock_DGRAM,所以是UDP。如果是SOCK_STREAM,则是TCP。
	{
		perror("Could not create simple datagram socket");
		exit(EXIT_FAILURE);
	}

	in_len = sizeof(struct sockaddr_in);
    /*设置默认服务器的信息*/
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(NET_PORT);
	servaddr.sin_addr.s_addr = inet_addr(NET_IP);
	memset(servaddr.sin_zero,0,sizeof(servaddr.sin_zero));
	/*connect 函数*/
	if(connect(sockfd,(struct sockaddr* )&servaddr,in_len) < 0 ) 
	{   

	    printf("not connect to internet!\n ");
	    close(sockfd);
	    return 0; 
	}else{   
	    printf("       connect ok!       \n");
	}  

//ioctl获取Signal level
	wreq.u.data.pointer = &stats;
	wreq.u.data.length = sizeof(iw_statistics);
	if(ioctl(sockfd, SIOCGIWSTATS, &wreq) == -1) 
	{
		perror("Error performing SIOCGIWSTATS");
		close(sockfd);
		exit(EXIT_FAILURE);
	}else{
		// IW_QUAL_DBM表示Level + Noise are dBmm
		printf("Signal level%s is %d%s.\n",(stats.qual.updated & IW_QUAL_DBM ? " (in dBm)" :""),(signed char)stats.qual.level,
		(stats.qual.updated & IW_QUAL_LEVEL_UPDATED ? " (updated)" :""));
	}

//ioctl获取essid
	wreq.u.essid.pointer = buffer;//如果不写这行可能会错误
	wreq.u.essid.length = 32; 
	if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
	 	perror("IOCTL SIOCGIWESSID Failed,error");
	 	exit(2);
	}else {	
	 	//printf("IOCTL SIOCGIWESSID Successfull\n");
		printf("The essid is:%s\n",wreq.u.essid.pointer);        //network name
	}
	close(sockfd);
	return 0;
}

    首先建立socket,函数原型即:int socket(int domain, int type, int protocol),再建立connect连接,我是用的谷歌的DNS:8.8.8.8来测试连接外网。如果连接成功后就可获取wifi名字和强度了。
    接下来使用ioctl函数,ioctl是设备驱动程序中对设备的I/O通道进行管理的函数。所谓对I/O通道进行管理,就是对设备的一些特性进行控制,它的调用个数如下:
int ioctl(int fd, ind cmd, …);
    其中fd是用户程序打开设备时使用open函数返回的文件标示符,cmd是用户程序对设备的控制命令,至于后面的省略号,那是一些补充参数,一般最多一个,这个参数的有无和cmd的意义相关。
    ioctl函数是文件结构中的一个属性分量,就是说如果你的驱动程序提供了对ioctl的支持,用户就可以在用户程序中使用ioctl函数来控制设备的I/O通道。

2、命令抓取方式

    FILE* fp=NULL;
    char buf[100] = {0};    
    char command[300] = " sudo iwlist wlan0 scan";
    char str1[100];
    char str2[100];
    char *q = NULL;
    memset(str1, '\0', sizeof(str1));
    memset(str2, '\0', sizeof(str2));
    if((fp = popen(command, "r")) != NULL)
    {
     //   printf("popen command success\n");
        while(fgets(buf, sizeof(buf), fp) != NULL)
        {
            //printf("buf=%s\n", buf);
            q = strstr(buf, "ESSID:");
            if (q != NULL)
            {
                //	printf("q =NULL\n");
                sscanf(q, "ESSID:\"%[^\"]\"", str1);
                q = NULL;
            }
            q = strstr(buf, "Signal level=");
            if (q != NULL)
            {
                //	printf("q =NULL\n");
                sscanf(q, "Signal level=%[^/]", str2);
                q = NULL;
                strcat(str1,"");
                strcat(str1,str2);
                wifilist.append(str1);
            }
        }
        pclose(fp);
    }

    popen将命令sudo iwlist wlan0 scan通过管道读到fp,fgets函数从指定的流中读取数据,每次读取一行。其原型为:
    char *fgets(char *str, int n, FILE *stream);从指定的流 stream 读取一行,并把它存储在 str 所指向的字符串内。使用strstr进行配对,再sscanf扫毛,函数具体实现可自行百度。
    小编是实习小白,查了很多资料来用c语言实现,但是对于ioctl函数得到wifi信息方式概念还是很模糊,希望对你们有所帮助吧。

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值