src/java.base/share/classes/java/net/DatagramSocket.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 55081 dd321e3596c0
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
   186      * <p>If there is a security manager,
   186      * <p>If there is a security manager,
   187      * its {@code checkListen} method is first called
   187      * its {@code checkListen} method is first called
   188      * with 0 as its argument to ensure the operation is allowed.
   188      * with 0 as its argument to ensure the operation is allowed.
   189      * This could result in a SecurityException.
   189      * This could result in a SecurityException.
   190      *
   190      *
   191      * @exception  SocketException  if the socket could not be opened,
   191      * @throws     SocketException  if the socket could not be opened,
   192      *               or the socket could not bind to the specified local port.
   192      *               or the socket could not bind to the specified local port.
   193      * @exception  SecurityException  if a security manager exists and its
   193      * @throws     SecurityException  if a security manager exists and its
   194      *             {@code checkListen} method doesn't allow the operation.
   194      *             {@code checkListen} method doesn't allow the operation.
   195      *
   195      *
   196      * @see SecurityManager#checkListen
   196      * @see SecurityManager#checkListen
   197      */
   197      */
   198     public DatagramSocket() throws SocketException {
   198     public DatagramSocket() throws SocketException {
   227      * This could result in a SecurityException.
   227      * This could result in a SecurityException.
   228      *
   228      *
   229      * @param bindaddr local socket address to bind, or {@code null}
   229      * @param bindaddr local socket address to bind, or {@code null}
   230      *                 for an unbound socket.
   230      *                 for an unbound socket.
   231      *
   231      *
   232      * @exception  SocketException  if the socket could not be opened,
   232      * @throws     SocketException  if the socket could not be opened,
   233      *               or the socket could not bind to the specified local port.
   233      *               or the socket could not bind to the specified local port.
   234      * @exception  SecurityException  if a security manager exists and its
   234      * @throws     SecurityException  if a security manager exists and its
   235      *             {@code checkListen} method doesn't allow the operation.
   235      *             {@code checkListen} method doesn't allow the operation.
   236      *
   236      *
   237      * @see SecurityManager#checkListen
   237      * @see SecurityManager#checkListen
   238      * @since   1.4
   238      * @since   1.4
   239      */
   239      */
   261      * with the {@code port} argument
   261      * with the {@code port} argument
   262      * as its argument to ensure the operation is allowed.
   262      * as its argument to ensure the operation is allowed.
   263      * This could result in a SecurityException.
   263      * This could result in a SecurityException.
   264      *
   264      *
   265      * @param      port port to use.
   265      * @param      port port to use.
   266      * @exception  SocketException  if the socket could not be opened,
   266      * @throws     SocketException  if the socket could not be opened,
   267      *               or the socket could not bind to the specified local port.
   267      *               or the socket could not bind to the specified local port.
   268      * @exception  SecurityException  if a security manager exists and its
   268      * @throws     SecurityException  if a security manager exists and its
   269      *             {@code checkListen} method doesn't allow the operation.
   269      *             {@code checkListen} method doesn't allow the operation.
   270      *
   270      *
   271      * @see SecurityManager#checkListen
   271      * @see SecurityManager#checkListen
   272      */
   272      */
   273     public DatagramSocket(int port) throws SocketException {
   273     public DatagramSocket(int port) throws SocketException {
   288      * This could result in a SecurityException.
   288      * This could result in a SecurityException.
   289      *
   289      *
   290      * @param port local port to use
   290      * @param port local port to use
   291      * @param laddr local address to bind
   291      * @param laddr local address to bind
   292      *
   292      *
   293      * @exception  SocketException  if the socket could not be opened,
   293      * @throws     SocketException  if the socket could not be opened,
   294      *               or the socket could not bind to the specified local port.
   294      *               or the socket could not bind to the specified local port.
   295      * @exception  SecurityException  if a security manager exists and its
   295      * @throws     SecurityException  if a security manager exists and its
   296      *             {@code checkListen} method doesn't allow the operation.
   296      *             {@code checkListen} method doesn't allow the operation.
   297      *
   297      *
   298      * @see SecurityManager#checkListen
   298      * @see SecurityManager#checkListen
   299      * @since   1.1
   299      * @since   1.1
   300      */
   300      */
   432      * and {@link java.lang.SecurityManager#checkAccept checkAccept} methods
   432      * and {@link java.lang.SecurityManager#checkAccept checkAccept} methods
   433      * are invoked, with the given {@code address} and {@code port}, to
   433      * are invoked, with the given {@code address} and {@code port}, to
   434      * verify that datagrams are permitted to be sent and received
   434      * verify that datagrams are permitted to be sent and received
   435      * respectively.
   435      * respectively.
   436      *
   436      *
   437      * <p> When a socket is connected, {@link #receive receive} and
   437      * <p> Care should be taken to ensure that a connected datagram socket
   438      * {@link #send send} <b>will not perform any security checks</b>
   438      * is not shared with untrusted code. When a socket is connected,
   439      * on incoming and outgoing packets, other than matching the packet's
   439      * {@link #receive receive} and {@link #send send} <b>will not perform
   440      * and the socket's address and port. On a send operation, if the
   440      * any security checks</b> on incoming and outgoing packets, other than
   441      * packet's address is set and the packet's address and the socket's
   441      * matching the packet's and the socket's address and port. On a send
   442      * address do not match, an {@code IllegalArgumentException} will be
   442      * operation, if the packet's address is set and the packet's address
   443      * thrown. A socket connected to a multicast address may only be used
   443      * and the socket's address do not match, an {@code IllegalArgumentException}
   444      * to send packets.
   444      * will be thrown. A socket connected to a multicast address may only
       
   445      * be used to send packets.
   445      *
   446      *
   446      * @param address the remote address for the socket
   447      * @param address the remote address for the socket
   447      *
   448      *
   448      * @param port the remote port for the socket.
   449      * @param port the remote port for the socket.
   449      *
   450      *
   633      * {@code p.getPort()}. Each call to a security manager method
   634      * {@code p.getPort()}. Each call to a security manager method
   634      * could result in a SecurityException if the operation is not allowed.
   635      * could result in a SecurityException if the operation is not allowed.
   635      *
   636      *
   636      * @param      p   the {@code DatagramPacket} to be sent.
   637      * @param      p   the {@code DatagramPacket} to be sent.
   637      *
   638      *
   638      * @exception  IOException  if an I/O error occurs.
   639      * @throws     IOException  if an I/O error occurs.
   639      * @exception  SecurityException  if a security manager exists and its
   640      * @throws     SecurityException  if a security manager exists and its
   640      *             {@code checkMulticast} or {@code checkConnect}
   641      *             {@code checkMulticast} or {@code checkConnect}
   641      *             method doesn't allow the send.
   642      *             method doesn't allow the send.
   642      * @exception  PortUnreachableException may be thrown if the socket is connected
   643      * @throws     PortUnreachableException may be thrown if the socket is connected
   643      *             to a currently unreachable destination. Note, there is no
   644      *             to a currently unreachable destination. Note, there is no
   644      *             guarantee that the exception will be thrown.
   645      *             guarantee that the exception will be thrown.
   645      * @exception  java.nio.channels.IllegalBlockingModeException
   646      * @throws     java.nio.channels.IllegalBlockingModeException
   646      *             if this socket has an associated channel,
   647      *             if this socket has an associated channel,
   647      *             and the channel is in non-blocking mode.
   648      *             and the channel is in non-blocking mode.
   648      * @exception  IllegalArgumentException if the socket is connected,
   649      * @throws     IllegalArgumentException if the socket is connected,
   649      *             and connected address and packet address differ.
   650      *             and connected address and packet address differ.
   650      *
   651      *
   651      * @see        java.net.DatagramPacket
   652      * @see        java.net.DatagramPacket
   652      * @see        SecurityManager#checkMulticast(InetAddress)
   653      * @see        SecurityManager#checkMulticast(InetAddress)
   653      * @see        SecurityManager#checkConnect
   654      * @see        SecurityManager#checkConnect
   706      * This method blocks until a datagram is received. The
   707      * This method blocks until a datagram is received. The
   707      * {@code length} field of the datagram packet object contains
   708      * {@code length} field of the datagram packet object contains
   708      * the length of the received message. If the message is longer than
   709      * the length of the received message. If the message is longer than
   709      * the packet's length, the message is truncated.
   710      * the packet's length, the message is truncated.
   710      * <p>
   711      * <p>
   711      * If there is a security manager, a packet cannot be received if the
   712      * If there is a security manager, and the socket is not currently
   712      * security manager's {@code checkAccept} method
   713      * connected to a remote address, a packet cannot be received if the
   713      * does not allow it.
   714      * security manager's {@code checkAccept} method does not allow it.
       
   715      * Datagrams that are not permitted by the security manager are silently
       
   716      * discarded.
   714      *
   717      *
   715      * @param      p   the {@code DatagramPacket} into which to place
   718      * @param      p   the {@code DatagramPacket} into which to place
   716      *                 the incoming data.
   719      *                 the incoming data.
   717      * @exception  IOException  if an I/O error occurs.
   720      * @throws     IOException  if an I/O error occurs.
   718      * @exception  SocketTimeoutException  if setSoTimeout was previously called
   721      * @throws     SocketTimeoutException  if setSoTimeout was previously called
   719      *                 and the timeout has expired.
   722      *                 and the timeout has expired.
   720      * @exception  PortUnreachableException may be thrown if the socket is connected
   723      * @throws     PortUnreachableException may be thrown if the socket is connected
   721      *             to a currently unreachable destination. Note, there is no guarantee that the
   724      *             to a currently unreachable destination. Note, there is no guarantee that the
   722      *             exception will be thrown.
   725      *             exception will be thrown.
   723      * @exception  java.nio.channels.IllegalBlockingModeException
   726      * @throws     java.nio.channels.IllegalBlockingModeException
   724      *             if this socket has an associated channel,
   727      *             if this socket has an associated channel,
   725      *             and the channel is in non-blocking mode.
   728      *             and the channel is in non-blocking mode.
   726      * @see        java.net.DatagramPacket
   729      * @see        java.net.DatagramPacket
   727      * @see        java.net.DatagramSocket
   730      * @see        java.net.DatagramSocket
   728      * @revised 1.4
   731      * @revised 1.4
   894      *  timeout must be {@code > 0}.
   897      *  timeout must be {@code > 0}.
   895      *  A timeout of zero is interpreted as an infinite timeout.
   898      *  A timeout of zero is interpreted as an infinite timeout.
   896      *
   899      *
   897      * @param timeout the specified timeout in milliseconds.
   900      * @param timeout the specified timeout in milliseconds.
   898      * @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
   899      * @since   1.1
   903      * @since   1.1
   900      * @see #getSoTimeout()
   904      * @see #getSoTimeout()
   901      */
   905      */
   902     public synchronized void setSoTimeout(int timeout) throws SocketException {
   906     public synchronized void setSoTimeout(int timeout) throws SocketException {
   903         if (isClosed())
   907         if (isClosed())
   904             throw new SocketException("Socket is closed");
   908             throw new SocketException("Socket is closed");
       
   909         if (timeout < 0)
       
   910             throw new IllegalArgumentException("timeout < 0");
   905         getImpl().setOption(SocketOptions.SO_TIMEOUT, timeout);
   911         getImpl().setOption(SocketOptions.SO_TIMEOUT, timeout);
   906     }
   912     }
   907 
   913 
   908     /**
   914     /**
   909      * Retrieve setting for SO_TIMEOUT.  0 returns implies that the
   915      * Retrieve setting for SO_TIMEOUT.  0 returns implies that the
   949      * packet is sent or discarded.
   955      * packet is sent or discarded.
   950      *
   956      *
   951      * @param size the size to which to set the send buffer
   957      * @param size the size to which to set the send buffer
   952      * size. This value must be greater than 0.
   958      * size. This value must be greater than 0.
   953      *
   959      *
   954      * @exception SocketException if there is an error
   960      * @throws    SocketException if there is an error
   955      * in the underlying protocol, such as an UDP error.
   961      * in the underlying protocol, such as an UDP error.
   956      * @exception IllegalArgumentException if the value is 0 or is
   962      * @throws    IllegalArgumentException if the value is 0 or is
   957      * negative.
   963      * negative.
   958      * @see #getSendBufferSize()
   964      * @see #getSendBufferSize()
   959      */
   965      */
   960     public synchronized void setSendBufferSize(int size)
   966     public synchronized void setSendBufferSize(int size)
   961     throws SocketException{
   967     throws SocketException{
   970     /**
   976     /**
   971      * Get value of the SO_SNDBUF option for this {@code DatagramSocket}, that is the
   977      * Get value of the SO_SNDBUF option for this {@code DatagramSocket}, that is the
   972      * buffer size used by the platform for output on this {@code DatagramSocket}.
   978      * buffer size used by the platform for output on this {@code DatagramSocket}.
   973      *
   979      *
   974      * @return the value of the SO_SNDBUF option for this {@code DatagramSocket}
   980      * @return the value of the SO_SNDBUF option for this {@code DatagramSocket}
   975      * @exception SocketException if there is an error in
   981      * @throws    SocketException if there is an error in
   976      * the underlying protocol, such as an UDP error.
   982      * the underlying protocol, such as an UDP error.
   977      * @see #setSendBufferSize
   983      * @see #setSendBufferSize
   978      */
   984      */
   979     public synchronized int getSendBufferSize() throws SocketException {
   985     public synchronized int getSendBufferSize() throws SocketException {
   980         if (isClosed())
   986         if (isClosed())
  1007      * than SO_RCVBUF can be received.
  1013      * than SO_RCVBUF can be received.
  1008      *
  1014      *
  1009      * @param size the size to which to set the receive buffer
  1015      * @param size the size to which to set the receive buffer
  1010      * size. This value must be greater than 0.
  1016      * size. This value must be greater than 0.
  1011      *
  1017      *
  1012      * @exception SocketException if there is an error in
  1018      * @throws    SocketException if there is an error in
  1013      * the underlying protocol, such as an UDP error.
  1019      * the underlying protocol, such as an UDP error.
  1014      * @exception IllegalArgumentException if the value is 0 or is
  1020      * @throws    IllegalArgumentException if the value is 0 or is
  1015      * negative.
  1021      * negative.
  1016      * @see #getReceiveBufferSize()
  1022      * @see #getReceiveBufferSize()
  1017      */
  1023      */
  1018     public synchronized void setReceiveBufferSize(int size)
  1024     public synchronized void setReceiveBufferSize(int size)
  1019     throws SocketException{
  1025     throws SocketException{
  1028     /**
  1034     /**
  1029      * Get value of the SO_RCVBUF option for this {@code DatagramSocket}, that is the
  1035      * Get value of the SO_RCVBUF option for this {@code DatagramSocket}, that is the
  1030      * buffer size used by the platform for input on this {@code DatagramSocket}.
  1036      * buffer size used by the platform for input on this {@code DatagramSocket}.
  1031      *
  1037      *
  1032      * @return the value of the SO_RCVBUF option for this {@code DatagramSocket}
  1038      * @return the value of the SO_RCVBUF option for this {@code DatagramSocket}
  1033      * @exception SocketException if there is an error in the underlying protocol, such as an UDP error.
  1039      * @throws    SocketException if there is an error in the underlying protocol, such as an UDP error.
  1034      * @see #setReceiveBufferSize(int)
  1040      * @see #setReceiveBufferSize(int)
  1035      */
  1041      */
  1036     public synchronized int getReceiveBufferSize()
  1042     public synchronized int getReceiveBufferSize()
  1037     throws SocketException{
  1043     throws SocketException{
  1038         if (isClosed())
  1044         if (isClosed())
  1068      * The behaviour when {@code SO_REUSEADDR} is enabled or
  1074      * The behaviour when {@code SO_REUSEADDR} is enabled or
  1069      * disabled after a socket is bound (See {@link #isBound()})
  1075      * disabled after a socket is bound (See {@link #isBound()})
  1070      * is not defined.
  1076      * is not defined.
  1071      *
  1077      *
  1072      * @param on  whether to enable or disable the
  1078      * @param on  whether to enable or disable the
  1073      * @exception SocketException if an error occurs enabling or
  1079      * @throws    SocketException if an error occurs enabling or
  1074      *            disabling the {@code SO_REUSEADDR} socket option,
  1080      *            disabling the {@code SO_REUSEADDR} socket option,
  1075      *            or the socket is closed.
  1081      *            or the socket is closed.
  1076      * @since 1.4
  1082      * @since 1.4
  1077      * @see #getReuseAddress()
  1083      * @see #getReuseAddress()
  1078      * @see #bind(SocketAddress)
  1084      * @see #bind(SocketAddress)
  1091 
  1097 
  1092     /**
  1098     /**
  1093      * Tests if SO_REUSEADDR is enabled.
  1099      * Tests if SO_REUSEADDR is enabled.
  1094      *
  1100      *
  1095      * @return a {@code boolean} indicating whether or not SO_REUSEADDR is enabled.
  1101      * @return a {@code boolean} indicating whether or not SO_REUSEADDR is enabled.
  1096      * @exception SocketException if there is an error
  1102      * @throws    SocketException if there is an error
  1097      * in the underlying protocol, such as an UDP error.
  1103      * in the underlying protocol, such as an UDP error.
  1098      * @since   1.4
  1104      * @since   1.4
  1099      * @see #setReuseAddress(boolean)
  1105      * @see #setReuseAddress(boolean)
  1100      */
  1106      */
  1101     public synchronized boolean getReuseAddress() throws SocketException {
  1107     public synchronized boolean getReuseAddress() throws SocketException {
  1129     }
  1135     }
  1130 
  1136 
  1131     /**
  1137     /**
  1132      * Tests if SO_BROADCAST is enabled.
  1138      * Tests if SO_BROADCAST is enabled.
  1133      * @return a {@code boolean} indicating whether or not SO_BROADCAST is enabled.
  1139      * @return a {@code boolean} indicating whether or not SO_BROADCAST is enabled.
  1134      * @exception SocketException if there is an error
  1140      * @throws    SocketException if there is an error
  1135      * in the underlying protocol, such as an UDP error.
  1141      * in the underlying protocol, such as an UDP error.
  1136      * @since 1.4
  1142      * @since 1.4
  1137      * @see #setBroadcast(boolean)
  1143      * @see #setBroadcast(boolean)
  1138      */
  1144      */
  1139     public synchronized boolean getBroadcast() throws SocketException {
  1145     public synchronized boolean getBroadcast() throws SocketException {
  1288      * the security manager's {@code checkSetFactory} method
  1294      * the security manager's {@code checkSetFactory} method
  1289      * to ensure the operation is allowed.
  1295      * to ensure the operation is allowed.
  1290      * This could result in a SecurityException.
  1296      * This could result in a SecurityException.
  1291      *
  1297      *
  1292      * @param      fac   the desired factory.
  1298      * @param      fac   the desired factory.
  1293      * @exception  IOException  if an I/O error occurs when setting the
  1299      * @throws     IOException  if an I/O error occurs when setting the
  1294      *              datagram socket factory.
  1300      *              datagram socket factory.
  1295      * @exception  SocketException  if the factory is already defined.
  1301      * @throws     SocketException  if the factory is already defined.
  1296      * @exception  SecurityException  if a security manager exists and its
  1302      * @throws     SecurityException  if a security manager exists and its
  1297      *             {@code checkSetFactory} method doesn't allow the operation.
  1303      *             {@code checkSetFactory} method doesn't allow the operation.
  1298      * @see       java.net.DatagramSocketImplFactory#createDatagramSocketImpl()
  1304      * @see       java.net.DatagramSocketImplFactory#createDatagramSocketImpl()
  1299      * @see       SecurityManager#checkSetFactory
  1305      * @see       SecurityManager#checkSetFactory
  1300      * @since 1.3
  1306      * @since 1.3
  1301      */
  1307      */