地址及端口

InetAddress    封装ip及DNS

import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressDemo {
	public static void main(String[] args) {
		try {
			InetAddress  inet=InetAddress.getLocalHost();
			System.out.println(inet.getHostAddress());//返回本机地址
			System.out.println(inet.getHostName());//返回本机的计算机名
			inet=InetAddress.getByName("www.163.com");
			System.out.println(inet.getHostAddress());//返回服务器地址
			System.out.println(inet.getHostName());//返回服务器的ip
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
}

InetSockeAddress

import java.net.InetAddress;
import java.net.InetSocketAddress;
public class InetSockeAddressDemo {
	public static void main(String[] args) {
		InetSocketAddress inetSocke=InetSocketAddress.createUnresolved("localhost", 8888);
		System.out.println(inetSocke.getHostName());//返回本机
		System.out.println(inetSocke.getPort());//返回端口号
	}
}

你可能感兴趣的:(地址及端口)