相关疑难解决方法(0)

用Java获取"外部"IP地址

我不太确定如何获取机器的外部IP地址,因为网络外的计算机会看到它.

我的以下IPAddress类仅获取计算机的本地IP地址.

public class IPAddress {

    private InetAddress thisIp;

    private String thisIpAddress;

    private void setIpAdd() {
        try {
            InetAddress thisIp = InetAddress.getLocalHost();
            thisIpAddress = thisIp.getHostAddress().toString();
        } catch (Exception e) {
        }
    }

    protected String getIpAddress() {
        setIpAdd();
        return thisIpAddress;
    }
}
Run Code Online (Sandbox Code Playgroud)

java networking network-programming external ip-address

81
推荐指数
7
解决办法
10万
查看次数