目录
Java中socket编程涉及到Socket和ServerSocket两个常用类,在java.net包中。java.net.InetAddress的实例包含一个ip地址,该类的构造函数私有,需要通过静态方法来获取实例,常用的方法有:
static InetAddress[] getAllByName(String host)://通过主机名获得Inet实例
static InetAddress getByAddress(byte[] addr)://通过IP地址获得
byte[] ba={(byte)220,(byte)181,(byte)111,(byte)188};
InetAddress ip=InetAddress.getByAddress(ba); 实例:/220.181.111.188
static InetAddress getByAddress(String host,byte[] addr)://通过主机名和地址获得
static InetAddress getByName(String host)://通过主机名获得
InetAddress ip2=InetAddress.getByName("www.baidu.com");//实例:www.baidu.com/220.181.111.188
static InetAddress getLocalHost();//获得本机的InetAddress实例
java.net.Socket用于创建客户端的socket
构造函数