How to get local IP in java

 

There is a class in Java called Application, which can be used to obtain the local IP address, and can also be used to obtain network connection information, such as what hosts are on the network, the host name that needs to access the local machine, etc. However, this class can only be used on this machine. If you want to access an external host, you need to use other methods. First write a java.util.Application() method in the main program, and then define an Application class in the subclass, as shown in Figure 1. Through this class, you can get the local IP address. Of course, this class is not only available on this machine, but can also be used elsewhere. 2. Define an Application method in the subclass (choose the method name as needed). 4. Use the above method to obtain the local IP address (the method name and subclass are both customized). 5. Call the application method to obtain the local IP address. 6. If Debug mode is used in the Java program, you can use Debug mode in this step to obtain the local IP address. But if dynamic IP is used, then the dynamic IP protocol must be used to obtain the local IP address.

  • 1. Obtain dynamic IP

    Dynamic IP means that the router dynamically allocates IP addresses according to certain rules. When a host forwards data from a router, the router saves the IP address in its own memory and calculates the IP address for the next period based on the IP address in the next cycle. IP address. In dynamic IP, the IP address of the host is not fixed. In other words, after a successful connection, the IP address will not change. What needs to be obtained is the current dynamic IP address of the host, that is, the "local dynamic IP address". The dynamic IP protocol used in Java is the IPv4 protocol in the TCP/IP protocol. The IPv4 protocol is not directly related to the Internet. Its main goal is to provide virtual private network services on the Internet. The TCP/IP protocol is one of the three basic protocols used for communication on the Internet. TCP/IP is a set of specifications built on TCP/IP. It stipulates some rules (including datagram format, network layer transmission format, header structure, etc.) and interface specifications that should be followed when communicating on the Internet.

  • 2. Network protocol

    An IP address is a set of numbers that indicates the location of a host on the network. In order for all hosts on the network to use IP addresses, a protocol is needed. This protocol is called IP protocol, also called network protocol. There are three different ways to represent host addresses on the network in the TCP/IP protocol. The first way is to use the network number to express it, the second way is to use the IP address to express it, and the third way is to use the port number to express it. Therefore, in the TCP/IP protocol, IP addresses and port numbers are also used. The third method is used here. To understand the relationship between IP addresses and port numbers, you can take a look at the following figure: According to Figure 3, you can see the connection between the two ports. If there is only one data line between the two ports, then it is 1; if there are two data lines, then it is 2; if there are three data lines, then it is 3. For the connection between the three ports, we can use the following figure to represent it: As shown in Figure 4, it can be seen that there is a data line between the three ports. This data line corresponds to the corresponding network protocol.

  • 3. Basic configuration

    1. First open the cmd command window, enter cmd in the command window, and press Enter. 2. Enter ipconfig in the command window and press Enter.

  • 4. Local configuration

    1. When using the Application method to obtain the local IP address in a Java program, the local application method is not called directly, but a java.util.Application() method is used. If there are multiple local connections, multiple application methods can be used. 2. In the local configuration, use the local machine’s IP address, port, network segment and other information. For example, if the IP address on this machine is 192.168.1.0 and the port is 80, then the address and port need to be set to 192.168.1.1 and 80 respectively. 3. When using Debug mode to obtain the local IP address, just set the above information. 4. If you want to obtain a dynamic IP address, you need to set all the above steps to automatic.

  • 5. Security configuration

    1. First, you need to configure the security of the above class library in Java. The method is to add a security parameter named test in the static member method of the class, as shown in Figure 2. If the Test class is added to the class library, it will automatically detect whether the test class is a static class when the program is running. If it is a static class, the test class cannot be detected when the program is running, and the IP address of the local machine cannot be obtained. 2. Then add the Test class to the class library, as shown in Figure 3. 4. Bind the obtained local IP address to the test class. If you need to access an external host, you need to use the dynamic IP protocol to access it. If you use the static IP protocol, you can obtain the IP address of the external host. 5. Bind the obtained local IP address to the test class, so that you can use the test class to access the external host.

  • 6. Network configuration

    1. Enter the network configuration interface, in the "Local Area Connection" window, enter "Connect to the Internet" and press the Enter key to start the Internet. 2. Go to the Internet, and in the "General" tab, select "Properties". 3. Click "Network", as shown in Figure 3, enter "IP Address" in the pop-up dialog box and press Enter, then click "Properties". As shown in Figure 5. 5. At this time, enter the network setting interface and add the subnet mask and gateway in front of the IP address to complete the network configuration. 6. This completes the acquisition of the local IP address.

  • 7. Operating environment

    4. Based on the above configuration of environment variables, if a network adapter (Network Adapter) is used, then you also need to configure the related variables of the network adapter.

The following are several Java codes to obtain the IP address of the local machine:

1. 使用InetAddress类
```java
import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetLocalIP {
public static void main(String[] args) {
try {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println("本机IP地址:" + localHost.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
```
2. 使用NetworkInterface类
```java
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class GetLocalIP {
public static void main(String[] args) {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (!inetAddress.isLinkLocalAddress() && !inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) {
System.out.println("本机IP地址:" + inetAddress.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
```
3. 使用System类
```java
public class GetLocalIP {
public static void main(String[] args) {
String localIP = null;
try {
localIP = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println("本机IP地址:" + localIP);
}
}
```

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130167416