Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1371441
  • 博文数量: 198
  • 博客积分: 1629
  • 博客等级: 上尉
  • 技术积分: 2743
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-01 15:41
文章分类
文章存档

2023年(6)

2022年(20)

2021年(8)

2020年(3)

2018年(17)

2017年(3)

2016年(3)

2015年(9)

2014年(13)

2013年(17)

2012年(77)

2011年(22)

分类: 嵌入式

2022-03-25 17:54:37


点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <sys/ioctl.h>
  4. #include <linux/wireless.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <netinet/in.h>
  9. #include <unistd.h>
  10. #include <arpa/inet.h>

  11. /*
  12. int main()
  13. {
  14.     FILE *fp=NULL;
  15.     char buf[100] = {0};
  16.     //char command[300] = "sudo iwlist wlan0 scan";
  17.     char command[300] = "sudo iwlist wlp2s0 scan";
  18.     char str1[100];
  19.     char str2[100];
  20.     char* q = NULL;
  21.     memset(str1, '\0', sizeof(str1));
  22.     memset(str2, '\0', sizeof(str2));
  23.     if((fp = popen(command, "r")) != NULL)
  24.     {
  25.         while(fgets(buf, sizeof(buf), fp) != NULL)
  26.         {
  27.             q = strstr(buf, "ESSID:");
  28.             if(q != NULL)
  29.             {
  30.                 sscanf(q, "ESSID:\"%[^\"]", str1);
  31.                 q = NULL;
  32.             }
  33.             q = strstr(buf, "Signal level=");
  34.             if(q != NULL)
  35.             {
  36.                 sscanf(q, "Signal level=%[^/]", str2);
  37.                 printf("%s %s\n", str1,str2);
  38.                 q = NULL;
  39.                 strcat(str1, "");
  40.                 strcat(str1, str2);
  41.                 printf("%s \n", str1);
  42.             }
  43.         }
  44.         pclose(fp);
  45.     }
  46. }
  47. */


  48. #define NET_PORT 53
  49. #define NET_IP "8.8.8.8"

  50. int main()
  51. {
  52.     int sockfd;
  53.     struct iwreq wreq;
  54.     struct iw_statistics stats;
  55.     char buffer[32];
  56.     int in_len=0;
  57.     struct sockaddr_in servaddr;
  58.     memset(buffer, 0, 32);
  59.     memset(&stats, 0, sizeof(stats));
  60.     strcpy(wreq.ifr_name, "wlan0");
  61.     //strcpy(wreq.ifr_name, "wlp2s0");

  62.     if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  63.     {
  64.         perror("Cound not create simple socket");
  65.         return -1;
  66.     }

  67.     in_len = sizeof(struct sockaddr_in);
  68.     servaddr.sin_family = AF_INET;
  69.     servaddr.sin_port = htons(NET_PORT);
  70.     servaddr.sin_addr.s_addr = inet_addr(NET_IP);

  71.     memset(servaddr.sin_zero,0,sizeof(servaddr.sin_zero));

  72.     if(connect(sockfd,(struct sockaddr*) &servaddr,in_len) < 0)
  73.     {
  74.         printf("not connect to internet!\n");
  75.         close(sockfd);
  76.         return -1;
  77.     }else{
  78.         printf(" connect ok!\n");
  79.     }

  80.     wreq.u.data.pointer = &stats;
  81.     //wreq.u.data.length = sizeof(iw_statistics);
  82.     wreq.u.data.length = sizeof(stats);
  83.     if(ioctl(sockfd, SIOCGIWSTATS, &wreq) == -1)
  84.     {
  85.         perror("SIOCGIWSTATS");
  86.         close(sockfd);
  87.         return -2;
  88.     }else{
  89.         printf("Signal level%s is %d\n", (stats.qual.updated & IW_QUAL_DBM ? " (in dBm)" : ""), (signed char)stats.qual.level,(stats.qual.updated & IW_QUAL_LEVEL_UPDATED ? "(updated)" : ""));

  90.     }

  91.     wreq.u.essid.pointer = buffer;
  92.     wreq.u.essid.length = 32;
  93.     if(ioctl(sockfd, SIOCGIWESSID, &wreq) == -1){
  94.         perror("SIOCGIWESSID");
  95.         close(sockfd);
  96.         return -3;
  97.     }else{
  98.         printf("The essid is : %s\n", wreq.u.essid.pointer);
  99.     }

  100.     close(sockfd);
  101.     return 0;
  102. }

阅读(493) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~