QT acquisition of native network information (MAC, IP, etc., very full)

Source: Internet
Author: User
Tags get ip

Use the command line frequently to view configuration information for some computers.

1. First press and hold the "Start key +r" button on the keyboard, then enter "CMD" in the pop-up dialog, and you can also click Start > All Programs > Accessories > Command Prompt 2. Next, enter "Ipconfig/all" in the command Prompt window that pops up. and press Enter on the keyboard (note: The ipconfig command is to view the IP configuration information, the parameter "All" means to view all the configuration information for all network cards) as shown in:

So, how to get the host name, IP, Port, Mac and so on in QT? The main classes used are:
    • Qhostinfo
    • Qhostaddress
    • Qnetworkinterface
    • Qnetworkaddressentry
1. Get logged in user
 QString name = qgetenv ("USERNAME"); Qdebug () << "User name:" << name;
Output: User name: "wangl-d" 2. Get host name, IP address
The Qhostinfo class acts to get the hostname, or the host name can be used to find the IP address, or to reverse the host name by IP address. QString localhostname = Qhostinfo::localhostname () qdebug () << "Localhostname:" << localhostname;// Get IP address qhostinfo info = qhostinfo::fromname (localhostname), Qdebug () << "IP address:" << info.addresses (); foreach (qhostaddress address, info.addresses ()) {    if (address.protocol () = = Qabstractsocket::ipv4protocol)        Qdebug () << "IPv4 Address:" << address.tostring ();} foreach (qhostaddress address, qnetworkinterface::alladdresses ()) {    qdebug () << "Address:" << address;}
Output:

Localhostname: "WANGL-D-PC"

IP Address: (Qhostaddress ("Fe80::396e:183d:aac6:f7f2"), Qhostaddress ("192.168.118.55"))

IPv4 Address: "192.168.118.55"

Address:qhostaddress ("Fe80::396e:183d:aac6:f7f2")

Address:qhostaddress ("192.168.118.55")

Address:qhostaddress (":: 1")

Address:qhostaddress ("127.0.0.1")

Address:qhostaddress ("fe80::5efe:c0a8:7637")

Address:qhostaddress ("fe80::e0:0:0:0")

3. Get the device name, MAC address, subnet mask, broadcast address, etc.

Get list of all network interfaces foreach (Qnetworkinterface netinterface, qnetworkinterface::allinterfaces ()) {    //device name    Qdebug () << "Device:" << netinterface.name ();    MAC address    Qdebug () << "hardwareaddress:" << netinterface.hardwareaddress ();    Qlist entrylist = Netinterface.addressentries ();    Iterate through each IP address (each containing an IP address, a subnet mask, and a broadcast address)    foreach (Qnetworkaddressentry entry, entrylist)    {        //IP address        qdebug () << "IP Address:" << entry.ip (). toString ();        Subnet mask        qdebug () << "Netmask:" << entry.netmask (). toString ();        Broadcast address        qdebug () << "broadcast:" << entry.broadcast (). toString ();    }}
Output:

Device: "{4c5c2613-c18f-416a-a21f-47b707c3b935}"

Hardwareaddress: "74:d4:35:1b:a6:2f"

IP Address: "FE80::396E:183D:AAC6:F7F2"

Netmask: "FFFF:FFFF:FFFF:FFFF::"

Broadcast: ""

IP Address: "192.168.118.55"

Netmask: "255.255.255.0"

Broadcast: "192.168.118.255"

Device: "{846ee342-7039-11de-9d20-806e6f6e6963}"

Hardwareaddress: ""

IP Address: ":: 1"

Netmask: "Ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"

Broadcast: ""

IP Address: "127.0.0.1"

Netmask: ""

Broadcast: ""

Device: "{539f3167-f67c-432d-8904-34129f3f5521}"

Hardwareaddress: "00:00:00:00:00:00:00:e0"

IP Address: "fe80::5efe:c0a8:7637"

Netmask: "Ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"

Broadcast: ""

Device: "{5DECB4C5-20AC-4BD6-A685-9FE608ED6EF5}"

Hardwareaddress: "00:00:00:00:00:00:00:e0"

IP Address: "fe80::e0:0:0:0"

Netmask: "FFFF:FFFF:FFFF:FFFF::"

Broadcast: ""

Http://blog.sina.com.cn/s/blog_a6fb6cc90102v0mz.html

QT acquisition of native network information (MAC, IP, etc., very full)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.