简体   繁体   English

Java:在我的家庭网络中获得自己的IP地址

[英]Java: Get my own ip address in my home network

I have find two examples on the web to get the ip address the router has given to my pc. 我在网上找到两个示例,以获取路由器给我PC的IP地址。 Here is the code: 这是代码:

import java.net.InetAddress;
import java.net.UnknownHostException;

public class tryNet {

public static void displayStuff(String whichHost, InetAddress inetAddr) {
    System.out.println("---------------------");
    System.out.println("host: " + whichHost);
    System.out.println("Canonical host name: " + inetAddr.getCanonicalHostName());
    System.out.println("Host Name: " + inetAddr.getHostName());
    System.out.println("Host Address: " + inetAddr.getHostAddress());
    System.out.println("---------------------");
}


public static void main(String argv[]) {
    try {
        InetAddress inetAddr = InetAddress.getLocalHost();
        displayStuff("localhost", inetAddr);
    }

    catch (UnknownHostException e) {
        e.printStackTrace();
    }
}

}

I have read that after having initialized InetAddress inetAddr = InetAddress.getLocalHost(); 我已经阅读了初始化InetAddress inetAddr = InetAddress.getLocalHost();之后的内容。 I can use the method inetAddr.getHostAddress() to get my ip address, the one given by my router (such as write ifconfig in the terminal in ubuntu, or ipconfig in windows) Instead it returns me my loopback address...(127.0.0.1) Why? 我可以使用inetAddr.getHostAddress()方法获取我的IP地址,这是我的路由器给定的IP地址(例如,在ubuntu的终端中写入ifconfig或在Windows中的ipconfig),而是返回我的环回地址...(127.0) .0.1)为什么?

Your PC has multiple interfaces (at least two) and multiple IP addresses (If it's plugged into a network, of course). 您的PC具有多个接口(至少两个)和多个IP地址(当然,如果已插入网络)。 Typically localhost is going to resolve to 127.0.0.1 (on the loopback interface) and the various methods you are using are going to return that. 通常, localhost将解析为127.0.0.1 (在回送接口上),而您正在使用的各种方法将返回该值。

The following will show you all the interfaces on the machine and the IP addresses assigned to them: 下面将向您显示计算机上的所有接口以及分配给它们的IP地址:

public static void main(String[] args) throws InterruptedException, IOException
{
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements())
    {
        NetworkInterface n = e.nextElement();
        System.out.println(n.getName());
        Enumeration<InetAddress> ee = n.getInetAddresses();
        while (ee.hasMoreElements())
        {
            InetAddress i = ee.nextElement();
            System.out.println(i.getHostAddress());
        }
    }
}

Typically you host has a name which points to loopback interface. 通常,主机的名称指向环回接口。 A DHCP server assigned an IP address. DHCP服务器分配了IP地址。 Depending on your dhcp client configuration host may take a new name as well. 根据您的dhcp客户端配置,主机也可能会使用新名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使我的外部IP地址发生变化,如何保持与家庭网络之外的Wamp服务器的连接 - how to stay connected to my wamp server outside of my home network even when my external IP address changes Java获取我的IP地址 - Java getting my IP address 如何在Java中询问我的外部IP地址 - How ask my outer IP address in java 如何使用java获取我们自己的无线局域网IP地址 - How to get our own Wireless LAN Ip address using java 当我尝试使用网络IP对自己的PC进行ping操作时,可能会得到java.net.UnknownHostException的原因是什么? - what could be the reason I am getting java.net.UnknownHostException, when I try to ping my own pc with a network IP? 使用JAVA从网络上的IP地址获取主机名 - Get host name from IP address on network using JAVA Java 无法将我自己的公共 ip 分配给 SocketServer - Java can't assign my own public ip to SocketServer 是否可以获取我的家庭网络中所有已连接设备的mac地址? - Is it possible to get the mac addresses of all connected device in my home network? 如何创建自己的网络协议以使用Java进行通信 - How to create my own network protocol to communicate in java 在Java中使用子网和子网掩码映射我的IP地址的有效方法 - Efficient way to map my Ip address with subnet and subnet mask in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM