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()); } }