Skip to content

Commit 04e8cb8

Browse files
committed
8336815: Several methods in java.net.Socket and ServerSocket do not specify behavior when already bound, connected or closed
Reviewed-by: alanb
1 parent b2599f8 commit 04e8cb8

File tree

4 files changed

+367
-64
lines changed

4 files changed

+367
-64
lines changed

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ private SocketImpl getImpl() throws SocketException {
328328
* an ephemeral port and a valid local address to bind the socket.
329329
*
330330
* @param endpoint The IP address and port number to bind to.
331-
* @throws IOException if the bind operation fails, or if the socket
332-
* is already bound.
331+
* @throws IOException if the bind operation fails, the socket
332+
* is already bound or the socket is closed.
333333
* @throws SecurityException if a {@code SecurityManager} is present and
334334
* its {@code checkListen} method doesn't allow the operation.
335335
* @throws IllegalArgumentException if endpoint is a
@@ -357,8 +357,8 @@ public void bind(SocketAddress endpoint) throws IOException {
357357
* @param endpoint The IP address and port number to bind to.
358358
* @param backlog requested maximum length of the queue of
359359
* incoming connections.
360-
* @throws IOException if the bind operation fails, or if the socket
361-
* is already bound.
360+
* @throws IOException if the bind operation fails, the socket
361+
* is already bound or the socket is closed.
362362
* @throws SecurityException if a {@code SecurityManager} is present and
363363
* its {@code checkListen} method doesn't allow the operation.
364364
* @throws IllegalArgumentException if endpoint is a
@@ -518,7 +518,7 @@ public SocketAddress getLocalSocketAddress() {
518518
* client socket implementation factory}, if one has been set.
519519
*
520520
* @throws IOException if an I/O error occurs when waiting for a
521-
* connection.
521+
* connection, the socket is not bound or the socket is closed.
522522
* @throws SecurityException if a security manager exists and its
523523
* {@code checkAccept} method doesn't allow the operation.
524524
* @throws SocketTimeoutException if a timeout was previously set with setSoTimeout and
@@ -736,6 +736,9 @@ private void ensureCompatible(SocketImpl si) throws IOException {
736736
* <p> If this socket has an associated channel then the channel is closed
737737
* as well.
738738
*
739+
* <p> Once closed, several of the methods defined by this class will throw
740+
* an exception if invoked on the closed socket.
741+
*
739742
* @throws IOException if an I/O error occurs when closing the socket.
740743
*/
741744
public void close() throws IOException {
@@ -806,8 +809,8 @@ public boolean isClosed() {
806809
* operation to have effect.
807810
*
808811
* @param timeout the specified timeout, in milliseconds
809-
* @throws SocketException if there is an error in the underlying protocol,
810-
* such as a TCP error
812+
* @throws SocketException if there is an error in the underlying protocol,
813+
* such as a TCP error, or the socket is closed.
811814
* @throws IllegalArgumentException if {@code timeout} is negative
812815
* @since 1.1
813816
* @see #getSoTimeout()
@@ -824,7 +827,7 @@ public void setSoTimeout(int timeout) throws SocketException {
824827
* Retrieve setting for {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}.
825828
* 0 returns implies that the option is disabled (i.e., timeout of infinity).
826829
* @return the {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT} value
827-
* @throws IOException if an I/O error occurs
830+
* @throws IOException if an I/O error occurs or the socket is closed.
828831
* @since 1.1
829832
* @see #setSoTimeout(int)
830833
*/
@@ -887,8 +890,8 @@ public void setReuseAddress(boolean on) throws SocketException {
887890
*
888891
* @return a {@code boolean} indicating whether or not
889892
* {@code SO_REUSEADDR} is enabled.
890-
* @throws SocketException if there is an error
891-
* in the underlying protocol, such as a TCP error.
893+
* @throws SocketException if there is an error in the underlying protocol, such as a TCP error,
894+
* or the socket is closed.
892895
* @since 1.4
893896
* @see #setReuseAddress(boolean)
894897
*/
@@ -1001,8 +1004,8 @@ public static synchronized void setSocketFactory(SocketImplFactory fac) throws I
10011004
* requested value but the TCP receive window in sockets accepted from
10021005
* this ServerSocket will be no larger than 64K bytes.
10031006
*
1004-
* @throws SocketException if there is an error
1005-
* in the underlying protocol, such as a TCP error.
1007+
* @throws SocketException if there is an error in the underlying protocol,
1008+
* such as a TCP error, or the socket is closed.
10061009
*
10071010
* @param size the size to which to set the receive buffer
10081011
* size. This value must be greater than 0.
@@ -1029,8 +1032,8 @@ public void setReceiveBufferSize(int size) throws SocketException {
10291032
* <p>Note, the value actually set in the accepted socket is determined by
10301033
* calling {@link Socket#getReceiveBufferSize()}.
10311034
* @return the value of the {@code SO_RCVBUF} option for this {@code Socket}.
1032-
* @throws SocketException if there is an error
1033-
* in the underlying protocol, such as a TCP error.
1035+
* @throws SocketException if there is an error in the underlying protocol,
1036+
* such as a TCP error, or the socket is closed.
10341037
* @see #setReceiveBufferSize(int)
10351038
* @since 1.4
10361039
*/

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

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,8 @@ void setConnected() {
683683
* </ol>
684684
*
685685
* @param endpoint the {@code SocketAddress}
686-
* @throws IOException if an error occurs during the connection
686+
* @throws IOException if an error occurs during the connection, the socket
687+
* is already connected or the socket is closed
687688
* @throws java.nio.channels.IllegalBlockingModeException
688689
* if this socket has an associated channel,
689690
* and the channel is in non-blocking mode
@@ -717,7 +718,8 @@ public void connect(SocketAddress endpoint) throws IOException {
717718
*
718719
* @param endpoint the {@code SocketAddress}
719720
* @param timeout the timeout value to be used in milliseconds.
720-
* @throws IOException if an error occurs during the connection
721+
* @throws IOException if an error occurs during the connection, the socket
722+
* is already connected or the socket is closed
721723
* @throws SocketTimeoutException if timeout expires before connecting
722724
* @throws java.nio.channels.IllegalBlockingModeException
723725
* if this socket has an associated channel,
@@ -780,8 +782,8 @@ public void connect(SocketAddress endpoint, int timeout) throws IOException {
780782
* an ephemeral port and a valid local address to bind the socket.
781783
*
782784
* @param bindpoint the {@code SocketAddress} to bind to
783-
* @throws IOException if the bind operation fails, or if the socket
784-
* is already bound.
785+
* @throws IOException if the bind operation fails, the socket
786+
* is already bound or the socket is closed.
785787
* @throws IllegalArgumentException if bindpoint is a
786788
* SocketAddress subclass not supported by this socket
787789
* @throws SecurityException if a security manager exists and its
@@ -1174,8 +1176,8 @@ public void close() throws IOException {
11741176
* will close the associated socket.
11751177
*
11761178
* @return an output stream for writing bytes to this socket.
1177-
* @throws IOException if an I/O error occurs when creating the
1178-
* output stream or if the socket is not connected.
1179+
* @throws IOException if an I/O error occurs when creating the
1180+
* output stream, the socket is not connected or the socket is closed.
11791181
*/
11801182
public OutputStream getOutputStream() throws IOException {
11811183
int s = state;
@@ -1251,8 +1253,8 @@ public void close() throws IOException {
12511253
* @param on {@code true} to enable {@code TCP_NODELAY},
12521254
* {@code false} to disable.
12531255
*
1254-
* @throws SocketException if there is an error
1255-
* in the underlying protocol, such as a TCP error.
1256+
* @throws SocketException if there is an error in the underlying protocol,
1257+
* such as a TCP error, or the socket is closed.
12561258
*
12571259
* @since 1.1
12581260
*
@@ -1269,8 +1271,8 @@ public void setTcpNoDelay(boolean on) throws SocketException {
12691271
*
12701272
* @return a {@code boolean} indicating whether or not
12711273
* {@code TCP_NODELAY} is enabled.
1272-
* @throws SocketException if there is an error
1273-
* in the underlying protocol, such as a TCP error.
1274+
* @throws SocketException if there is an error in the underlying protocol,
1275+
* such as a TCP error, or the socket is closed.
12741276
* @since 1.1
12751277
* @see #setTcpNoDelay(boolean)
12761278
*/
@@ -1289,9 +1291,9 @@ public boolean getTcpNoDelay() throws SocketException {
12891291
*
12901292
* @param on whether or not to linger on.
12911293
* @param linger how long to linger for, if on is true.
1292-
* @throws SocketException if there is an error
1293-
* in the underlying protocol, such as a TCP error.
1294-
* @throws IllegalArgumentException if the linger value is negative.
1294+
* @throws SocketException if there is an error in the underlying protocol,
1295+
* such as a TCP error, or the socket is closed.
1296+
* @throws IllegalArgumentException if the linger value is negative.
12951297
* @since 1.1
12961298
* @see #getSoLinger()
12971299
*/
@@ -1318,8 +1320,8 @@ public void setSoLinger(boolean on, int linger) throws SocketException {
13181320
* The setting only affects socket close.
13191321
*
13201322
* @return the setting for {@code SO_LINGER}.
1321-
* @throws SocketException if there is an error
1322-
* in the underlying protocol, such as a TCP error.
1323+
* @throws SocketException if there is an error in the underlying protocol,
1324+
* such as a TCP error, or the socket is closed.
13231325
* @since 1.1
13241326
* @see #setSoLinger(boolean, int)
13251327
*/
@@ -1368,8 +1370,8 @@ public void sendUrgentData(int data) throws IOException {
13681370
* @param on {@code true} to enable {@code SO_OOBINLINE},
13691371
* {@code false} to disable.
13701372
*
1371-
* @throws SocketException if there is an error
1372-
* in the underlying protocol, such as a TCP error.
1373+
* @throws SocketException if there is an error in the underlying protocol,
1374+
* such as a TCP error, or the socket is closed.
13731375
*
13741376
* @since 1.4
13751377
*
@@ -1387,8 +1389,8 @@ public void setOOBInline(boolean on) throws SocketException {
13871389
* @return a {@code boolean} indicating whether or not
13881390
* {@code SO_OOBINLINE} is enabled.
13891391
*
1390-
* @throws SocketException if there is an error
1391-
* in the underlying protocol, such as a TCP error.
1392+
* @throws SocketException if there is an error in the underlying protocol,
1393+
* such as a TCP error, or the socket is closed.
13921394
* @since 1.4
13931395
* @see #setOOBInline(boolean)
13941396
*/
@@ -1409,8 +1411,8 @@ public boolean getOOBInline() throws SocketException {
14091411
* to have effect.
14101412
*
14111413
* @param timeout the specified timeout, in milliseconds.
1412-
* @throws SocketException if there is an error in the underlying protocol,
1413-
* such as a TCP error
1414+
* @throws SocketException if there is an error in the underlying protocol,
1415+
* such as a TCP error, or the socket is closed.
14141416
* @throws IllegalArgumentException if {@code timeout} is negative
14151417
* @since 1.1
14161418
* @see #getSoTimeout()
@@ -1428,8 +1430,8 @@ public void setSoTimeout(int timeout) throws SocketException {
14281430
* 0 returns implies that the option is disabled (i.e., timeout of infinity).
14291431
*
14301432
* @return the setting for {@code SO_TIMEOUT}
1431-
* @throws SocketException if there is an error
1432-
* in the underlying protocol, such as a TCP error.
1433+
* @throws SocketException if there is an error in the underlying protocol,
1434+
* such as a TCP error, or the socket is closed.
14331435
*
14341436
* @since 1.1
14351437
* @see #setSoTimeout(int)
@@ -1455,14 +1457,12 @@ public int getSoTimeout() throws SocketException {
14551457
* <p>Because {@code SO_SNDBUF} is a hint, applications that want to verify
14561458
* what size the buffers were set to should call {@link #getSendBufferSize()}.
14571459
*
1458-
* @throws SocketException if there is an error
1459-
* in the underlying protocol, such as a TCP error.
1460-
*
14611460
* @param size the size to which to set the send buffer
14621461
* size. This value must be greater than 0.
14631462
*
1464-
* @throws IllegalArgumentException if the
1465-
* value is 0 or is negative.
1463+
* @throws SocketException if there is an error in the underlying protocol,
1464+
* such as a TCP error, or the socket is closed.
1465+
* @throws IllegalArgumentException if the value is 0 or is negative.
14661466
*
14671467
* @see #getSendBufferSize()
14681468
* @since 1.2
@@ -1481,8 +1481,8 @@ public void setSendBufferSize(int size) throws SocketException {
14811481
* for output on this {@code Socket}.
14821482
* @return the value of the {@code SO_SNDBUF} option for this {@code Socket}.
14831483
*
1484-
* @throws SocketException if there is an error
1485-
* in the underlying protocol, such as a TCP error.
1484+
* @throws SocketException if there is an error in the underlying protocol,
1485+
* such as a TCP error, or the socket is closed.
14861486
*
14871487
* @see #setSendBufferSize(int)
14881488
* @since 1.2
@@ -1529,8 +1529,8 @@ public int getSendBufferSize() throws SocketException {
15291529
* @throws IllegalArgumentException if the value is 0 or is
15301530
* negative.
15311531
*
1532-
* @throws SocketException if there is an error
1533-
* in the underlying protocol, such as a TCP error.
1532+
* @throws SocketException if there is an error in the underlying protocol,
1533+
* such as a TCP error, or the socket is closed.
15341534
*
15351535
* @see #getReceiveBufferSize()
15361536
* @see ServerSocket#setReceiveBufferSize(int)
@@ -1550,8 +1550,8 @@ public void setReceiveBufferSize(int size) throws SocketException {
15501550
* for input on this {@code Socket}.
15511551
*
15521552
* @return the value of the {@code SO_RCVBUF} option for this {@code Socket}.
1553-
* @throws SocketException if there is an error
1554-
* in the underlying protocol, such as a TCP error.
1553+
* @throws SocketException if there is an error in the underlying protocol,
1554+
* such as a TCP error, or the socket is closed.
15551555
* @see #setReceiveBufferSize(int)
15561556
* @since 1.2
15571557
*/
@@ -1570,8 +1570,8 @@ public int getReceiveBufferSize() throws SocketException {
15701570
* Enable/disable {@link StandardSocketOptions#SO_KEEPALIVE SO_KEEPALIVE}.
15711571
*
15721572
* @param on whether or not to have socket keep alive turned on.
1573-
* @throws SocketException if there is an error
1574-
* in the underlying protocol, such as a TCP error.
1573+
* @throws SocketException if there is an error in the underlying protocol,
1574+
* such as a TCP error, or the socket is closed.
15751575
* @since 1.3
15761576
* @see #getKeepAlive()
15771577
*/
@@ -1586,8 +1586,8 @@ public void setKeepAlive(boolean on) throws SocketException {
15861586
*
15871587
* @return a {@code boolean} indicating whether or not
15881588
* {@code SO_KEEPALIVE} is enabled.
1589-
* @throws SocketException if there is an error
1590-
* in the underlying protocol, such as a TCP error.
1589+
* @throws SocketException if there is an error in the underlying protocol,
1590+
* such as a TCP error, or the socket is closed.
15911591
* @since 1.3
15921592
* @see #setKeepAlive(boolean)
15931593
*/
@@ -1637,8 +1637,8 @@ public boolean getKeepAlive() throws SocketException {
16371637
* would be placed into the sin6_flowinfo field of the IP header.
16381638
*
16391639
* @param tc an {@code int} value for the bitset.
1640-
* @throws SocketException if there is an error setting the
1641-
* traffic class or type-of-service
1640+
* @throws SocketException if there is an error setting the traffic class or type-of-service,
1641+
* or the socket is closed.
16421642
* @since 1.4
16431643
* @see #getTrafficClass
16441644
* @see StandardSocketOptions#IP_TOS
@@ -1661,8 +1661,8 @@ public void setTrafficClass(int tc) throws SocketException {
16611661
* set using the {@link #setTrafficClass(int)} method on this Socket.
16621662
*
16631663
* @return the traffic class or type-of-service already set
1664-
* @throws SocketException if there is an error obtaining the
1665-
* traffic class or type-of-service value.
1664+
* @throws SocketException if there is an error obtaining the traffic class
1665+
* or type-of-service value, or the socket is closed.
16661666
* @since 1.4
16671667
* @see #setTrafficClass(int)
16681668
* @see StandardSocketOptions#IP_TOS
@@ -1715,8 +1715,8 @@ public void setReuseAddress(boolean on) throws SocketException {
17151715
*
17161716
* @return a {@code boolean} indicating whether or not
17171717
* {@code SO_REUSEADDR} is enabled.
1718-
* @throws SocketException if there is an error
1719-
* in the underlying protocol, such as a TCP error.
1718+
* @throws SocketException if there is an error in the underlying protocol,
1719+
* such as a TCP error, or the socket is closed.
17201720
* @since 1.4
17211721
* @see #setReuseAddress(boolean)
17221722
*/
@@ -1733,8 +1733,9 @@ public boolean getReuseAddress() throws SocketException {
17331733
* will throw a {@link SocketException}.
17341734
* <p>
17351735
* Once a socket has been closed, it is not available for further networking
1736-
* use (i.e. can't be reconnected or rebound). A new socket needs to be
1737-
* created.
1736+
* use (i.e. can't be reconnected or rebound) and several of the methods defined
1737+
* by this class will throw an exception if invoked on the closed socket. A new
1738+
* socket needs to be created.
17381739
*
17391740
* <p> Closing this socket will also close the socket's
17401741
* {@link java.io.InputStream InputStream} and
@@ -1767,8 +1768,8 @@ public void close() throws IOException {
17671768
* socket, the stream's {@code available} method will return 0, and its
17681769
* {@code read} methods will return {@code -1} (end of stream).
17691770
*
1770-
* @throws IOException if an I/O error occurs when shutting down this
1771-
* socket.
1771+
* @throws IOException if an I/O error occurs when shutting down this socket, the
1772+
* socket is not connected or the socket is closed.
17721773
*
17731774
* @since 1.3
17741775
* @see java.net.Socket#shutdownOutput()
@@ -1797,8 +1798,8 @@ public void shutdownInput() throws IOException {
17971798
* shutdownOutput() on the socket, the stream will throw
17981799
* an IOException.
17991800
*
1800-
* @throws IOException if an I/O error occurs when shutting down this
1801-
* socket.
1801+
* @throws IOException if an I/O error occurs when shutting down this socket, the socket
1802+
* is not connected or the socket is closed.
18021803
*
18031804
* @since 1.3
18041805
* @see java.net.Socket#shutdownInput()

0 commit comments

Comments
 (0)