jdk/src/share/classes/java/net/ServerSocket.java
changeset 18212 22f8c33b0690
parent 14342 8435a30053c1
child 18251 3743160a4cb8
equal deleted inserted replaced
18211:74aeb4741e3d 18212:22f8c33b0690
   388      * Returns the local address of this server socket.
   388      * Returns the local address of this server socket.
   389      * <p>
   389      * <p>
   390      * If the socket was bound prior to being {@link #close closed},
   390      * If the socket was bound prior to being {@link #close closed},
   391      * then this method will continue to return the local address
   391      * then this method will continue to return the local address
   392      * after the socket is closed.
   392      * after the socket is closed.
       
   393      * <p>
       
   394      * If there is a security manager set, its {@code checkConnect} method is
       
   395      * called with the local address and {@code -1} as its arguments to see
       
   396      * if the operation is allowed. If the operation is not allowed,
       
   397      * the {@link InetAddress#getLoopbackAddress loopback} address is returned.
   393      *
   398      *
   394      * @return  the address to which this socket is bound,
   399      * @return  the address to which this socket is bound,
   395      *          or <code>null</code> if the socket is unbound.
   400      *          or the loopback address if denied by the security manager,
       
   401      *          or {@code null} if the socket is unbound.
       
   402      *
       
   403      * @see SecurityManager#checkConnect
   396      */
   404      */
   397     public InetAddress getInetAddress() {
   405     public InetAddress getInetAddress() {
   398         if (!isBound())
   406         if (!isBound())
   399             return null;
   407             return null;
   400         try {
   408         try {
   401             return getImpl().getInetAddress();
   409             InetAddress in = getImpl().getInetAddress();
       
   410             SecurityManager sm = System.getSecurityManager();
       
   411             if (sm != null)
       
   412                 sm.checkConnect(in.getHostAddress(), -1);
       
   413             return in;
       
   414         } catch (SecurityException e) {
       
   415             return InetAddress.getLoopbackAddress();
   402         } catch (SocketException e) {
   416         } catch (SocketException e) {
   403             // nothing
   417             // nothing
   404             // If we're bound, the impl has been created
   418             // If we're bound, the impl has been created
   405             // so we shouldn't get here
   419             // so we shouldn't get here
   406         }
   420         }
   429         }
   443         }
   430         return -1;
   444         return -1;
   431     }
   445     }
   432 
   446 
   433     /**
   447     /**
   434      * Returns the address of the endpoint this socket is bound to, or
   448      * Returns the address of the endpoint this socket is bound to.
   435      * <code>null</code> if it is not bound yet.
       
   436      * <p>
   449      * <p>
   437      * If the socket was bound prior to being {@link #close closed},
   450      * If the socket was bound prior to being {@link #close closed},
   438      * then this method will continue to return the address of the endpoint
   451      * then this method will continue to return the address of the endpoint
   439      * after the socket is closed.
   452      * after the socket is closed.
   440      *
   453      * <p>
   441      * @return a <code>SocketAddress</code> representing the local endpoint of this
   454      * If there is a security manager set, its {@code checkConnect} method is
   442      *         socket, or <code>null</code> if it is not bound yet.
   455      * called with the local address and {@code -1} as its arguments to see
       
   456      * if the operation is allowed. If the operation is not allowed,
       
   457      * a {@code SocketAddress} representing the
       
   458      * {@link InetAddress#getLoopbackAddress loopback} address and the local
       
   459      * port to which the socket is bound is returned.
       
   460      *
       
   461      * @return a {@code SocketAddress} representing the local endpoint of
       
   462      *         this socket, or a {@code SocketAddress} representing the
       
   463      *         loopback address if denied by the security manager,
       
   464      *         or {@code null} if the socket is not bound yet.
       
   465      *
   443      * @see #getInetAddress()
   466      * @see #getInetAddress()
   444      * @see #getLocalPort()
   467      * @see #getLocalPort()
   445      * @see #bind(SocketAddress)
   468      * @see #bind(SocketAddress)
       
   469      * @see SecurityManager#checkConnect
   446      * @since 1.4
   470      * @since 1.4
   447      */
   471      */
   448 
   472 
   449     public SocketAddress getLocalSocketAddress() {
   473     public SocketAddress getLocalSocketAddress() {
   450         if (!isBound())
   474         if (!isBound())
   706     }
   730     }
   707 
   731 
   708     /**
   732     /**
   709      * Returns the implementation address and implementation port of
   733      * Returns the implementation address and implementation port of
   710      * this socket as a <code>String</code>.
   734      * this socket as a <code>String</code>.
       
   735      * <p>
       
   736      * If there is a security manager set, its {@code checkConnect} method is
       
   737      * called with the local address and {@code -1} as its arguments to see
       
   738      * if the operation is allowed. If the operation is not allowed,
       
   739      * an {@code InetAddress} representing the
       
   740      * {@link InetAddress#getLoopbackAddress loopback} address is returned as
       
   741      * the implementation address.
   711      *
   742      *
   712      * @return  a string representation of this socket.
   743      * @return  a string representation of this socket.
   713      */
   744      */
   714     public String toString() {
   745     public String toString() {
   715         if (!isBound())
   746         if (!isBound())
   716             return "ServerSocket[unbound]";
   747             return "ServerSocket[unbound]";
   717         return "ServerSocket[addr=" + impl.getInetAddress() +
   748         InetAddress in;
       
   749         if (System.getSecurityManager() != null)
       
   750             in = InetAddress.getLoopbackAddress();
       
   751         else
       
   752             in = impl.getInetAddress();
       
   753         return "ServerSocket[addr=" + in +
   718                 ",localport=" + impl.getLocalPort()  + "]";
   754                 ",localport=" + impl.getLocalPort()  + "]";
   719     }
   755     }
   720 
   756 
   721     void setBound() {
   757     void setBound() {
   722         bound = true;
   758         bound = true;