Intenet 地址

java.net.InetAddress类是java对Ip地址(包括ipv4和ipv6)的高层表示,大多数其他网络类都要用到这个类,包括Socket, ServerSocket, URL, DatagramSocket, DatagramPacket等

InetAddress address = InetAddress.getByName("www.baidu.com"); //生产InetAddress的工厂方法,设置InetAdress类中的一个私有字段,并建立与本地DNS服务器的一个链接,来查找数组和名字地址 , 不检查DNS

InetAddress[] address = InetAddress.getAllByName("www.baidu.com");//得到所有地址

InetAddress address = InetAddress.getLocalHost(); //得到主机地址,即运行这段代码的主机地址 ,不可信代码总是返回主机名127.0.0.1

Ipublic static InetAddress.getByAddress(byte[] addr) throws UnknownHostException //用一个IP地址创建一个InetAddress对象

Ipublic static InetAddress.getByAddress(String hostname,byte[] addr) throws UnknownHostException //用一个主机名和一个IP地址创建一个InetAddress对象

byte[] address = {107,23,(byte)216,(byte)196}  //必须把两个大数组转换成字节
InetAddress lessWrong= InetAddress.getByAddress(address)  ;

SecurityManager s = new SecurityManager();
s.checkConnect("223.210.232.29", 80);如果调用线程不允许打开与指定的主机和端口号的套接字连接,则抛出一个 SecurityException

posted @ 2019-03-10 23:26  江期玉  阅读(270)  评论(0编辑  收藏  举报