catchip.java

来自「JAVA案例开发集锦源代码 袁然 郑自国编」· Java 代码 · 共 40 行

JAVA
40
字号
//chp 9
/**
* 获取IP地址
*/

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class CatchIp {
        private InetAddress LocalIP = null;
        private InetAddress ServerIP = null;
        public static void main(String args[]) {
                CatchIp mytest;
                mytest = new CatchIp();
                System.out.println("LocalHost IP is: " + mytest.catchLocalIP());
                System.out.println("Server IP is :" + mytest.catchServerIP());
        }
    //取得本机IP地址
        public InetAddress catchLocalIP() {
                try {
                        LocalIP = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                }
                return LocalIP;
        }

        public InetAddress catchServerIP() {
                try {
                        ServerIP = InetAddress.getByName("www.sina.com.cn");
                        //取得 www.sina.com.cn 的IP地址
                } catch (UnknownHostException e) {
                        System.out.println(e.getMessage());
                }
                return ServerIP;
        }
}//end class CatchIp


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?