现在的位置: 首页 > 综合 > 正文

InetAddress测试

2018年05月09日 ⁄ 综合 ⁄ 共 940字 ⁄ 字号 评论关闭
package com.net.ip;

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

/**
 * 封装端口:在InetAddress基础+端口
 * @author Administrator
 *
 */
public class InetSocketDemo1 {

	public static void main(String[] args) throws UnknownHostException {
		// TODO Auto-generated method stub
		InetAddress addr = InetAddress.getLocalHost();
		System.out.println(addr.getHostAddress());
		System.out.println(addr.getHostName());
		
		
		addr = InetAddress.getByName("www.baidu.com");
		System.out.println(addr.getHostAddress());
		System.out.println(addr.getHostName());
		
		
		addr = InetAddress.getByName("58.27.209.39");
		System.out.println(addr.getHostAddress());
		System.out.println(addr.getHostName());
		
		InetSocketAddress address = new InetSocketAddress("localhost", 9999);
		System.out.println(address.getHostName());
		System.out.println(address.getPort());
		
		InetAddress inetAddress = address.getAddress();
		System.out.println(inetAddress.getHostAddress());
		System.out.println(inetAddress.getHostName());
	}

}

抱歉!评论已关闭.