1 /* |
1 /* |
2 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
69 .filter(Inet6Address::isLinkLocalAddress) |
69 .filter(Inet6Address::isLinkLocalAddress) |
70 .findFirst(); |
70 .findFirst(); |
71 } |
71 } |
72 |
72 |
73 static void test1(Inet6Address sin) throws Exception { |
73 static void test1(Inet6Address sin) throws Exception { |
74 try (ServerSocket ssock = new ServerSocket(0); |
74 try (ServerSocket ssock = createBoundServer(sin); |
75 Socket sock = new Socket()) { |
75 Socket sock = new Socket()) { |
76 int port = ssock.getLocalPort(); |
76 int port = ssock.getLocalPort(); |
77 sock.connect(new InetSocketAddress(sin, port), 100); |
77 sock.connect(new InetSocketAddress(sin, port), 100); |
78 } catch (SocketTimeoutException e) { |
78 } catch (SocketTimeoutException e) { |
79 // time out exception is okay |
79 // time out exception is okay |
80 System.out.println("timed out when connecting."); |
80 System.out.println("timed out when connecting."); |
81 } |
81 } |
82 } |
82 } |
83 |
83 |
84 static void test2(Inet6Address sin) throws Exception { |
84 static void test2(Inet6Address sin) throws Exception { |
85 try (ServerSocket ssock = new ServerSocket(0); |
85 try (ServerSocket ssock = createBoundServer(sin); |
86 Socket sock = new Socket()) { |
86 Socket sock = new Socket()) { |
87 int port = ssock.getLocalPort(); |
87 int port = ssock.getLocalPort(); |
88 ssock.setSoTimeout(100); |
88 ssock.setSoTimeout(100); |
89 sock.bind(new InetSocketAddress(sin, 0)); |
89 sock.bind(new InetSocketAddress(sin, 0)); |
90 sock.connect(new InetSocketAddress(sin, port), 100); |
90 sock.connect(new InetSocketAddress(sin, port), 100); |
91 } catch (SocketTimeoutException expected) { |
91 } catch (SocketTimeoutException expected) { |
92 // time out exception is okay |
92 // time out exception is okay |
93 System.out.println("timed out when connecting."); |
93 System.out.println("timed out when connecting."); |
94 } |
94 } |
|
95 } |
|
96 |
|
97 static ServerSocket createBoundServer(Inet6Address sin) throws IOException { |
|
98 ServerSocket ss = new ServerSocket(); |
|
99 InetSocketAddress address = new InetSocketAddress(sin, 0); |
|
100 ss.bind(address); |
|
101 return ss; |
95 } |
102 } |
96 |
103 |
97 public static void main(String[] args) throws Exception { |
104 public static void main(String[] args) throws Exception { |
98 Optional<Inet6Address> oaddr = getLocalAddr(); |
105 Optional<Inet6Address> oaddr = getLocalAddr(); |
99 if (!oaddr.isPresent()) { |
106 if (!oaddr.isPresent()) { |