Skip to content

Commit 686eb23

Browse files
committed
8336817: Several methods on DatagramSocket and MulticastSocket do not specify behaviour when already closed or connected
Reviewed-by: dfuchs, alanb
1 parent 01d03e0 commit 686eb23

File tree

2 files changed

+57
-41
lines changed

2 files changed

+57
-41
lines changed

src/java.base/share/classes/java/net/DatagramSocket.java

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -395,7 +395,7 @@ public DatagramSocket(int port, InetAddress laddr) throws SocketException {
395395
*
396396
* @param addr The address and port to bind to.
397397
* @throws SocketException if any error happens during the bind, or if the
398-
* socket is already bound.
398+
* socket is already bound or is closed.
399399
* @throws SecurityException if a security manager exists and its
400400
* {@code checkListen} method doesn't allow the operation.
401401
* @throws IllegalArgumentException if addr is a SocketAddress subclass
@@ -422,6 +422,11 @@ public void bind(SocketAddress addr) throws SocketException {
422422
* call to send or receive may throw a PortUnreachableException. Note,
423423
* there is no guarantee that the exception will be thrown.
424424
*
425+
* <p> If this socket is already connected, then this method will attempt to
426+
* connect to the given address. If this connect fails then the state of
427+
* this socket is unknown - it may or may not be connected to the address
428+
* that it was previously connected to.
429+
*
425430
* <p> If a security manager has been installed then it is invoked to check
426431
* access to the remote address. Specifically, if the given {@code address}
427432
* is a {@link InetAddress#isMulticastAddress multicast address},
@@ -461,7 +466,7 @@ public void bind(SocketAddress addr) throws SocketException {
461466
* not permit access to the given remote address
462467
*
463468
* @throws UncheckedIOException
464-
* may be thrown if connect fails, for example, if the
469+
* if the port is 0 or connect fails, for example, if the
465470
* destination address is non-routable
466471
*
467472
* @see #disconnect
@@ -484,6 +489,11 @@ public void connect(InetAddress address, int port) {
484489
* have not been {@linkplain #receive(DatagramPacket) received} before invoking
485490
* this method, may be discarded.
486491
*
492+
* <p> If this socket is already connected, then this method will attempt to
493+
* connect to the given address. If this connect fails then the state of
494+
* this socket is unknown - it may or may not be connected to the address
495+
* that it was previously connected to.
496+
*
487497
* @param addr The remote address.
488498
*
489499
* @throws SocketException
@@ -643,7 +653,7 @@ public SocketAddress getLocalSocketAddress() {
643653
*
644654
* @param p the {@code DatagramPacket} to be sent.
645655
*
646-
* @throws IOException if an I/O error occurs.
656+
* @throws IOException if an I/O error occurs, or the socket is closed.
647657
* @throws SecurityException if a security manager exists and its
648658
* {@code checkMulticast} or {@code checkConnect}
649659
* method doesn't allow the send.
@@ -702,7 +712,7 @@ public void send(DatagramPacket p) throws IOException {
702712
*
703713
* @param p the {@code DatagramPacket} into which to place
704714
* the incoming data.
705-
* @throws IOException if an I/O error occurs.
715+
* @throws IOException if an I/O error occurs, or the socket is closed.
706716
* @throws SocketTimeoutException if setSoTimeout was previously called
707717
* and the timeout has expired.
708718
* @throws PortUnreachableException may be thrown if the socket is connected
@@ -770,7 +780,8 @@ public int getLocalPort() {
770780
* operation to have effect.
771781
*
772782
* @param timeout the specified timeout in milliseconds.
773-
* @throws SocketException if there is an error in the underlying protocol, such as an UDP error.
783+
* @throws SocketException if there is an error in the underlying protocol,
784+
* such as an UDP error, or the socket is closed.
774785
* @throws IllegalArgumentException if {@code timeout} is negative
775786
* @since 1.1
776787
* @see #getSoTimeout()
@@ -784,7 +795,8 @@ public void setSoTimeout(int timeout) throws SocketException {
784795
* option is disabled (i.e., timeout of infinity).
785796
*
786797
* @return the setting for SO_TIMEOUT
787-
* @throws SocketException if there is an error in the underlying protocol, such as an UDP error.
798+
* @throws SocketException if there is an error in the underlying protocol,
799+
* such as an UDP error, or the socket is closed.
788800
* @since 1.1
789801
* @see #setSoTimeout(int)
790802
*/
@@ -820,8 +832,8 @@ public int getSoTimeout() throws SocketException {
820832
* @param size the size to which to set the send buffer
821833
* size, in bytes. This value must be greater than 0.
822834
*
823-
* @throws SocketException if there is an error
824-
* in the underlying protocol, such as an UDP error.
835+
* @throws SocketException if there is an error in the underlying protocol,
836+
* such as an UDP error, or the socket is closed.
825837
* @throws IllegalArgumentException if the value is 0 or is
826838
* negative.
827839
* @see #getSendBufferSize()
@@ -841,8 +853,8 @@ public void setSendBufferSize(int size) throws SocketException {
841853
* getOption(StandardSocketOptions.SO_SNDBUF)}.
842854
*
843855
* @return the value of the SO_SNDBUF option for this {@code DatagramSocket}
844-
* @throws SocketException if there is an error in
845-
* the underlying protocol, such as an UDP error.
856+
* @throws SocketException if there is an error in the underlying protocol,
857+
* such as an UDP error, or the socket is closed.
846858
* @see #setSendBufferSize
847859
* @see StandardSocketOptions#SO_SNDBUF
848860
* @since 1.2
@@ -878,8 +890,8 @@ public int getSendBufferSize() throws SocketException {
878890
* @param size the size to which to set the receive buffer
879891
* size, in bytes. This value must be greater than 0.
880892
*
881-
* @throws SocketException if there is an error in
882-
* the underlying protocol, such as an UDP error.
893+
* @throws SocketException if there is an error in the underlying protocol,
894+
* such as an UDP error, or the socket is closed.
883895
* @throws IllegalArgumentException if the value is 0 or is
884896
* negative.
885897
* @see #getReceiveBufferSize()
@@ -899,7 +911,8 @@ public void setReceiveBufferSize(int size) throws SocketException {
899911
* getOption(StandardSocketOptions.SO_RCVBUF)}.
900912
*
901913
* @return the value of the SO_RCVBUF option for this {@code DatagramSocket}
902-
* @throws SocketException if there is an error in the underlying protocol, such as an UDP error.
914+
* @throws SocketException if there is an error in the underlying protocol,
915+
* such as an UDP error, or the socket is closed.
903916
* @see #setReceiveBufferSize(int)
904917
* @see StandardSocketOptions#SO_RCVBUF
905918
* @since 1.2
@@ -959,8 +972,8 @@ public void setReuseAddress(boolean on) throws SocketException {
959972
* getOption(StandardSocketOptions.SO_REUSEADDR)}.
960973
*
961974
* @return a {@code boolean} indicating whether or not SO_REUSEADDR is enabled.
962-
* @throws SocketException if there is an error
963-
* in the underlying protocol, such as an UDP error.
975+
* @throws SocketException if there is an error in the underlying protocol,
976+
* such as an UDP error, or the socket is closed.
964977
* @since 1.4
965978
* @see #setReuseAddress(boolean)
966979
* @see StandardSocketOptions#SO_REUSEADDR
@@ -983,9 +996,8 @@ public boolean getReuseAddress() throws SocketException {
983996
* @param on
984997
* whether or not to have broadcast turned on.
985998
*
986-
* @throws SocketException
987-
* if there is an error in the underlying protocol, such as an UDP
988-
* error.
999+
* @throws SocketException if there is an error in the underlying protocol,
1000+
* such as an UDP error, or the socket is closed.
9891001
*
9901002
* @since 1.4
9911003
* @see #getBroadcast()
@@ -1003,8 +1015,8 @@ public void setBroadcast(boolean on) throws SocketException {
10031015
* getOption(StandardSocketOptions.SO_BROADCAST)}.
10041016
*
10051017
* @return a {@code boolean} indicating whether or not SO_BROADCAST is enabled.
1006-
* @throws SocketException if there is an error
1007-
* in the underlying protocol, such as an UDP error.
1018+
* @throws SocketException if there is an error in the underlying protocol,
1019+
* such as an UDP error, or the socket is closed.
10081020
* @since 1.4
10091021
* @see #setBroadcast(boolean)
10101022
* @see StandardSocketOptions#SO_BROADCAST
@@ -1049,8 +1061,8 @@ public boolean getBroadcast() throws SocketException {
10491061
* setOption(StandardSocketOptions.IP_TOS, tc)}.
10501062
*
10511063
* @param tc an {@code int} value for the bitset.
1052-
* @throws SocketException if there is an error setting the
1053-
* traffic class or type-of-service
1064+
* @throws SocketException if there is an error setting the traffic class or type-of-service,
1065+
* or the socket is closed.
10541066
* @since 1.4
10551067
* @see #getTrafficClass
10561068
* @see StandardSocketOptions#IP_TOS
@@ -1074,8 +1086,8 @@ public void setTrafficClass(int tc) throws SocketException {
10741086
* getOption(StandardSocketOptions.IP_TOS)}.
10751087
*
10761088
* @return the traffic class or type-of-service already set
1077-
* @throws SocketException if there is an error obtaining the
1078-
* traffic class or type-of-service value.
1089+
* @throws SocketException if there is an error obtaining the traffic class
1090+
* or type-of-service value, or the socket is closed.
10791091
* @since 1.4
10801092
* @see #setTrafficClass(int)
10811093
* @see StandardSocketOptions#IP_TOS
@@ -1092,6 +1104,9 @@ public int getTrafficClass() throws SocketException {
10921104
*
10931105
* <p> If this socket has an associated channel then the channel is closed
10941106
* as well.
1107+
*
1108+
* <p> Once closed, several of the methods defined by this class will throw
1109+
* an exception if invoked on the closed socket.
10951110
*/
10961111
public void close() {
10971112
delegate().close();
@@ -1299,7 +1314,7 @@ public Set<SocketOption<?>> supportedOptions() {
12991314
* datagram packets, or {@code null}.
13001315
* @throws IOException if there is an error joining, or when the address
13011316
* is not a multicast address, or the platform does not support
1302-
* multicasting
1317+
* multicasting, or the socket is closed
13031318
* @throws SecurityException if a security manager exists and its
13041319
* {@code checkMulticast} method doesn't allow the join.
13051320
* @throws IllegalArgumentException if mcastaddr is {@code null} or is a
@@ -1343,7 +1358,7 @@ public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
13431358
* is unspecified: any interface may be selected or the operation
13441359
* may fail with a {@code SocketException}.
13451360
* @throws IOException if there is an error leaving or when the address
1346-
* is not a multicast address.
1361+
* is not a multicast address, or the socket is closed.
13471362
* @throws SecurityException if a security manager exists and its
13481363
* {@code checkMulticast} method doesn't allow the operation.
13491364
* @throws IllegalArgumentException if mcastaddr is {@code null} or is a

src/java.base/share/classes/java/net/MulticastSocket.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public MulticastSocket(SocketAddress bindaddr) throws IOException {
221221
*
222222
* @param ttl the time-to-live
223223
* @throws IOException if an I/O exception occurs
224-
* while setting the default time-to-live value
224+
* while setting the default time-to-live value, or the socket is closed.
225225
* @deprecated use the {@link #setTimeToLive(int)} method instead, which uses
226226
* <b>int</b> instead of <b>byte</b> as the type for ttl.
227227
* @see #getTTL()
@@ -250,7 +250,7 @@ public void setTTL(byte ttl) throws IOException {
250250
*
251251
* @throws IOException
252252
* if an I/O exception occurs while setting the
253-
* default time-to-live value
253+
* default time-to-live value, or the socket is closed.
254254
*
255255
* @see #getTimeToLive()
256256
* @see StandardSocketOptions#IP_MULTICAST_TTL
@@ -265,7 +265,7 @@ public void setTimeToLive(int ttl) throws IOException {
265265
* the socket.
266266
*
267267
* @throws IOException if an I/O exception occurs
268-
* while getting the default time-to-live value
268+
* while getting the default time-to-live value, or the socket is closed.
269269
* @return the default time-to-live value
270270
* @deprecated use the {@link #getTimeToLive()} method instead,
271271
* which returns an <b>int</b> instead of a <b>byte</b>.
@@ -285,7 +285,7 @@ public byte getTTL() throws IOException {
285285
* getOption(StandardSocketOptions.IP_MULTICAST_TTL)}.
286286
*
287287
* @throws IOException if an I/O exception occurs while
288-
* getting the default time-to-live value
288+
* getting the default time-to-live value, or the socket is closed.
289289
* @return the default time-to-live value
290290
* @see #setTimeToLive(int)
291291
* @see StandardSocketOptions#IP_MULTICAST_TTL
@@ -311,7 +311,7 @@ public int getTimeToLive() throws IOException {
311311
* @param mcastaddr is the multicast address to join
312312
* @throws IOException if there is an error joining,
313313
* or when the address is not a multicast address,
314-
* or the platform does not support multicasting
314+
* or the platform does not support multicasting, or the socket is closed.
315315
* @throws SecurityException if a security manager exists and its
316316
* {@code checkMulticast} method doesn't allow the join.
317317
* @deprecated This method does not accept the network interface on
@@ -339,7 +339,7 @@ public void joinGroup(InetAddress mcastaddr) throws IOException {
339339
*
340340
* @param mcastaddr is the multicast address to leave
341341
* @throws IOException if there is an error leaving
342-
* or when the address is not a multicast address.
342+
* or when the address is not a multicast address, or the socket is closed.
343343
* @throws SecurityException if a security manager exists and its
344344
* {@code checkMulticast} method doesn't allow the operation.
345345
* @deprecated This method does not accept the network interface on which
@@ -393,7 +393,7 @@ public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
393393
*
394394
* @param inf the InetAddress
395395
* @throws SocketException if there is an error in
396-
* the underlying protocol, such as a TCP error.
396+
* the underlying protocol, such as a TCP error, or the socket is closed.
397397
* @deprecated The InetAddress may not uniquely identify
398398
* the network interface. Use
399399
* {@link #setNetworkInterface(NetworkInterface)} instead.
@@ -413,7 +413,7 @@ public void setInterface(InetAddress inf) throws SocketException {
413413
* or if no interface has been set, an {@code InetAddress}
414414
* representing any local address.
415415
* @throws SocketException if there is an error in the
416-
* underlying protocol, such as a TCP error.
416+
* underlying protocol, such as a TCP error, or the socket is closed.
417417
* @deprecated The network interface may not be uniquely identified by
418418
* the InetAddress returned.
419419
* Use {@link #getNetworkInterface()} instead.
@@ -434,7 +434,7 @@ public InetAddress getInterface() throws SocketException {
434434
*
435435
* @param netIf the interface
436436
* @throws SocketException if there is an error in
437-
* the underlying protocol, such as a TCP error.
437+
* the underlying protocol, such as a TCP error, or the socket is closed.
438438
* @see #getNetworkInterface()
439439
* @see StandardSocketOptions#IP_MULTICAST_IF
440440
* @since 1.4
@@ -454,7 +454,7 @@ public void setNetworkInterface(NetworkInterface netIf)
454454
* getOption(StandardSocketOptions.IP_MULTICAST_IF)}.
455455
*
456456
* @throws SocketException if there is an error in
457-
* the underlying protocol, such as a TCP error.
457+
* the underlying protocol, such as a TCP error, or the socket is closed.
458458
* @return The multicast {@code NetworkInterface} currently set. A placeholder
459459
* NetworkInterface is returned when there is no interface set; it has
460460
* a single InetAddress to represent any local address.
@@ -476,7 +476,8 @@ public NetworkInterface getNetworkInterface() throws SocketException {
476476
* verify what loopback mode is set to should call
477477
* {@link #getLoopbackMode()}
478478
* @param disable {@code true} to disable the LoopbackMode
479-
* @throws SocketException if an error occurs while setting the value
479+
* @throws SocketException if an error occurs while setting the value, or
480+
* the socket is closed.
480481
* @since 1.4
481482
* @deprecated Use {@link #setOption(SocketOption, Object)} with
482483
* {@link java.net.StandardSocketOptions#IP_MULTICAST_LOOP}
@@ -493,7 +494,8 @@ public void setLoopbackMode(boolean disable) throws SocketException {
493494
/**
494495
* Get the setting for local loopback of multicast datagrams.
495496
*
496-
* @throws SocketException if an error occurs while getting the value
497+
* @throws SocketException if an error occurs while getting the value, or
498+
* the socket is closed.
497499
* @return true if the LoopbackMode has been disabled
498500
* @since 1.4
499501
* @deprecated Use {@link #getOption(SocketOption)} with
@@ -534,8 +536,7 @@ public boolean getLoopbackMode() throws SocketException {
534536
* @param ttl optional time to live for multicast packet.
535537
* default ttl is 1.
536538
*
537-
* @throws IOException is raised if an error occurs i.e
538-
* error while setting ttl.
539+
* @throws IOException if an I/O error occurs, or the socket is closed.
539540
* @throws SecurityException if a security manager exists and its
540541
* {@code checkMulticast} or {@code checkConnect}
541542
* method doesn't allow the send.

0 commit comments

Comments
 (0)