本文介绍了想在UBUNTU中通过C++知道无线网络的ESSID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下程序来获取我的桌面当前连接到的无线网络的 ESSID,但它给了我错误.谁能帮我纠正错误?代码:

I have written the following program to get the ESSID of the wireless network to which my Desktop is currently connected, but it is giving me errors. Can anyone help me correct the bugs? Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/wireless.h>

#define IW_INTERFACE "wlan0"

extern int errno;
struct iwreq wreq;

int main (void)
{
    int sockfd;
    char * id;

    memset(&wreq, 0, sizeof(struct iwreq));
    sprintf(wreq.ifr_name, IW_INTERFACE);

    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        fprintf(stderr, "Cannot open socket
");
        fprintf(stderr, "errno = %d
", errno);
        fprintf(stderr, "Error description is : %s
",strerror(errno));
        exit(1);
    }
    printf("Socket opened successfully
");


    id = new char(IW_ESSID_MAX_SIZE+1);
    wreq.u.essid.pointer = id;
    if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
        fprintf(stderr, "Get ESSID ioctl failed
");
        fprintf(stderr, "errno = %d
", errno);
        fprintf(stderr, "Error description : %s
",strerror(errno));
        exit(2);
    }
    printf("IOCTL Successfull
");
    printf("ESSID is %s
", wreq.u.essid.pointer);
    exit(0);
}

我收到以下错误:

  > 1. Get ESSID ioctl failed
  > 2. errno = 7
  > 3. Error description : Argument list too long

推荐答案

在使用 werq 之前,您应该先正确设置长度,检查这个,

You should set length properly first before using werq,check this,

int sockfd;
    char * id;
   id = new char[IW_ESSID_MAX_SIZE+1];

    struct iwreq wreq;
    memset(&wreq, 0, sizeof(struct iwreq));
    wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;

    sprintf(wreq.ifr_name, IW_INTERFACE);

    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        fprintf(stderr, "Cannot open socket
");
        fprintf(stderr, "errno = %d
", errno);
        fprintf(stderr, "Error description is : %s
",strerror(errno));
        exit(1);
    }
    printf("
Socket opened successfully
");

    wreq.u.essid.pointer = id;
    if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
        fprintf(stderr, "Get ESSID ioctl failed
");
        fprintf(stderr, "errno = %d
", errno);
        fprintf(stderr, "Error description : %s
",strerror(errno));
        exit(2);
    }

    printf("IOCTL Successfull
");

    printf("ESSID is %s
", (char *)wreq.u.essid.pointer);

这篇关于想在UBUNTU中通过C++知道无线网络的ESSID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-09 05:44