< prev index next >

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

Print this page




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.IOException;

  29 import java.nio.channels.DatagramChannel;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedExceptionAction;
  32 import java.util.Objects;
  33 import java.util.Set;
  34 import java.util.Collections;
  35 
  36 /**
  37  * This class represents a socket for sending and receiving datagram packets.
  38  *
  39  * <p>A datagram socket is the sending or receiving point for a packet
  40  * delivery service. Each packet sent or received on a datagram socket
  41  * is individually addressed and routed. Multiple packets sent from
  42  * one machine to another may be routed differently, and may arrive in
  43  * any order.
  44  *
  45  * <p> Where possible, a newly constructed {@code DatagramSocket} has the
  46  * {@link StandardSocketOptions#SO_BROADCAST SO_BROADCAST} socket option enabled so as
  47  * to allow the transmission of broadcast datagrams. In order to receive
  48  * broadcast packets a DatagramSocket should be bound to the wildcard address.


 481      * is not shared with untrusted code. When a socket is connected,
 482      * {@link #receive receive} and {@link #send send} <b>will not perform
 483      * any security checks</b> on incoming and outgoing packets, other than
 484      * matching the packet's and the socket's address and port. On a send
 485      * operation, if the packet's address is set and the packet's address
 486      * and the socket's address do not match, an {@code IllegalArgumentException}
 487      * will be thrown. A socket connected to a multicast address may only
 488      * be used to send packets.
 489      *
 490      * @param address the remote address for the socket
 491      *
 492      * @param port the remote port for the socket.
 493      *
 494      * @throws IllegalArgumentException
 495      *         if the address is null, or the port is out of range.
 496      *
 497      * @throws SecurityException
 498      *         if a security manager has been installed and it does
 499      *         not permit access to the given remote address
 500      *




 501      * @see #disconnect
 502      */
 503     public void connect(InetAddress address, int port) {
 504         try {
 505             connectInternal(address, port);
 506         } catch (SocketException se) {
 507             throw new Error("connect failed", se);
 508         }
 509     }
 510 
 511     /**
 512      * Connects this socket to a remote socket address (IP address + port number).
 513      *
 514      * <p> If given an {@link InetSocketAddress InetSocketAddress}, this method
 515      * behaves as if invoking {@link #connect(InetAddress,int) connect(InetAddress,int)}
 516      * with the given socket addresses IP address and port number.
 517      *
 518      * @param   addr    The remote address.
 519      *
 520      * @throws  SocketException
 521      *          if the connect fails
 522      *
 523      * @throws IllegalArgumentException
 524      *         if {@code addr} is {@code null}, or {@code addr} is a SocketAddress
 525      *         subclass not supported by this socket
 526      *
 527      * @throws SecurityException
 528      *         if a security manager has been installed and it does
 529      *         not permit access to the given remote address
 530      *
 531      * @since 1.4
 532      */
 533     public void connect(SocketAddress addr) throws SocketException {
 534         if (addr == null)
 535             throw new IllegalArgumentException("Address can't be null");
 536         if (!(addr instanceof InetSocketAddress))
 537             throw new IllegalArgumentException("Unsupported address type");
 538         InetSocketAddress epoint = (InetSocketAddress) addr;
 539         if (epoint.isUnresolved())
 540             throw new SocketException("Unresolved address");
 541         connectInternal(epoint.getAddress(), epoint.getPort());
 542     }
 543 
 544     /**
 545      * Disconnects the socket. If the socket is closed or not connected,
 546      * then this method has no effect.
 547      *








 548      * @see #connect
 549      */
 550     public void disconnect() {
 551         synchronized (this) {
 552             if (isClosed())
 553                 return;
 554             if (connectState == ST_CONNECTED) {
 555                 impl.disconnect ();
 556             }
 557             connectedAddress = null;
 558             connectedPort = -1;
 559             connectState = ST_NOT_CONNECTED;
 560             explicitFilter = false;
 561         }
 562     }
 563 
 564     /**
 565      * Returns the binding state of the socket.
 566      * <p>
 567      * If the socket was bound prior to being {@link #close closed},




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.net;
  27 
  28 import java.io.IOException;
  29 import java.io.UncheckedIOException;
  30 import java.nio.channels.DatagramChannel;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedExceptionAction;
  33 import java.util.Objects;
  34 import java.util.Set;
  35 import java.util.Collections;
  36 
  37 /**
  38  * This class represents a socket for sending and receiving datagram packets.
  39  *
  40  * <p>A datagram socket is the sending or receiving point for a packet
  41  * delivery service. Each packet sent or received on a datagram socket
  42  * is individually addressed and routed. Multiple packets sent from
  43  * one machine to another may be routed differently, and may arrive in
  44  * any order.
  45  *
  46  * <p> Where possible, a newly constructed {@code DatagramSocket} has the
  47  * {@link StandardSocketOptions#SO_BROADCAST SO_BROADCAST} socket option enabled so as
  48  * to allow the transmission of broadcast datagrams. In order to receive
  49  * broadcast packets a DatagramSocket should be bound to the wildcard address.


 482      * is not shared with untrusted code. When a socket is connected,
 483      * {@link #receive receive} and {@link #send send} <b>will not perform
 484      * any security checks</b> on incoming and outgoing packets, other than
 485      * matching the packet's and the socket's address and port. On a send
 486      * operation, if the packet's address is set and the packet's address
 487      * and the socket's address do not match, an {@code IllegalArgumentException}
 488      * will be thrown. A socket connected to a multicast address may only
 489      * be used to send packets.
 490      *
 491      * @param address the remote address for the socket
 492      *
 493      * @param port the remote port for the socket.
 494      *
 495      * @throws IllegalArgumentException
 496      *         if the address is null, or the port is out of range.
 497      *
 498      * @throws SecurityException
 499      *         if a security manager has been installed and it does
 500      *         not permit access to the given remote address
 501      *
 502      * @throws UncheckedIOException
 503      *         May be thrown by an implementation if connect fails,
 504      *         for example, if the destination address is non-routable
 505      *
 506      * @see #disconnect
 507      */
 508     public void connect(InetAddress address, int port) {
 509         try {
 510             connectInternal(address, port);
 511         } catch (SocketException se) {
 512             throw new UncheckedIOException("connect failed", se);
 513         }
 514     }
 515 
 516     /**
 517      * Connects this socket to a remote socket address (IP address + port number).
 518      *
 519      * <p> If given an {@link InetSocketAddress InetSocketAddress}, this method
 520      * behaves as if invoking {@link #connect(InetAddress,int) connect(InetAddress,int)}
 521      * with the given socket addresses IP address and port number.
 522      *
 523      * @param   addr    The remote address.
 524      *
 525      * @throws  SocketException
 526      *          if the connect fails
 527      *
 528      * @throws IllegalArgumentException
 529      *         if {@code addr} is {@code null}, or {@code addr} is a SocketAddress
 530      *         subclass not supported by this socket
 531      *
 532      * @throws SecurityException
 533      *         if a security manager has been installed and it does
 534      *         not permit access to the given remote address
 535      *
 536      * @since 1.4
 537      */
 538     public void connect(SocketAddress addr) throws SocketException {
 539         if (addr == null)
 540             throw new IllegalArgumentException("Address can't be null");
 541         if (!(addr instanceof InetSocketAddress))
 542             throw new IllegalArgumentException("Unsupported address type");
 543         InetSocketAddress epoint = (InetSocketAddress) addr;
 544         if (epoint.isUnresolved())
 545             throw new SocketException("Unresolved address");
 546         connectInternal(epoint.getAddress(), epoint.getPort());
 547     }
 548 
 549     /**
 550      * Disconnects the socket. If the socket is closed or not connected,
 551      * then this method has no effect.
 552      *
 553      * @apiNote If this method throws an UncheckedIOException, the socket
 554      * may be left in an unspecified state. It is strongly recommended that
 555      * the socket be closed when disconnect fails.
 556      *
 557      * @throws  UncheckedIOException
 558      *          May be thrown by an implementation if it fails to dissolve the
 559      *          association and restore the socket to a consistent state.
 560      *
 561      * @see #connect
 562      */
 563     public void disconnect() {
 564         synchronized (this) {
 565             if (isClosed())
 566                 return;
 567             if (connectState == ST_CONNECTED) {
 568                 impl.disconnect ();
 569             }
 570             connectedAddress = null;
 571             connectedPort = -1;
 572             connectState = ST_NOT_CONNECTED;
 573             explicitFilter = false;
 574         }
 575     }
 576 
 577     /**
 578      * Returns the binding state of the socket.
 579      * <p>
 580      * If the socket was bound prior to being {@link #close closed},


< prev index next >