web123456

Java Get IP Address

existJava programmingIn Java, we often need to get the IP address of a local or remote computer, which is a set of numbers used to uniquely identify a computer and plays an important role in network communication. The following will introduce several methods to obtain the IP address in Java, and provide the corresponding source code.

  1. Get local IP address

To get the localcalculatorIP address, we can use Java'sInetAddressClass. This class provides a number of functions related to network communication, including methods for obtaining an IP address. The following is a code example for obtaining a local IP address:

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

public class LocalIPExample {
    public static void main(String[] args) {
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            String ipAddress = localHost.getHostAddress();
            System.out.println("Local IP address:" + ipAddress);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

In the above code, we use()method to get the local host'sInetAddressobject, and then use thegetHostAddress()method to get the IP address. Finally, we print out the IP address.

  1. Getting a remote IP address

To get the IP address of a remote computer, we can use theInetAddressclassgetByName()method. The method accepts a hostname or IP address as a parameter and returns the correspondingInetAddressObject. The following is a code example for obtaining a remote IP address:

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

public class RemoteIPExample {
    public static void main(String[] args) {
        String hostname = ""; // Remote hostname or IP address

        try {
            InetAddress remoteHost = InetAddress.getByName(hostname);
            String ipAddress = remoteHost.getHostAddress();
            System.out.println("Remote IP address:" + ipAddress);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

In the above code, we have passed the()method passes in a remote hostname or IP address to get the correspondingInetAddressobject. Then, use thegetHostAddress()method to get the IP address and output it.

  1. Get client IP address

In web application development, sometimes we need to get the IP address of the client. This can be done using theHttpServletRequestobject to get the client IP address. The following is a code example to get the client IP address:

import javax.servlet.http.HttpServletRequest;

public class ClientIPExample {
    public static void main(String[] args) {
        HttpServletRequest request = ...; // Get the HttpServletRequest object, omit the fetching process here.

        String ipAddress = request.getRemoteAddr();
        System.out.println("Client IP address:" + ipAddress);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

In the above code, we assume that we have obtained theHttpServletRequestobject and use thegetRemoteAddr()method to obtain the IP address of the client.

summarize

This article describes several common ways to get an IP address in Java. This is accomplished by using theInetAddressclass, we can easily get the IP addresses of local and remote computers. For web applications, you can use theHttpServletRequestobject to obtain the IP address of the client. Depending on the specific needs, choose the appropriate method to obtain the IP address and process it accordingly in the application program.