Here you can find the source of getLocalInternetProtocolAddress()
Parameter | Description |
---|---|
SocketException | an exception |
public static String getLocalInternetProtocolAddress() throws SocketException
//package com.java2s; //License from project: Apache License import java.net.*; import java.util.Enumeration; public class Main { /**// w w w . j a v a 2 s.c o m * @return The local Internet protocol address of this machine * @throws SocketException */ public static String getLocalInternetProtocolAddress() throws SocketException { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { for (InterfaceAddress interfaceAddress : networkInterfaces.nextElement().getInterfaceAddresses()) { if (interfaceAddress.getAddress().isSiteLocalAddress()) { return interfaceAddress.getAddress().toString().replace("/", ""); } } } throw new IllegalStateException("Expected the computer's local IP address but didn't get one!"); } }