codecamp

InetAddress

InetAddress测试: 解决ip地址问题。

import java.net.InetAddress;
import java.net.UnknownHostException;


public class Test {
    public static void main(String[] args) throws UnknownHostException {
        /**
         * InetAddress没有封装端口
         */
        //使用getLocalHost方法创建InetAddress对象
        InetAddress address = InetAddress.getLocalHost();
        System.out.println(address.getHostAddress());//返回本机的ip地址
        System.out.println(address.getHostName());//输出本机计算机名
        //根据域名得到InetAddress对象
        address = InetAddress.getByName("www.163.com");
        System.out.println(address.getHostAddress());//输出163服务器的ip:125.39.21.3
        System.out.println(address.getHostName());//输出域名:www.163.com
        //根据ip得到InetAddress对象
        address = InetAddress.getByName("125.39.21.3");
        System.out.println(address.getHostAddress());//输出163服务器的ip:125.39.21.3
        System.out.println(address.getHostName());//如果ip被解析(DNS)则输出域名,如果没被解析则输出ip。
    }
}
基本概念
InetSocketAddress
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }