想通过UBUNTU中的C ++知道无线networking的ESSID

我写了下面的程序来获取我的桌面当前连接的无线networking的ESSID,但它给我错误。 任何人都可以帮助我纠正错误? 码:

#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 \n"); fprintf(stderr, "errno = %d \n", errno); fprintf(stderr, "Error description is : %s\n",strerror(errno)); exit(1); } printf("Socket opened successfully \n"); 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 \n"); fprintf(stderr, "errno = %d \n", errno); fprintf(stderr, "Error description : %s\n",strerror(errno)); exit(2); } printf("IOCTL Successfull\n"); printf("ESSID is %s\n", wreq.u.essid.pointer); exit(0); } 

我收到以下错误:

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

您正在测试ioctl(2)的返回码错误。 ioctl(2)在错误时返回-1,不是true(非零)。 由于没有返回错误, errno的值是未定义的,并且是误导性的。

它应该是:

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

编辑:用不正确的代码,这是这样的:

 id = new char(IW_ESSID_MAX_SIZE+1); 

也许你的意思是:

 id = new char[IW_ESSID_MAX_SIZE+1]; 

前者只分配一个字符并将其值设置为IW_ESSID_MAX_SIZE+1 。 后者分配一个大小为IW_ESSID_MAX_SIZE+1的数组。

随着这种变化,代码为我工作,并没有给ioctl一个错误。

尝试将wreq.u.essid.length设置为正确的值。

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

 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 \n"); fprintf(stderr, "errno = %d \n", errno); fprintf(stderr, "Error description is : %s\n",strerror(errno)); exit(1); } printf("\nSocket opened successfully \n"); wreq.u.essid.pointer = id; if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) { fprintf(stderr, "Get ESSID ioctl failed \n"); fprintf(stderr, "errno = %d \n", errno); fprintf(stderr, "Error description : %s\n",strerror(errno)); exit(2); } printf("IOCTL Successfull\n"); printf("ESSID is %s\n", (char *)wreq.u.essid.pointer); 

printf("Socket opened successfully \n");

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