287 * This could result in a SecurityException.
288 *
289 * @param port local port to use
290 * @param laddr local address to bind
291 *
292 * @exception SocketException if the socket could not be opened,
293 * or the socket could not bind to the specified local port.
294 * @exception SecurityException if a security manager exists and its
295 * {@code checkListen} method doesn't allow the operation.
296 *
297 * @see SecurityManager#checkListen
298 * @since 1.1
299 */
300 public DatagramSocket(int port, InetAddress laddr) throws SocketException {
301 this(new InetSocketAddress(laddr, port));
302 }
303
304 private void checkOldImpl() {
305 if (impl == null)
306 return;
307 // DatagramSocketImpl.peekdata() is a protected method, therefore we need to use
308 // getDeclaredMethod, therefore we need permission to access the member
309 try {
310 AccessController.doPrivileged(
311 new PrivilegedExceptionAction<>() {
312 public Void run() throws NoSuchMethodException {
313 Class<?>[] cl = new Class<?>[1];
314 cl[0] = DatagramPacket.class;
315 impl.getClass().getDeclaredMethod("peekData", cl);
316 return null;
317 }
318 });
319 } catch (java.security.PrivilegedActionException e) {
320 oldImpl = true;
321 }
322 }
323
324 static Class<?> implClass = null;
325
326 void createImpl() throws SocketException {
327 if (impl == null) {
643 * guarantee that the exception will be thrown.
644 * @exception java.nio.channels.IllegalBlockingModeException
645 * if this socket has an associated channel,
646 * and the channel is in non-blocking mode.
647 * @exception IllegalArgumentException if the socket is connected,
648 * and connected address and packet address differ.
649 *
650 * @see java.net.DatagramPacket
651 * @see SecurityManager#checkMulticast(InetAddress)
652 * @see SecurityManager#checkConnect
653 * @revised 1.4
654 * @spec JSR-51
655 */
656 public void send(DatagramPacket p) throws IOException {
657 InetAddress packetAddress = null;
658 synchronized (p) {
659 if (isClosed())
660 throw new SocketException("Socket is closed");
661 checkAddress (p.getAddress(), "send");
662 if (connectState == ST_NOT_CONNECTED) {
663 // check the address is ok wiht the security manager on every send.
664 SecurityManager security = System.getSecurityManager();
665
666 // The reason you want to synchronize on datagram packet
667 // is because you don't want an applet to change the address
668 // while you are trying to send the packet for example
669 // after the security check but before the send.
670 if (security != null) {
671 if (p.getAddress().isMulticastAddress()) {
672 security.checkMulticast(p.getAddress());
673 } else {
674 security.checkConnect(p.getAddress().getHostAddress(),
675 p.getPort());
676 }
677 }
678 } else {
679 // we're connected
680 packetAddress = p.getAddress();
681 if (packetAddress == null) {
682 p.setAddress(connectedAddress);
683 p.setPort(connectedPort);
1053 * (See {@link java.net.MulticastSocket}). The
1054 * {@code SO_REUSEADDR} socket option allows multiple
1055 * sockets to be bound to the same socket address if the
1056 * {@code SO_REUSEADDR} socket option is enabled prior
1057 * to binding the socket using {@link #bind(SocketAddress)}.
1058 * <p>
1059 * Note: This functionality is not supported by all existing platforms,
1060 * so it is implementation specific whether this option will be ignored
1061 * or not. However, if it is not supported then
1062 * {@link #getReuseAddress()} will always return {@code false}.
1063 * <p>
1064 * When a {@code DatagramSocket} is created the initial setting
1065 * of {@code SO_REUSEADDR} is disabled.
1066 * <p>
1067 * The behaviour when {@code SO_REUSEADDR} is enabled or
1068 * disabled after a socket is bound (See {@link #isBound()})
1069 * is not defined.
1070 *
1071 * @param on whether to enable or disable the
1072 * @exception SocketException if an error occurs enabling or
1073 * disabling the {@code SO_RESUEADDR} socket option,
1074 * or the socket is closed.
1075 * @since 1.4
1076 * @see #getReuseAddress()
1077 * @see #bind(SocketAddress)
1078 * @see #isBound()
1079 * @see #isClosed()
1080 */
1081 public synchronized void setReuseAddress(boolean on) throws SocketException {
1082 if (isClosed())
1083 throw new SocketException("Socket is closed");
1084 // Integer instead of Boolean for compatibility with older DatagramSocketImpl
1085 if (oldImpl)
1086 getImpl().setOption(SocketOptions.SO_REUSEADDR, on?-1:0);
1087 else
1088 getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
1089 }
1090
1091 /**
1092 * Tests if SO_REUSEADDR is enabled.
1093 *
|
287 * This could result in a SecurityException.
288 *
289 * @param port local port to use
290 * @param laddr local address to bind
291 *
292 * @exception SocketException if the socket could not be opened,
293 * or the socket could not bind to the specified local port.
294 * @exception SecurityException if a security manager exists and its
295 * {@code checkListen} method doesn't allow the operation.
296 *
297 * @see SecurityManager#checkListen
298 * @since 1.1
299 */
300 public DatagramSocket(int port, InetAddress laddr) throws SocketException {
301 this(new InetSocketAddress(laddr, port));
302 }
303
304 private void checkOldImpl() {
305 if (impl == null)
306 return;
307 // DatagramSocketImpl.peekData() is a protected method, therefore we need to use
308 // getDeclaredMethod, therefore we need permission to access the member
309 try {
310 AccessController.doPrivileged(
311 new PrivilegedExceptionAction<>() {
312 public Void run() throws NoSuchMethodException {
313 Class<?>[] cl = new Class<?>[1];
314 cl[0] = DatagramPacket.class;
315 impl.getClass().getDeclaredMethod("peekData", cl);
316 return null;
317 }
318 });
319 } catch (java.security.PrivilegedActionException e) {
320 oldImpl = true;
321 }
322 }
323
324 static Class<?> implClass = null;
325
326 void createImpl() throws SocketException {
327 if (impl == null) {
643 * guarantee that the exception will be thrown.
644 * @exception java.nio.channels.IllegalBlockingModeException
645 * if this socket has an associated channel,
646 * and the channel is in non-blocking mode.
647 * @exception IllegalArgumentException if the socket is connected,
648 * and connected address and packet address differ.
649 *
650 * @see java.net.DatagramPacket
651 * @see SecurityManager#checkMulticast(InetAddress)
652 * @see SecurityManager#checkConnect
653 * @revised 1.4
654 * @spec JSR-51
655 */
656 public void send(DatagramPacket p) throws IOException {
657 InetAddress packetAddress = null;
658 synchronized (p) {
659 if (isClosed())
660 throw new SocketException("Socket is closed");
661 checkAddress (p.getAddress(), "send");
662 if (connectState == ST_NOT_CONNECTED) {
663 // check the address is ok with the security manager on every send.
664 SecurityManager security = System.getSecurityManager();
665
666 // The reason you want to synchronize on datagram packet
667 // is because you don't want an applet to change the address
668 // while you are trying to send the packet for example
669 // after the security check but before the send.
670 if (security != null) {
671 if (p.getAddress().isMulticastAddress()) {
672 security.checkMulticast(p.getAddress());
673 } else {
674 security.checkConnect(p.getAddress().getHostAddress(),
675 p.getPort());
676 }
677 }
678 } else {
679 // we're connected
680 packetAddress = p.getAddress();
681 if (packetAddress == null) {
682 p.setAddress(connectedAddress);
683 p.setPort(connectedPort);
1053 * (See {@link java.net.MulticastSocket}). The
1054 * {@code SO_REUSEADDR} socket option allows multiple
1055 * sockets to be bound to the same socket address if the
1056 * {@code SO_REUSEADDR} socket option is enabled prior
1057 * to binding the socket using {@link #bind(SocketAddress)}.
1058 * <p>
1059 * Note: This functionality is not supported by all existing platforms,
1060 * so it is implementation specific whether this option will be ignored
1061 * or not. However, if it is not supported then
1062 * {@link #getReuseAddress()} will always return {@code false}.
1063 * <p>
1064 * When a {@code DatagramSocket} is created the initial setting
1065 * of {@code SO_REUSEADDR} is disabled.
1066 * <p>
1067 * The behaviour when {@code SO_REUSEADDR} is enabled or
1068 * disabled after a socket is bound (See {@link #isBound()})
1069 * is not defined.
1070 *
1071 * @param on whether to enable or disable the
1072 * @exception SocketException if an error occurs enabling or
1073 * disabling the {@code SO_REUSEADDR} socket option,
1074 * or the socket is closed.
1075 * @since 1.4
1076 * @see #getReuseAddress()
1077 * @see #bind(SocketAddress)
1078 * @see #isBound()
1079 * @see #isClosed()
1080 */
1081 public synchronized void setReuseAddress(boolean on) throws SocketException {
1082 if (isClosed())
1083 throw new SocketException("Socket is closed");
1084 // Integer instead of Boolean for compatibility with older DatagramSocketImpl
1085 if (oldImpl)
1086 getImpl().setOption(SocketOptions.SO_REUSEADDR, on?-1:0);
1087 else
1088 getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
1089 }
1090
1091 /**
1092 * Tests if SO_REUSEADDR is enabled.
1093 *
|