How to Determine Internet Network Interface in Java

后端 未结 4 811
再見小時候
再見小時候 2020-12-05 08:10

How do you determine which network interface is connected to the internet using Java? For example, I run

InetAddress.getLocalHost().getHostAddress();
         


        
4条回答
  •  死守一世寂寞
    2020-12-05 09:05

    Take a look at Retrieving Network Interfaces and Listing Network Interface Addresses in the Java Tutorial.

    If you have several network interfaces which are up and running, you must select programmatically the network interface that your program should use.

    UPDATE:

    I've found the question that is similar to yours.
    See the answer to how-to-check-if-internet-connection-is-present-in-java.

    UPDATE 2:

    IMHO You should do the following in your program:

    1. Find all IPv4 network interfaces on the machine that your program is running on;
    2. Ask the user which of them your program should use;
    3. Use the interface, that the user pointed to.

提交回复
热议问题