0

南非,

我正在尝试创建套接字并从接入点接收数据和日志,当我从 shell 运行代码时,代码工作正常。但是,当我尝试使用运行命令的 execvp 从代码连接到 AP 时

sudo iwconfig wlan0 essid "access point name"

recv 函数不再从代码或 shell 中工作,当我运行此命令时似乎某些设置已更改,但我不确定到底发生了什么变化

这就是我创建套接字的方式

memset(&proc_addr, 0, sizeof(struct sockaddr_nl));
proc_addr.nl_family = AF_NETLINK;
proc_addr.nl_pid = getpid();            
proc_addr.nl_groups = CN_IDX_IWLAGN;

if (bind(sock_fd, (struct sockaddr *)&proc_addr, sizeof(struct sockaddr_nl)) == -1)
    exit_program_err(-1, "bind");

{
    int on = proc_addr.nl_groups;
    ret = setsockopt(sock_fd, 270, NETLINK_ADD_MEMBERSHIP, &on, sizeof(on));
    if (ret)
        exit_program_err(-1, "setsockopt");
}

while (1)
{
    /* Receive from socket with infinite timeout */
    printf("trying to receive");
    ret = recv(sock_fd, buf, sizeof(buf), 0);
    printf("received");
}

因此,尽管我可以从 AP ping 数据,但只打印了尝试接收然后什么也没有打印

执行代码:

string cmd1_connect = "sudo iwconfig wlan0 essid ciscosb_3"; 
string cmd_check = "sudo dhclient wlan0";
 string cmd2_connect = "sudo iwconfig wlan0 essid Cisco13-guest"; 
while(true) { 
if(stream1) { 
shell->runcmd(&m, &cmd1_connect); 
shell->runcmd(&m, &cmd_check); 
} else { 
shell->runcmd(&m, &cmd2_connect); 
shell->runcmd(&m, &cmd_check); }
4

0 回答 0