< prev index next >

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

Print this page




1220         }
1221         return result;
1222     }
1223 
1224     /**
1225      * Sets the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option to the
1226      * specified value for this {@code Socket}. The
1227      * {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option is
1228      * used by the platform's networking code as a hint for the size to set
1229      * the underlying network I/O buffers.
1230      *
1231      * <p>Increasing the receive buffer size can increase the performance of
1232      * network I/O for high-volume connection, while decreasing it can
1233      * help reduce the backlog of incoming data.
1234      *
1235      * <p>Because {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is a hint,
1236      * applications that want to verify what size the buffers were set to
1237      * should call {@link #getReceiveBufferSize()}.
1238      *
1239      * <p>The value of {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is also used
1240      * to set the TCP receive window that is advertized to the remote peer.
1241      * Generally, the window size can be modified at any time when a socket is
1242      * connected. However, if a receive window larger than 64K is required then
1243      * this must be requested <B>before</B> the socket is connected to the
1244      * remote peer. There are two cases to be aware of:
1245      * <ol>
1246      * <li>For sockets accepted from a ServerSocket, this must be done by calling
1247      * {@link ServerSocket#setReceiveBufferSize(int)} before the ServerSocket
1248      * is bound to a local address.</li>
1249      * <li>For client sockets, setReceiveBufferSize() must be called before
1250      * connecting the socket to its remote peer.</li></ol>
1251      * @param size the size to which to set the receive buffer
1252      * size. This value must be greater than 0.
1253      *
1254      * @exception IllegalArgumentException if the value is 0 or is
1255      * negative.
1256      *
1257      * @exception SocketException if there is an error
1258      * in the underlying protocol, such as a TCP error.
1259      *
1260      * @see #getReceiveBufferSize()


1561      * Converts this socket to a {@code String}.
1562      *
1563      * @return  a string representation of this socket.
1564      */
1565     public String toString() {
1566         try {
1567             if (isConnected())
1568                 return "Socket[addr=" + getImpl().getInetAddress() +
1569                     ",port=" + getImpl().getPort() +
1570                     ",localport=" + getImpl().getLocalPort() + "]";
1571         } catch (SocketException e) {
1572         }
1573         return "Socket[unconnected]";
1574     }
1575 
1576     /**
1577      * Returns the connection state of the socket.
1578      * <p>
1579      * Note: Closing a socket doesn't clear its connection state, which means
1580      * this method will return {@code true} for a closed socket
1581      * (see {@link #isClosed()}) if it was successfuly connected prior
1582      * to being closed.
1583      *
1584      * @return true if the socket was successfuly connected to a server
1585      * @since 1.4
1586      */
1587     public boolean isConnected() {
1588         // Before 1.3 Sockets were always connected during creation
1589         return connected || oldImpl;
1590     }
1591 
1592     /**
1593      * Returns the binding state of the socket.
1594      * <p>
1595      * Note: Closing a socket doesn't clear its binding state, which means
1596      * this method will return {@code true} for a closed socket
1597      * (see {@link #isClosed()}) if it was successfuly bound prior
1598      * to being closed.
1599      *
1600      * @return true if the socket was successfuly bound to an address
1601      * @since 1.4
1602      * @see #bind
1603      */
1604     public boolean isBound() {
1605         // Before 1.3 Sockets were always bound during creation
1606         return bound || oldImpl;
1607     }
1608 
1609     /**
1610      * Returns the closed state of the socket.
1611      *
1612      * @return true if the socket has been closed
1613      * @since 1.4
1614      * @see #close
1615      */
1616     public boolean isClosed() {
1617         synchronized(closeLock) {
1618             return closed;
1619         }
1620     }




1220         }
1221         return result;
1222     }
1223 
1224     /**
1225      * Sets the {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option to the
1226      * specified value for this {@code Socket}. The
1227      * {@link SocketOptions#SO_RCVBUF SO_RCVBUF} option is
1228      * used by the platform's networking code as a hint for the size to set
1229      * the underlying network I/O buffers.
1230      *
1231      * <p>Increasing the receive buffer size can increase the performance of
1232      * network I/O for high-volume connection, while decreasing it can
1233      * help reduce the backlog of incoming data.
1234      *
1235      * <p>Because {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is a hint,
1236      * applications that want to verify what size the buffers were set to
1237      * should call {@link #getReceiveBufferSize()}.
1238      *
1239      * <p>The value of {@link SocketOptions#SO_RCVBUF SO_RCVBUF} is also used
1240      * to set the TCP receive window that is advertised to the remote peer.
1241      * Generally, the window size can be modified at any time when a socket is
1242      * connected. However, if a receive window larger than 64K is required then
1243      * this must be requested <B>before</B> the socket is connected to the
1244      * remote peer. There are two cases to be aware of:
1245      * <ol>
1246      * <li>For sockets accepted from a ServerSocket, this must be done by calling
1247      * {@link ServerSocket#setReceiveBufferSize(int)} before the ServerSocket
1248      * is bound to a local address.</li>
1249      * <li>For client sockets, setReceiveBufferSize() must be called before
1250      * connecting the socket to its remote peer.</li></ol>
1251      * @param size the size to which to set the receive buffer
1252      * size. This value must be greater than 0.
1253      *
1254      * @exception IllegalArgumentException if the value is 0 or is
1255      * negative.
1256      *
1257      * @exception SocketException if there is an error
1258      * in the underlying protocol, such as a TCP error.
1259      *
1260      * @see #getReceiveBufferSize()


1561      * Converts this socket to a {@code String}.
1562      *
1563      * @return  a string representation of this socket.
1564      */
1565     public String toString() {
1566         try {
1567             if (isConnected())
1568                 return "Socket[addr=" + getImpl().getInetAddress() +
1569                     ",port=" + getImpl().getPort() +
1570                     ",localport=" + getImpl().getLocalPort() + "]";
1571         } catch (SocketException e) {
1572         }
1573         return "Socket[unconnected]";
1574     }
1575 
1576     /**
1577      * Returns the connection state of the socket.
1578      * <p>
1579      * Note: Closing a socket doesn't clear its connection state, which means
1580      * this method will return {@code true} for a closed socket
1581      * (see {@link #isClosed()}) if it was successfully connected prior
1582      * to being closed.
1583      *
1584      * @return true if the socket was successfully connected to a server
1585      * @since 1.4
1586      */
1587     public boolean isConnected() {
1588         // Before 1.3 Sockets were always connected during creation
1589         return connected || oldImpl;
1590     }
1591 
1592     /**
1593      * Returns the binding state of the socket.
1594      * <p>
1595      * Note: Closing a socket doesn't clear its binding state, which means
1596      * this method will return {@code true} for a closed socket
1597      * (see {@link #isClosed()}) if it was successfully bound prior
1598      * to being closed.
1599      *
1600      * @return true if the socket was successfully bound to an address
1601      * @since 1.4
1602      * @see #bind
1603      */
1604     public boolean isBound() {
1605         // Before 1.3 Sockets were always bound during creation
1606         return bound || oldImpl;
1607     }
1608 
1609     /**
1610      * Returns the closed state of the socket.
1611      *
1612      * @return true if the socket has been closed
1613      * @since 1.4
1614      * @see #close
1615      */
1616     public boolean isClosed() {
1617         synchronized(closeLock) {
1618             return closed;
1619         }
1620     }


< prev index next >