您尚未登录。

楼主 #1 2020-05-20 15:16:21

无情一刀
会员
注册时间: 2018-09-04
已发帖子: 694
积分: 685

用udhcpc -i wlan0 如何获取当前分配的IP地址

_20200520151145.png

离线

#2 2020-05-20 15:20:17

哇酷小二
wechat微信:whycan_cn
所在地: 你猜
注册时间: 2020-04-22
已发帖子: 3,378
积分: 1902
个人网站

Re: 用udhcpc -i wlan0 如何获取当前分配的IP地址

前面那个问题是怎么解决的?





离线

#3 2020-05-20 15:20:36

哇酷小二
wechat微信:whycan_cn
所在地: 你猜
注册时间: 2020-04-22
已发帖子: 3,378
积分: 1902
个人网站

Re: 用udhcpc -i wlan0 如何获取当前分配的IP地址

你是要程序获取IP地址?





离线

楼主 #4 2020-05-20 15:45:07

无情一刀
会员
注册时间: 2018-09-04
已发帖子: 694
积分: 685

Re: 用udhcpc -i wlan0 如何获取当前分配的IP地址

哇酷小二 说:

你是要程序获取IP地址?

是的 就是DHCP分配了地址了  我程序要怎样知道这个分配IP 
我现在就是用 ip addr show | grep \"scope global wlan0\"去读取出来放到文件中  再去解析文件  我觉得太麻烦了

离线

#5 2020-05-20 15:47:25

哇酷小二
wechat微信:whycan_cn
所在地: 你猜
注册时间: 2020-04-22
已发帖子: 3,378
积分: 1902
个人网站

Re: 用udhcpc -i wlan0 如何获取当前分配的IP地址

无情一刀 说:

是的 就是DHCP分配了地址了  我程序要怎样知道这个分配IP 
我现在就是用 ip addr show | grep \"scope global wlan0\"去读取出来放到文件中  再去解析文件  我觉得太麻烦了

https://stackoverflow.com/questions/2283494/get-ip-address-of-an-interface-on-linux

#include <stdio.h>
#include <unistd.h>
#include <string.h> /* for strncpy */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>

int
main()
{
 int fd;
 struct ifreq ifr;

 fd = socket(AF_INET, SOCK_DGRAM, 0);

 /* I want to get an IPv4 IP address */
 ifr.ifr_addr.sa_family = AF_INET;

 /* I want IP address attached to "eth0" */
 strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);

 ioctl(fd, SIOCGIFADDR, &ifr);

 close(fd);

 /* display result */
 printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

 return 0;
}

把这个 eth0 改成 wlan0 试一试。





离线

楼主 #6 2020-05-21 14:51:20

无情一刀
会员
注册时间: 2018-09-04
已发帖子: 694
积分: 685

Re: 用udhcpc -i wlan0 如何获取当前分配的IP地址

完成了 我用的是这个

QString WifiList::GetCurrwlan0IpAddr(QString str)
{
    QString filename = "/V3S/wifi_file/Wifi_Addr";
    QFile file(filename);
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug()<<"!file.open(FileName) error";
        return NULL;
    }
    QTextStream stream(&file);
    QString ReadLineStr = stream.readLine();
    if(ReadLineStr == NULL)
    {
        return NULL;
    }
    QStringList str3=  ReadLineStr.split("/",QString::SkipEmptyParts);
    QStringList str=  str3[0].split("/",QString::SkipEmptyParts);
    QStringList str1 = str[0].split(" ",QString::SkipEmptyParts);
    return str1[1];*/
    QString str1;
    QList<QNetworkAddressEntry> list;
    QHostAddress                addr1;
    QNetworkInterface addr = QNetworkInterface::interfaceFromName(str);
    list = addr.addressEntries();
    if(!list.isEmpty())
    {
            addr1 = list.at(0).ip();
            str1 = addr1.toString();
            CurrConnectWifiIP = str1;
            //IPAddres = CurrConnectWifiIP;
    }
    qDebug()<<str1;
    return str1;
}

但是有一个问题就是  如何设置有线网络逇优先级是最高啊  用route是设置不管用

离线

#7 2020-05-21 15:11:17

哇酷小二
wechat微信:whycan_cn
所在地: 你猜
注册时间: 2020-04-22
已发帖子: 3,378
积分: 1902
个人网站

Re: 用udhcpc -i wlan0 如何获取当前分配的IP地址

试一试指定网卡发送数据: https://whycan.cn/t_3670.html#p33573





离线

楼主 #8 2020-05-21 16:32:10

无情一刀
会员
注册时间: 2018-09-04
已发帖子: 694
积分: 685

Re: 用udhcpc -i wlan0 如何获取当前分配的IP地址

# ifconfig wlan0 up
ifconfig: SIOCSIFFLAGS: Operation not permitted


不知道怎么突然就wlan0 就不行了 ,也加载了库

离线

楼主 #9 2020-05-21 16:54:05

无情一刀
会员
注册时间: 2018-09-04
已发帖子: 694
积分: 685

Re: 用udhcpc -i wlan0 如何获取当前分配的IP地址

# insmod /usr/lib/r8723bs.ko
[  952.243349] r8723bs: module is from the staging directory, the quality is unknown, you have been warned.
[  952.260150] RTL8723BS: module init start
[  952.264092] RTL8723BS: rtl8723bs v4.3.5.5_12290.20140916_BTCOEX20140507-4E40
[  952.271227] RTL8723BS: rtl8723bs BT-Coex version = BTCOEX20140507-4E40
[  952.290744] pnetdev = c1317800
[  954.610905] RTL8723BS: rtw_ndev_init(wlan0)
[  954.715967] RTL8723BS: module init ret =0

加载库出现这样的 没有问题

离线

#10 2020-05-21 17:01:29

哇酷小二
wechat微信:whycan_cn
所在地: 你猜
注册时间: 2020-04-22
已发帖子: 3,378
积分: 1902
个人网站

Re: 用udhcpc -i wlan0 如何获取当前分配的IP地址

没用 wpa_supplicant 连接路由器吧?





离线

楼主 #11 2020-05-21 17:10:38

无情一刀
会员
注册时间: 2018-09-04
已发帖子: 694
积分: 685

Re: 用udhcpc -i wlan0 如何获取当前分配的IP地址

哇酷小二 说:

没用 wpa_supplicant 连接路由器吧?

用了
insmod /usr/lib/r8723bs.ko
ifconfig wlan0 up  //这里就出错了  就是那个不允许操作

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn