This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Question: How do I use Java to get the IP address of the current computer?

I’m trying to develop a system with multiple nodes running on different systems, or on different ports on the same system.

Now, all nodes have created a socket with the destination IP being the IP of a special node called the boot node. The node then creates its own ServerSocket and starts listening for connections.

Bootstrap nodes maintain a list of nodes and return them when queried.

Now all I need is that the node must register its IP with the boot node. I tried using cli.getinetAddress () when the client connected to the ServerSocket of the boot node, but it didn’t work.

  1. I need clients to register their PPP IP(if available)
  2. Otherwise use LAN IP (if available)
  3. Otherwise it must be registered as127.0.0.1

Use the following code

System.out.println(Inet4Address.getLocalHost().getHostAddress());
Copy the code

or

System.out.println(InetAddress.getLocalHost().getHostAddress());
Copy the code

My PPP IP connection address is: 117.204.44.192, but 192.168.1.2 is returned

I used the following code again, which lists all the IP addresses associated with the network interface, but how do I distinguish them?

Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements())
{
    NetworkInterface n = (NetworkInterface) e.nextElement();
    Enumeration ee = n.getInetAddresses();
    while(ee.hasMoreElements()) { InetAddress i = (InetAddress) ee.nextElement(); System.out.println(i.getHostAddress()); }}Copy the code

Output:

127.0. 01.
192.1681.2.
192.168. 561.
117.20444.19.
Copy the code

Popular answers

import java.net.DatagramSocket;
import java.net.InetAddress;

try(final DatagramSocket socket = new DatagramSocket()){
  socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
  ip = socket.getLocalAddress().getHostAddress();
}
Copy the code

This works well when there are multiple network interfaces. It always returns the preferred outbound IP. No need to reach the destination 8.8.8.8.

A connection on a UDP socket has the following effects: it sets the destination to send/receive, discards all packets from other addresses, converts the socket to the “connected” state, and sets its corresponding fields. This includes checking the existence of a route to a destination against the system’s routing table and setting up the local endpoint accordingly. This last part doesn’t seem to be officially documented, but it looks like a complete feature of the Berkeley Sockets API (a side effect of UDP “connection” status) that can be used across Windows and Linux.

Therefore, this method provides the local address to connect to the specified remote host. However, it does not establish a real connection, so the specified remote IP may also be inaccessible.

For MacOS you can do this:

Socket socket = new Socket();
socket.connect(new InetSocketAddress("google.com".80));
System.out.println(socket.getLocalAddress());
Copy the code

The article translated from Stack Overflow:stackoverflow.com/questions/9…