< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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.Set;
  33 import java.util.Collections;
  34 
  35 /**
  36  * This class represents a socket for sending and receiving datagram packets.
  37  *
  38  * <p>A datagram socket is the sending or receiving point for a packet
  39  * delivery service. Each packet sent or received on a datagram socket
  40  * is individually addressed and routed. Multiple packets sent from
  41  * one machine to another may be routed differently, and may arrive in
  42  * any order.
  43  *
  44  * <p> Where possible, a newly constructed {@code DatagramSocket} has the
  45  * {@link SocketOptions#SO_BROADCAST SO_BROADCAST} socket option enabled so as
  46  * to allow the transmission of broadcast datagrams. In order to receive
  47  * broadcast packets a DatagramSocket should be bound to the wildcard address.
  48  * In some implementations, broadcast packets may also be received when
  49  * a DatagramSocket is bound to a more specific address.
  50  * <p>
  51  * Example:


1326      *         does not support the option.
1327      *
1328      * @throws IllegalArgumentException if the value is not valid for
1329      *         the option.
1330      *
1331      * @throws IOException if an I/O error occurs, or if the socket is closed.
1332      *
1333      * @throws SecurityException if a security manager is set and if the socket
1334      *         option requires a security permission and if the caller does
1335      *         not have the required permission.
1336      *         {@link java.net.StandardSocketOptions StandardSocketOptions}
1337      *         do not require any security permission.
1338      *
1339      * @throws NullPointerException if name is {@code null}
1340      *
1341      * @since 9
1342      */
1343     public <T> DatagramSocket setOption(SocketOption<T> name, T value)
1344         throws IOException
1345     {



1346         getImpl().setOption(name, value);
1347         return this;
1348     }
1349 
1350     /**
1351      * Returns the value of a socket option.
1352      *
1353      * @param <T> The type of the socket option value
1354      * @param name The socket option
1355      *
1356      * @return The value of the socket option.
1357      *
1358      * @throws UnsupportedOperationException if the datagram socket
1359      *         does not support the option.
1360      *
1361      * @throws IOException if an I/O error occurs, or if the socket is closed.
1362      *
1363      * @throws NullPointerException if name is {@code null}
1364      *
1365      * @throws SecurityException if a security manager is set and if the socket
1366      *         option requires a security permission and if the caller does
1367      *         not have the required permission.
1368      *         {@link java.net.StandardSocketOptions StandardSocketOptions}
1369      *         do not require any security permission.
1370      *
1371      * @since 9
1372      */
1373     public <T> T getOption(SocketOption<T> name) throws IOException {



1374         return getImpl().getOption(name);
1375     }
1376 
1377     private static Set<SocketOption<?>> options;
1378     private static boolean optionsSet = false;
1379 
1380     /**
1381      * Returns a set of the socket options supported by this socket.
1382      *
1383      * This method will continue to return the set of options even after
1384      * the socket has been closed.
1385      *
1386      * @return A set of the socket options supported by this socket. This set
1387      *        may be empty if the socket's DatagramSocketImpl cannot be created.
1388      *
1389      * @since 9
1390      */
1391     public Set<SocketOption<?>> supportedOptions() {
1392         synchronized(DatagramSocket.class) {
1393             if (optionsSet) {
   1 /*
   2  * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   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 SocketOptions#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.
  49  * In some implementations, broadcast packets may also be received when
  50  * a DatagramSocket is bound to a more specific address.
  51  * <p>
  52  * Example:


1327      *         does not support the option.
1328      *
1329      * @throws IllegalArgumentException if the value is not valid for
1330      *         the option.
1331      *
1332      * @throws IOException if an I/O error occurs, or if the socket is closed.
1333      *
1334      * @throws SecurityException if a security manager is set and if the socket
1335      *         option requires a security permission and if the caller does
1336      *         not have the required permission.
1337      *         {@link java.net.StandardSocketOptions StandardSocketOptions}
1338      *         do not require any security permission.
1339      *
1340      * @throws NullPointerException if name is {@code null}
1341      *
1342      * @since 9
1343      */
1344     public <T> DatagramSocket setOption(SocketOption<T> name, T value)
1345         throws IOException
1346     {
1347         Objects.requireNonNull(name);
1348         if (isClosed())
1349             throw new SocketException("Socket is closed");
1350         getImpl().setOption(name, value);
1351         return this;
1352     }
1353 
1354     /**
1355      * Returns the value of a socket option.
1356      *
1357      * @param <T> The type of the socket option value
1358      * @param name The socket option
1359      *
1360      * @return The value of the socket option.
1361      *
1362      * @throws UnsupportedOperationException if the datagram socket
1363      *         does not support the option.
1364      *
1365      * @throws IOException if an I/O error occurs, or if the socket is closed.
1366      *
1367      * @throws NullPointerException if name is {@code null}
1368      *
1369      * @throws SecurityException if a security manager is set and if the socket
1370      *         option requires a security permission and if the caller does
1371      *         not have the required permission.
1372      *         {@link java.net.StandardSocketOptions StandardSocketOptions}
1373      *         do not require any security permission.
1374      *
1375      * @since 9
1376      */
1377     public <T> T getOption(SocketOption<T> name) throws IOException {
1378         Objects.requireNonNull(name);
1379         if (isClosed())
1380             throw new SocketException("Socket is closed");
1381         return getImpl().getOption(name);
1382     }
1383 
1384     private static Set<SocketOption<?>> options;
1385     private static boolean optionsSet = false;
1386 
1387     /**
1388      * Returns a set of the socket options supported by this socket.
1389      *
1390      * This method will continue to return the set of options even after
1391      * the socket has been closed.
1392      *
1393      * @return A set of the socket options supported by this socket. This set
1394      *        may be empty if the socket's DatagramSocketImpl cannot be created.
1395      *
1396      * @since 9
1397      */
1398     public Set<SocketOption<?>> supportedOptions() {
1399         synchronized(DatagramSocket.class) {
1400             if (optionsSet) {
< prev index next >