天天看点

java获取ip代码(全部复制运行)

package com.prochanges.ft;

import java.net.*;

public class ymJXIP {

    InetAddress myIPaddress = null;

    InetAddress myServer = null;

    public static void main(String args[]) {

    ymJXIP mytool;

        mytool = new ymJXIP();

        System.out.println("Your host IP is: " + mytool.getMyIP());

        System.out.println("The Server IP is :" + mytool.getServerIP());

    }

    // 取得LOCALHOST的IP地址

    public InetAddress getMyIP() {

        try {

            myIPaddress = InetAddress.getLocalHost();

        } catch (UnknownHostException e) {

        }

        return (myIPaddress);

    }

    // 取得 www.abc.com 的IP地址

    public InetAddress getServerIP() {

        try {

            myServer = InetAddress.getByName("www.baidu.com");

        } catch (UnknownHostException e) {

        }

        return (myServer);

    }

}