正在学习java的socket编程。在看源码时候发现,InetAddress类中的getAddress()方法的返回值为null;
表示很蒙圈,望大神能解答
InetAddress类中的getAddress()方法的返回值为什么直接是null?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
Enzoooooo 2017-08-08 06:38关注public static void main(String[] args) throws UnknownHostException {
InetAddress address = InetAddress.getLocalHost();
System.out.println("this is address:" + address);String hostName = address.getHostName(); String hostAddress = address.getHostAddress(); System.out.println("this is host name:" + hostName); System.out.println("this is host address:" + hostAddress); byte[] address4Byte = address.getAddress(); System.out.println("this is address4Byte len:" + address4Byte.length); for (int i = 0; i < address4Byte.length; i++) { System.out.print(address4Byte[i]); } } 这是楼主写的测试方法。我用byte[] address4Byte指向了address.getAddress();这个方法,但是address.getAddress();返回值是null; 但还是能获取到我的主机地址,这是为什么呢?本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1