src/java.base/share/classes/java/net/DatagramSocket.java
changeset 58899 5573a7098439
parent 58692 04946947ca79
child 59124 d01fe40e9cd8
equal deleted inserted replaced
58898:4ec9fc2b2f0d 58899:5573a7098439
   604      * @see #getLocalAddress()
   604      * @see #getLocalAddress()
   605      * @see #getLocalPort()
   605      * @see #getLocalPort()
   606      * @see #bind(SocketAddress)
   606      * @see #bind(SocketAddress)
   607      * @since 1.4
   607      * @since 1.4
   608      */
   608      */
   609 
       
   610     public SocketAddress getLocalSocketAddress() {
   609     public SocketAddress getLocalSocketAddress() {
   611         if (isClosed())
   610         if (isClosed())
   612             return null;
   611             return null;
   613         if (!isBound())
   612         if (!isBound())
   614             return null;
   613             return null;
   851      * @since   1.1
   850      * @since   1.1
   852      */
   851      */
   853     public InetAddress getLocalAddress() {
   852     public InetAddress getLocalAddress() {
   854         if (isClosed())
   853         if (isClosed())
   855             return null;
   854             return null;
   856         InetAddress in = null;
   855         InetAddress in;
   857         try {
   856         try {
   858             in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
   857             in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
   859             if (in.isAnyLocalAddress()) {
   858             if (in.isAnyLocalAddress()) {
   860                 in = InetAddress.anyLocalAddress();
   859                 in = InetAddress.anyLocalAddress();
   861             }
   860             }
   872     /**
   871     /**
   873      * Returns the port number on the local host to which this socket
   872      * Returns the port number on the local host to which this socket
   874      * is bound.
   873      * is bound.
   875      *
   874      *
   876      * @return  the port number on the local host to which this socket is bound,
   875      * @return  the port number on the local host to which this socket is bound,
   877                 {@code -1} if the socket is closed, or
   876      *          {@code -1} if the socket is closed, or
   878                 {@code 0} if it is not bound yet.
   877      *          {@code 0} if it is not bound yet.
   879      */
   878      */
   880     public int getLocalPort() {
   879     public int getLocalPort() {
   881         if (isClosed())
   880         if (isClosed())
   882             return -1;
   881             return -1;
   883         try {
   882         try {
   885         } catch (Exception e) {
   884         } catch (Exception e) {
   886             return 0;
   885             return 0;
   887         }
   886         }
   888     }
   887     }
   889 
   888 
   890     /** Enable/disable SO_TIMEOUT with the specified timeout, in
   889     /**
   891      *  milliseconds. With this option set to a positive timeout value,
   890      * Enable/disable SO_TIMEOUT with the specified timeout, in
   892      *  a call to receive() for this DatagramSocket
   891      * milliseconds. With this option set to a positive timeout value,
   893      *  will block for only this amount of time.  If the timeout expires,
   892      * a call to receive() for this DatagramSocket
   894      *  a <B>java.net.SocketTimeoutException</B> is raised, though the
   893      * will block for only this amount of time.  If the timeout expires,
   895      *  DatagramSocket is still valid. A timeout of zero is interpreted
   894      * a <B>java.net.SocketTimeoutException</B> is raised, though the
   896      *  as an infinite timeout.
   895      * DatagramSocket is still valid. A timeout of zero is interpreted
   897      *  The option <B>must</B> be enabled prior to entering the blocking
   896      * as an infinite timeout.
   898      *  operation to have effect.
   897      * The option <B>must</B> be enabled prior to entering the blocking
       
   898      * operation to have effect.
   899      *
   899      *
   900      * @param timeout the specified timeout in milliseconds.
   900      * @param timeout the specified timeout in milliseconds.
   901      * @throws SocketException if there is an error in the underlying protocol, such as an UDP error.
   901      * @throws SocketException if there is an error in the underlying protocol, such as an UDP error.
   902      * @throws IllegalArgumentException if {@code timeout} is negative
   902      * @throws IllegalArgumentException if {@code timeout} is negative
   903      * @since   1.1
   903      * @since   1.1
   961      * in the underlying protocol, such as an UDP error.
   961      * in the underlying protocol, such as an UDP error.
   962      * @throws    IllegalArgumentException if the value is 0 or is
   962      * @throws    IllegalArgumentException if the value is 0 or is
   963      * negative.
   963      * negative.
   964      * @see #getSendBufferSize()
   964      * @see #getSendBufferSize()
   965      */
   965      */
   966     public synchronized void setSendBufferSize(int size)
   966     public synchronized void setSendBufferSize(int size) throws SocketException {
   967     throws SocketException{
       
   968         if (!(size > 0)) {
   967         if (!(size > 0)) {
   969             throw new IllegalArgumentException("negative send size");
   968             throw new IllegalArgumentException("negative send size");
   970         }
   969         }
   971         if (isClosed())
   970         if (isClosed())
   972             throw new SocketException("Socket is closed");
   971             throw new SocketException("Socket is closed");
  1019      * the underlying protocol, such as an UDP error.
  1018      * the underlying protocol, such as an UDP error.
  1020      * @throws    IllegalArgumentException if the value is 0 or is
  1019      * @throws    IllegalArgumentException if the value is 0 or is
  1021      * negative.
  1020      * negative.
  1022      * @see #getReceiveBufferSize()
  1021      * @see #getReceiveBufferSize()
  1023      */
  1022      */
  1024     public synchronized void setReceiveBufferSize(int size)
  1023     public synchronized void setReceiveBufferSize(int size) throws SocketException {
  1025     throws SocketException{
       
  1026         if (size <= 0) {
  1024         if (size <= 0) {
  1027             throw new IllegalArgumentException("invalid receive size");
  1025             throw new IllegalArgumentException("invalid receive size");
  1028         }
  1026         }
  1029         if (isClosed())
  1027         if (isClosed())
  1030             throw new SocketException("Socket is closed");
  1028             throw new SocketException("Socket is closed");
  1037      *
  1035      *
  1038      * @return the value of the SO_RCVBUF option for this {@code DatagramSocket}
  1036      * @return the value of the SO_RCVBUF option for this {@code DatagramSocket}
  1039      * @throws    SocketException if there is an error in the underlying protocol, such as an UDP error.
  1037      * @throws    SocketException if there is an error in the underlying protocol, such as an UDP error.
  1040      * @see #setReceiveBufferSize(int)
  1038      * @see #setReceiveBufferSize(int)
  1041      */
  1039      */
  1042     public synchronized int getReceiveBufferSize()
  1040     public synchronized int getReceiveBufferSize() throws SocketException {
  1043     throws SocketException{
       
  1044         if (isClosed())
  1041         if (isClosed())
  1045             throw new SocketException("Socket is closed");
  1042             throw new SocketException("Socket is closed");
  1046         int result = 0;
  1043         int result = 0;
  1047         Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
  1044         Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
  1048         if (o instanceof Integer) {
  1045         if (o instanceof Integer) {