- 学习java网络编程需要一定的计算机网络只是基础。
- java提供了InetAddress类来代表IP地址,它没有具体的构造器,也就是说不能通过new关键字来获得InetAddress对象
- 一下为代码示例,具体的讲解穿插在代码中
import java.net.InetAddress; public class InetAddressTest { public static void main(String args[]) throws Exception{ InetAddress ip = InetAddress.getByName("www.crazyit.org"); // InetAddress类没有构造方法,可以通过getByName(String host)方法获得对象 System.out.println("crazy是否可以到达: " + ip.isReachable(2000)); // isReachable(int timeout)返回是否可以到达上述的ip地址 System.out.println(ip.getHostAddress()); // getHostAddress()方法返回ip地址(以字符串的方式) InetAddress local = InetAddress.getByAddress(new byte[]{127,0,0,1}); // InetAddress类没有构造方法,getByAddress(byte[] addr)方法获得对象 System.out.println("本机是否可以到达" + local.isReachable(5000)); System.out.println(local.getCanonicalHostName()); // getCanonicalHostName()方法返回全限定域名 } }
这是我看李刚编著的《疯狂Java讲义》后总结出来的。
java网络编程(1) InetAddress
