public class Main {
public static void main(String[] args) throws IOException {
InetAddress myIp = null;
try {
myIp = InetAddress.getLocalHost();
} catch (UnknownHostException ex) {
System.out.println("Exception cought.");
System.exit(0);
}
System.out.println(myIp);
}
}
I have this simple question that why my ip address is different when my wireless is off?
it's still the same computer, so why does it change? (isn't this a unique number?)
我有一个简单的问题,为什么我的无线网络关闭时我的IP地址不同?它仍然是同一台计算机,为什么它会改变? (这不是一个唯一的号码吗?)
5 个解决方案
#1
3
The IP address of the computer depends on the network it's connected to (and indeed, the same machine may have more than one, if it has multiple adapers).
计算机的IP地址取决于它所连接的网络(实际上,如果它有多个adapers,同一台机器可能有多个)。
So if I connect my machine to one of my networks, it may have the address 192.168.10.7
whereas on another of my networks, it may be 192.168.17.12
. It can vary between connections as well, although in practice they tend to be a bit sticky. (It depends on how the DHCP server is configured.)
因此,如果我将我的机器连接到我的一个网络,它可能具有地址192.168.10.7,而在我的另一个网络上,它可能是192.168.17.12。它也可以在不同的连接之间变化,但在实践中它们往往有点粘。 (这取决于DHCP服务器的配置方式。)
Your adapter can be configured with a fixed address, but if you do that, it has to be an address the network it's connecting to has reserved for it. Otherwise it may not work at all ("No route to host") or may conflict with another machine using the network.
您的适配器可以配置固定地址,但如果您这样做,它必须是它所连接的网络为其保留的地址。否则它可能根本不起作用(“无主机路由”)或可能与使用网络的另一台机器冲突。
#2
1
.An IP address is the address of a network adapter within a specific local network.
.IP地址是特定本地网络中的网络适配器的地址。
It will be different when connected to different networks.
连接到不同的网络时会有所不同。
When not connected to any network, it will either be a link-local address or an auto-configuration address.
未连接到任何网络时,它将是链路本地地址或自动配置地址。
You might want the MAC address, which is the hardware address of a single network adapter and is not very likely to change.
您可能需要MAC地址,这是单个网络适配器的硬件地址,并且不太可能更改。
#3
1
The provided code returns HOSTNAME/IP-Address(xx.xx.xx.xx).
提供的代码返回HOSTNAME / IP-Address(xx.xx.xx.xx)。
Hostname is your computer name ex: MY-PC and then you get the IP corresponding to it. When you are connected to a network, InetAddress.getLocalHost() asks the DHCP server in the network "what is the address of MY-PC (the name of your computer)", the DHCP replies -> 33.44.55.66
主机名是您的计算机名称ex:MY-PC,然后您将获得与其对应的IP。当您连接到网络时,InetAddress.getLocalHost()会询问网络中的DHCP服务器“MY-PC的地址(您的计算机名称)是什么”,DHCP回复 - > 33.44.55.66
Try the following CMD commands when both connected and disconnected.
连接和断开连接时,请尝试以下CMD命令。
\>hostname
MY-PC
\>nslookup MY-PC
44.55.66.77
When you are not connected to a network there are two possibilities:
当您未连接到网络时,有两种可能性:
- You do not get a hostname (default is localhost)
- 您没有获得主机名(默认为localhost)
- You do get a hostname, but there is no DHCP server on the network to return an IPaddress, so you get loopback - 127.0.0.1
- 你确实得到了一个主机名,但是网络上没有DHCP服务器来返回一个IP地址,所以你得到了环回 - 127.0.0.1
If you want to "call" your computer on the network locally, use the loopback http://www.pcmag.com/encyclopedia/term/57812/loopback-address
如果您想在本地“呼叫”您的计算机,请使用环回http://www.pcmag.com/encyclopedia/term/57812/loopback-address
Hope this helps
希望这可以帮助
#4
0
No. You're confusing IP and MAC addresses. The MAC address is a serial number of hardware(but may be programatically changed on certain chipsets).
不,你在混淆IP和MAC地址。 MAC地址是硬件的序列号(但可以在某些芯片组上以编程方式更改)。
The IP address is either software-determined or determined by the network. It can differ between networks or even with time.
IP地址由软件确定或由网络确定。它可以在网络之间或甚至时间上有所不同。
#5
0
IP addresses are (usually) interface specific, not machine specific.
IP地址(通常)是接口特定的,而不是特定于机器的。
If your machine only has one interface the difference is moot, but it matters if (for example) you have both wired and wireless ethernet.
如果您的机器只有一个接口,那么差异就没有实际意义,但是(例如)您有有线和无线以太网都很重要。
Also note that if you do have both and attempt to use them both at the same time on the same subnet that things will likely get very confused!
另请注意,如果您同时拥有这两者并尝试在同一子网上同时使用它们,那么事情可能会非常混乱!
#1
3
The IP address of the computer depends on the network it's connected to (and indeed, the same machine may have more than one, if it has multiple adapers).
计算机的IP地址取决于它所连接的网络(实际上,如果它有多个adapers,同一台机器可能有多个)。
So if I connect my machine to one of my networks, it may have the address 192.168.10.7
whereas on another of my networks, it may be 192.168.17.12
. It can vary between connections as well, although in practice they tend to be a bit sticky. (It depends on how the DHCP server is configured.)
因此,如果我将我的机器连接到我的一个网络,它可能具有地址192.168.10.7,而在我的另一个网络上,它可能是192.168.17.12。它也可以在不同的连接之间变化,但在实践中它们往往有点粘。 (这取决于DHCP服务器的配置方式。)
Your adapter can be configured with a fixed address, but if you do that, it has to be an address the network it's connecting to has reserved for it. Otherwise it may not work at all ("No route to host") or may conflict with another machine using the network.
您的适配器可以配置固定地址,但如果您这样做,它必须是它所连接的网络为其保留的地址。否则它可能根本不起作用(“无主机路由”)或可能与使用网络的另一台机器冲突。
#2
1
.An IP address is the address of a network adapter within a specific local network.
.IP地址是特定本地网络中的网络适配器的地址。
It will be different when connected to different networks.
连接到不同的网络时会有所不同。
When not connected to any network, it will either be a link-local address or an auto-configuration address.
未连接到任何网络时,它将是链路本地地址或自动配置地址。
You might want the MAC address, which is the hardware address of a single network adapter and is not very likely to change.
您可能需要MAC地址,这是单个网络适配器的硬件地址,并且不太可能更改。
#3
1
The provided code returns HOSTNAME/IP-Address(xx.xx.xx.xx).
提供的代码返回HOSTNAME / IP-Address(xx.xx.xx.xx)。
Hostname is your computer name ex: MY-PC and then you get the IP corresponding to it. When you are connected to a network, InetAddress.getLocalHost() asks the DHCP server in the network "what is the address of MY-PC (the name of your computer)", the DHCP replies -> 33.44.55.66
主机名是您的计算机名称ex:MY-PC,然后您将获得与其对应的IP。当您连接到网络时,InetAddress.getLocalHost()会询问网络中的DHCP服务器“MY-PC的地址(您的计算机名称)是什么”,DHCP回复 - > 33.44.55.66
Try the following CMD commands when both connected and disconnected.
连接和断开连接时,请尝试以下CMD命令。
\>hostname
MY-PC
\>nslookup MY-PC
44.55.66.77
When you are not connected to a network there are two possibilities:
当您未连接到网络时,有两种可能性:
- You do not get a hostname (default is localhost)
- 您没有获得主机名(默认为localhost)
- You do get a hostname, but there is no DHCP server on the network to return an IPaddress, so you get loopback - 127.0.0.1
- 你确实得到了一个主机名,但是网络上没有DHCP服务器来返回一个IP地址,所以你得到了环回 - 127.0.0.1
If you want to "call" your computer on the network locally, use the loopback http://www.pcmag.com/encyclopedia/term/57812/loopback-address
如果您想在本地“呼叫”您的计算机,请使用环回http://www.pcmag.com/encyclopedia/term/57812/loopback-address
Hope this helps
希望这可以帮助
#4
0
No. You're confusing IP and MAC addresses. The MAC address is a serial number of hardware(but may be programatically changed on certain chipsets).
不,你在混淆IP和MAC地址。 MAC地址是硬件的序列号(但可以在某些芯片组上以编程方式更改)。
The IP address is either software-determined or determined by the network. It can differ between networks or even with time.
IP地址由软件确定或由网络确定。它可以在网络之间或甚至时间上有所不同。
#5
0
IP addresses are (usually) interface specific, not machine specific.
IP地址(通常)是接口特定的,而不是特定于机器的。
If your machine only has one interface the difference is moot, but it matters if (for example) you have both wired and wireless ethernet.
如果您的机器只有一个接口,那么差异就没有实际意义,但是(例如)您有有线和无线以太网都很重要。
Also note that if you do have both and attempt to use them both at the same time on the same subnet that things will likely get very confused!
另请注意,如果您同时拥有这两者并尝试在同一子网上同时使用它们,那么事情可能会非常混乱!