📄 inetsocketaddresstest.java
字号:
import java.net.InetAddress;import java.net.Inet6Address;import java.net.InetSocketAddress;/** * Test for the InetSocketAddress class. */public class InetSocketAddressTest{ public static void main(String args[]) { InetSocketAddress isa; /* Argument checking */ try { new InetSocketAddress((String)null, 0); } catch(IllegalArgumentException e) { System.out.println(e); } try { new InetSocketAddress(100000); } catch(IllegalArgumentException e) { System.out.println(e); } try { new InetSocketAddress(65536); } catch(IllegalArgumentException e) { System.out.println(e); } try { new InetSocketAddress(-1); } catch(IllegalArgumentException e) { System.out.println(e); } try { new InetSocketAddress(-128); } catch(IllegalArgumentException e) { System.out.println(e); } /* Unresolved vs. Resolved addresses. */ isa = new InetSocketAddress("bad.bad.bad", 0); System.out.println("Unresolved " + isa.getHostName() + ": " + isa.isUnresolved()); System.out.println("Unresolved toString(): " + isa); if( isa.equals(new InetSocketAddress("bad.bad.bad", 128)) ) { System.out.println("Bad equals?"); } if( !isa.equals(new InetSocketAddress("bad.bad.bad", 0)) ) { System.out.println("Bad equals?"); } isa = new InetSocketAddress("localhost", 0); System.out.println("Unresolved " + isa.getHostName() + ": " + isa.isUnresolved()); System.out.println("Resolved toString(): " + check(isa)); if( isa.equals(new InetSocketAddress("localhost", 128)) ) { System.out.println("Bad equals?"); } if( !isa.equals(new InetSocketAddress("localhost", 0)) ) { System.out.println("Bad equals?"); } /* Wildcard address */ isa = new InetSocketAddress(128); System.out.println("Wildcard address: " + isa.getAddress().isAnyLocalAddress()); System.out.println("Port: " + isa.getPort()); isa = new InetSocketAddress((InetAddress)null, 0); System.out.println("Null address is wildcard: " + isa.getAddress().isAnyLocalAddress()); } private static String check(InetSocketAddress isa) { if (isa.getAddress() instanceof Inet6Address) { if (isa.toString().equals("localhost/::::::::1:0")) { return "localhost/127.0.0.1:0"; } } return isa.toString(); }}/* Expected Output:java.lang.IllegalArgumentException: Null host name valuejava.lang.IllegalArgumentException: Bad port number: 100000java.lang.IllegalArgumentException: Bad port number: 65536java.lang.IllegalArgumentException: Bad port number: -1java.lang.IllegalArgumentException: Bad port number: -128Unresolved bad.bad.bad: trueUnresolved toString(): bad.bad.bad:0Unresolved localhost: falseResolved toString(): localhost/127.0.0.1:0Wildcard address: truePort: 128Null address is wildcard: true*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -