1
1
/*
2
- * Copyright (c) 2012, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2012, 2024 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
34
34
import java .nio .*;
35
35
import java .nio .channels .*;
36
36
import java .io .IOException ;
37
+
37
38
import jdk .test .lib .net .IPSupport ;
38
39
39
40
public class Disconnect {
@@ -42,43 +43,61 @@ public static void main(String[] args) throws IOException {
42
43
43
44
// test with default protocol family
44
45
try (DatagramChannel dc = DatagramChannel .open ()) {
45
- test (dc );
46
- test (dc );
46
+ InetAddress lo = InetAddress .getLoopbackAddress ();
47
+ System .out .println ("Testing with default family and " + lo );
48
+ test (dc , lo );
49
+ test (dc , lo );
47
50
}
48
51
49
52
if (IPSupport .hasIPv4 ()) {
50
53
// test with IPv4 only
51
54
try (DatagramChannel dc = DatagramChannel .open (StandardProtocolFamily .INET )) {
52
- test (dc );
53
- test (dc );
55
+ InetAddress lo4 = InetAddress .ofLiteral ("127.0.0.1" );
56
+ System .out .println ("Testing with INET family and " + lo4 );
57
+ test (dc , lo4 );
58
+ test (dc , lo4 );
54
59
}
55
60
}
56
61
57
62
if (IPSupport .hasIPv6 ()) {
58
63
// test with IPv6 only
59
64
try (DatagramChannel dc = DatagramChannel .open (StandardProtocolFamily .INET6 )) {
60
- test (dc );
61
- test (dc );
65
+ InetAddress lo6 = InetAddress .ofLiteral ("::1" );
66
+ System .out .println ("Testing with INET6 family and " + lo6 );
67
+ test (dc , lo6 );
68
+ test (dc , lo6 );
62
69
}
63
70
}
64
71
}
65
72
73
+ static int getLocalPort (DatagramChannel ch ) throws IOException {
74
+ return ((InetSocketAddress ) ch .getLocalAddress ()).getPort ();
75
+ }
76
+
66
77
/**
67
78
* Connect DatagramChannel to a server, write a datagram and disconnect. Invoke
68
79
* a second or subsequent time with the same DatagramChannel instance to check
69
80
* that disconnect works as expected.
70
81
*/
71
- static void test (DatagramChannel dc ) throws IOException {
82
+ static void test (DatagramChannel dc , InetAddress lo ) throws IOException {
72
83
try (DatagramChannel server = DatagramChannel .open ()) {
73
- server .bind (new InetSocketAddress (0 ));
84
+ server .bind (new InetSocketAddress (lo , 0 ));
74
85
75
- InetAddress lh = InetAddress .getLocalHost ();
76
- dc .connect (new InetSocketAddress (lh , server .socket ().getLocalPort ()));
86
+ SocketAddress dcbound = dc .getLocalAddress ();
87
+ dc .connect (new InetSocketAddress (lo , server .socket ().getLocalPort ()));
88
+ System .out .println ("dc bound to " + dcbound + " and connected from " +
89
+ dc .getLocalAddress () + " to " + dc .getRemoteAddress ());
77
90
78
91
dc .write (ByteBuffer .wrap ("hello" .getBytes ()));
79
92
80
- ByteBuffer bb = ByteBuffer .allocate (100 );
81
- server .receive (bb );
93
+ if (getLocalPort (dc ) != getLocalPort (server )) {
94
+ ByteBuffer bb = ByteBuffer .allocate (100 );
95
+ server .receive (bb );
96
+ } else {
97
+ // some systems may allow dc and server to bind to the same port.
98
+ // when that happen the datagram may never be received
99
+ System .out .println ("Server and clients are bound to the same port: skipping receive" );
100
+ }
82
101
83
102
dc .disconnect ();
84
103
0 commit comments