37 * TIME_WAIT state we should be able to re-bind if |
37 * TIME_WAIT state we should be able to re-bind if |
38 * SO_REUSEADDR is enabled. |
38 * SO_REUSEADDR is enabled. |
39 */ |
39 */ |
40 |
40 |
41 public static void main(String args[]) throws Exception { |
41 public static void main(String args[]) throws Exception { |
|
42 ServerSocket ss = new ServerSocket(0); |
|
43 Socket s1 = null, s2 = null; |
|
44 try { |
|
45 int port = ss.getLocalPort(); |
42 |
46 |
43 InetSocketAddress isa = new InetSocketAddress(0); |
47 s1 = new Socket(InetAddress.getLocalHost(), port); |
44 ServerSocket ss = new ServerSocket(); |
48 s2 = ss.accept(); |
45 ss.bind(isa); |
|
46 |
49 |
47 int port = ss.getLocalPort(); |
50 // close server socket and the accepted connection |
|
51 ss.close(); |
|
52 s2.close(); |
48 |
53 |
49 Socket s1 = new Socket(InetAddress.getLocalHost(), port); |
54 ss = new ServerSocket(); |
50 Socket s2 = ss.accept(); |
55 ss.bind( new InetSocketAddress(port) ); |
|
56 ss.close(); |
51 |
57 |
52 // close server socket and the accepted connection |
58 // close the client socket |
53 ss.close(); |
59 s1.close(); |
54 s2.close(); |
60 } finally { |
55 |
61 if (ss != null) ss.close(); |
56 boolean failed = false; |
62 if (s1 != null) s1.close(); |
57 |
63 if (s2 != null) s2.close(); |
58 ss = new ServerSocket(); |
64 } |
59 ss.bind( new InetSocketAddress(port) ); |
|
60 ss.close(); |
|
61 |
|
62 // close the client socket |
|
63 s1.close(); |
|
64 } |
65 } |
65 } |
66 } |