jdk/src/share/classes/java/net/DatagramSocket.java
author michaelm
Wed, 02 Dec 2009 12:17:42 +0000
changeset 5180 8161f879d704
parent 1817 2deb4cc4d5dc
child 5465 950789cfc3c5
permissions -rw-r--r--
6893954: Subclasses of InetAddress may incorrectly interpret network addresses Summary: runtime type checks and deserialization check Reviewed-by: chegar, alanb, jccollet
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1995-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InterruptedIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.DatagramChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This class represents a socket for sending and receiving datagram packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>A datagram socket is the sending or receiving point for a packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * delivery service. Each packet sent or received on a datagram socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * is individually addressed and routed. Multiple packets sent from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * one machine to another may be routed differently, and may arrive in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * any order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
1817
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
    44
 * <p> Where possible, a newly constructed {@code DatagramSocket} has the
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
    45
 * {@link SocketOptions#SO_BROADCAST SO_BROADCAST} socket option enabled so as
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
    46
 * to allow the transmission of broadcast datagrams. In order to receive
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
    47
 * broadcast packets a DatagramSocket should be bound to the wildcard address.
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
    48
 * In some implementations, broadcast packets may also be received when
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a DatagramSocket is bound to a more specific address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *              DatagramSocket s = new DatagramSocket(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *              s.bind(new InetSocketAddress(8888));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Which is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *              DatagramSocket s = new DatagramSocket(8888);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Both cases will create a DatagramSocket able to receive broadcasts on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * UDP port 8888.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author  Pavani Diwanji
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @see     java.net.DatagramPacket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @see     java.nio.channels.DatagramChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @since JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
class DatagramSocket implements java.io.Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Various states of this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private boolean created = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private boolean bound = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private Object closeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * The implementation of this DatagramSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    DatagramSocketImpl impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Are we using an older DatagramSocketImpl?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    boolean oldImpl = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Connection state:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * ST_NOT_CONNECTED = socket not connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * ST_CONNECTED = socket connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * ST_CONNECTED_NO_IMPL = socket connected but not at impl level
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    static final int ST_NOT_CONNECTED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static final int ST_CONNECTED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static final int ST_CONNECTED_NO_IMPL = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    int connectState = ST_NOT_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Connected address & port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    InetAddress connectedAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    int connectedPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Connects this socket to a remote socket address (IP address + port number).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Binds socket if not already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param   addr    The remote address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param   port    The remote port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @throws  SocketException if binding the socket fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private synchronized void connectInternal(InetAddress address, int port) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (port < 0 || port > 0xFFFF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new IllegalArgumentException("connect: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (address == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new IllegalArgumentException("connect: null address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   121
        checkAddress (address, "connect");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (address.isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                security.checkMulticast(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                security.checkConnect(address.getHostAddress(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                security.checkAccept(address.getHostAddress(), port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
          bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // old impls do not support connect/disconnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (oldImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            connectState = ST_CONNECTED_NO_IMPL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                getImpl().connect(address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                // socket is now connected by the impl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                connectState = ST_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            } catch (SocketException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                // connection will be emulated by DatagramSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                connectState = ST_CONNECTED_NO_IMPL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        connectedAddress = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        connectedPort = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Constructs a datagram socket and binds it to any available port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * on the local host machine.  The socket will be bound to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * {@link InetAddress#isAnyLocalAddress wildcard} address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * an IP address chosen by the kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <p>If there is a security manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * its <code>checkListen</code> method is first called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * with 0 as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @exception  SocketException  if the socket could not be opened,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *               or the socket could not bind to the specified local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *             <code>checkListen</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public DatagramSocket() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // create a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        createImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } catch (SocketException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new SocketException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Creates an unbound datagram socket with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * DatagramSocketImpl.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param impl an instance of a <B>DatagramSocketImpl</B>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *        the subclass wishes to use on the DatagramSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    protected DatagramSocket(DatagramSocketImpl impl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        this.impl = impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        checkOldImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Creates a datagram socket, bound to the specified local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * socket address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * If, if the address is <code>null</code>, creates an unbound socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <p>If there is a security manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * its <code>checkListen</code> method is first called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * with the port from the socket address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param bindaddr local socket address to bind, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *                 for an unbound socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @exception  SocketException  if the socket could not be opened,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *               or the socket could not bind to the specified local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *             <code>checkListen</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public DatagramSocket(SocketAddress bindaddr) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // create a datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        createImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (bindaddr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            bind(bindaddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Constructs a datagram socket and binds it to the specified port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * on the local host machine.  The socket will be bound to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * {@link InetAddress#isAnyLocalAddress wildcard} address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * an IP address chosen by the kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <p>If there is a security manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * its <code>checkListen</code> method is first called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * with the <code>port</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param      port port to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @exception  SocketException  if the socket could not be opened,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *               or the socket could not bind to the specified local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *             <code>checkListen</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public DatagramSocket(int port) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        this(port, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Creates a datagram socket, bound to the specified local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * address.  The local port must be between 0 and 65535 inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * If the IP address is 0.0.0.0, the socket will be bound to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * {@link InetAddress#isAnyLocalAddress wildcard} address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * an IP address chosen by the kernel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * <p>If there is a security manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * its <code>checkListen</code> method is first called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * with the <code>port</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * as its argument to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param port local port to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param laddr local address to bind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception  SocketException  if the socket could not be opened,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *               or the socket could not bind to the specified local port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *             <code>checkListen</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @see SecurityManager#checkListen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public DatagramSocket(int port, InetAddress laddr) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        this(new InetSocketAddress(laddr, port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private void checkOldImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (impl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        // DatagramSocketImpl.peekdata() is a protected method, therefore we need to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // getDeclaredMethod, therefore we need permission to access the member
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   292
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   293
                new PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   294
                    public Void run() throws NoSuchMethodException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        Class[] cl = new Class[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        cl[0] = DatagramPacket.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        impl.getClass().getDeclaredMethod("peekData", cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } catch (java.security.PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            oldImpl = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    static Class implClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    void createImpl() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (impl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                impl = factory.createDatagramSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                checkOldImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                boolean isMulticast = (this instanceof MulticastSocket) ? true : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                impl = DefaultDatagramSocketImplFactory.createDatagramSocketImpl(isMulticast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                checkOldImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        // creates a udp socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        impl.create();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        created = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Get the <code>DatagramSocketImpl</code> attached to this socket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * creating it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @return  the <code>DatagramSocketImpl</code> attached to that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *          DatagramSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @throws SocketException if creation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    DatagramSocketImpl getImpl() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (!created)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            createImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Binds this DatagramSocket to a specific address & port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * If the address is <code>null</code>, then the system will pick up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * an ephemeral port and a valid local address to bind the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param   addr The address & port to bind to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @throws  SocketException if any error happens during the bind, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *          socket is already bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @throws  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *             <code>checkListen</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @throws IllegalArgumentException if addr is a SocketAddress subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *         not supported by this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public synchronized void bind(SocketAddress addr) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            throw new SocketException("already bound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (addr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            addr = new InetSocketAddress(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (!(addr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            throw new IllegalArgumentException("Unsupported address type!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        InetSocketAddress epoint = (InetSocketAddress) addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            throw new SocketException("Unresolved address");
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   367
        InetAddress iaddr = epoint.getAddress();
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   368
        int port = epoint.getPort();
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   369
        checkAddress(iaddr, "bind");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        SecurityManager sec = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (sec != null) {
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   372
            sec.checkListen(port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        try {
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   375
            getImpl().bind(port, iaddr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            getImpl().close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        bound = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   383
    void checkAddress (InetAddress addr, String op) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   384
        if (addr == null) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   385
            return;
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   386
        }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   387
        if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) {
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   388
            throw new IllegalArgumentException(op + ": invalid address type");
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   389
        }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   390
    }
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   391
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Connects the socket to a remote address for this socket. When a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * socket is connected to a remote address, packets may only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * sent to or received from that address. By default a datagram
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * socket is not connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <p>If the remote destination to which the socket is connected does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * exist, or is otherwise unreachable, and if an ICMP destination unreachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * packet has been received for that address, then a subsequent call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * send or receive may throw a PortUnreachableException. Note, there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * guarantee that the exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <p>A caller's permission to send and receive datagrams to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * given host and port are checked at connect time. When a socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * is connected, receive and send <b>will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * perform any security checks</b> on incoming and outgoing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * packets, other than matching the packet's and the socket's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * address and port. On a send operation, if the packet's address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * is set and the packet's address and the socket's address do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * match, an IllegalArgumentException will be thrown. A socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * connected to a multicast address may only be used to send packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @param address the remote address for the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param port the remote port for the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @exception IllegalArgumentException if the address is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * or the port is out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @exception SecurityException if the caller is not allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * send datagrams to and receive datagrams from the address and port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @see #disconnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @see #send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @see #receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public void connect(InetAddress address, int port) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            connectInternal(address, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        } catch (SocketException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            throw new Error("connect failed", se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Connects this socket to a remote socket address (IP address + port number).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param   addr    The remote address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @throws  SocketException if the connect fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @throws  IllegalArgumentException if addr is null or addr is a SocketAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *          subclass not supported by this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @see #connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public void connect(SocketAddress addr) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (addr == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throw new IllegalArgumentException("Address can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (!(addr instanceof InetSocketAddress))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            throw new IllegalArgumentException("Unsupported address type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        InetSocketAddress epoint = (InetSocketAddress) addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (epoint.isUnresolved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            throw new SocketException("Unresolved address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        connectInternal(epoint.getAddress(), epoint.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Disconnects the socket. If the socket is closed or not connected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * then this method has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @see #connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public void disconnect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            if (connectState == ST_CONNECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                impl.disconnect ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            connectedAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            connectedPort = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            connectState = ST_NOT_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Returns the binding state of the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * If the socket was bound prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * then this method will continue to return <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @return true if the socket successfully bound to an address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return bound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Returns the connection state of the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * then this method will continue to return <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @return true if the socket successfully connected to a server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    public boolean isConnected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return connectState != ST_NOT_CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Returns the address to which this socket is connected. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * <code>null</code> if the socket is not connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * then this method will continue to return the connected address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @return the address to which this socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return connectedAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Returns the port number to which this socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Returns <code>-1</code> if the socket is not connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * then this method will continue to return the connected port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @return the port number to which this socket is connected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        return connectedPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Returns the address of the endpoint this socket is connected to, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <code>null</code> if it is unconnected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * If the socket was connected prior to being {@link #close closed},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * then this method will continue to return the connected address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * after the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @return a <code>SocketAddress</code> representing the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *         endpoint of this socket, or <code>null</code> if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *         not connected yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @see #getInetAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @see #getPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @see #connect(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    public SocketAddress getRemoteSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (!isConnected())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return new InetSocketAddress(getInetAddress(), getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Returns the address of the endpoint this socket is bound to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @return a <code>SocketAddress</code> representing the local endpoint of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *         socket, or <code>null</code> if it is closed or not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @see #getLocalAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @see #getLocalPort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    public SocketAddress getLocalSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return new InetSocketAddress(getLocalAddress(), getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Sends a datagram packet from this socket. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <code>DatagramPacket</code> includes information indicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * data to be sent, its length, the IP address of the remote host,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * and the port number on the remote host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <p>If there is a security manager, and the socket is not currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * connected to a remote address, this method first performs some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * security checks. First, if <code>p.getAddress().isMulticastAddress()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * is true, this method calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * security manager's <code>checkMulticast</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * with <code>p.getAddress()</code> as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * If the evaluation of that expression is false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * this method instead calls the security manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * <code>checkConnect</code> method with arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * <code>p.getAddress().getHostAddress()</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * <code>p.getPort()</code>. Each call to a security manager method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * could result in a SecurityException if the operation is not allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param      p   the <code>DatagramPacket</code> to be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *             <code>checkMulticast</code> or <code>checkConnect</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *             method doesn't allow the send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @exception  PortUnreachableException may be thrown if the socket is connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *             to a currently unreachable destination. Note, there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *             guarantee that the exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @exception  java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *             if this socket has an associated channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *             and the channel is in non-blocking mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @exception  IllegalArgumentException if the socket is connected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *             and connected address and packet address differ.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @see        java.net.DatagramPacket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @see        SecurityManager#checkMulticast(InetAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @see        SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public void send(DatagramPacket p) throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        InetAddress packetAddress = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        synchronized (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                throw new SocketException("Socket is closed");
5180
8161f879d704 6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents: 1817
diff changeset
   618
            checkAddress (p.getAddress(), "send");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if (connectState == ST_NOT_CONNECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                // check the address is ok wiht the security manager on every send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                // The reason you want to synchronize on datagram packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                // is because you dont want an applet to change the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                // while you are trying to send the packet for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                // after the security check but before the send.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    if (p.getAddress().isMulticastAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        security.checkMulticast(p.getAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                        security.checkConnect(p.getAddress().getHostAddress(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                              p.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                // we're connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                packetAddress = p.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                if (packetAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    p.setAddress(connectedAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    p.setPort(connectedPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                } else if ((!packetAddress.equals(connectedAddress)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                           p.getPort() != connectedPort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    throw new IllegalArgumentException("connected address " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                                                       "and packet address" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                                       " differ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            // Check whether the socket is bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            // call the  method to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            getImpl().send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Receives a datagram packet from this socket. When this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * returns, the <code>DatagramPacket</code>'s buffer is filled with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * the data received. The datagram packet also contains the sender's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * IP address, and the port number on the sender's machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * This method blocks until a datagram is received. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * <code>length</code> field of the datagram packet object contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * the length of the received message. If the message is longer than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * the packet's length, the message is truncated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * If there is a security manager, a packet cannot be received if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * security manager's <code>checkAccept</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * does not allow it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @param      p   the <code>DatagramPacket</code> into which to place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *                 the incoming data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @exception  SocketTimeoutException  if setSoTimeout was previously called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *                 and the timeout has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @exception  PortUnreachableException may be thrown if the socket is connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *             to a currently unreachable destination. Note, there is no guarantee that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *             exception will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @exception  java.nio.channels.IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *             if this socket has an associated channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *             and the channel is in non-blocking mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @see        java.net.DatagramPacket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @see        java.net.DatagramSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public synchronized void receive(DatagramPacket p) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        synchronized (p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            if (!isBound())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            if (connectState == ST_NOT_CONNECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                // check the address is ok with the security manager before every recv.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        String peekAd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                        int peekPort = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                        // peek at the packet to see who it is from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                        if (!oldImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                            // We can use the new peekData() API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                            DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                            peekPort = getImpl().peekData(peekPacket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                            peekAd = peekPacket.getAddress().getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                            InetAddress adr = new InetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                            peekPort = getImpl().peek(adr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                            peekAd = adr.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                            security.checkAccept(peekAd, peekPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                            // security check succeeded - so now break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                            // and recv the packet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                            // Throw away the offending packet by consuming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                            // it in a tmp buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                            DatagramPacket tmp = new DatagramPacket(new byte[1], 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                            getImpl().receive(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                            // silently discard the offending packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                            // and continue: unknown/malicious
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                            // entities on nets should not make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                            // runtime throw security exception and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                            // disrupt the applet by sending random
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                            // datagram packets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    } // end of while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (connectState == ST_CONNECTED_NO_IMPL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                // We have to do the filtering the old fashioned way since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                // the native impl doesn't support connect or the connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                // via the impl failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                boolean stop = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                while (!stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    // peek at the packet to see who it is from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                    InetAddress peekAddress = new InetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    int peekPort = getImpl().peek(peekAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    if ((!connectedAddress.equals(peekAddress)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        (connectedPort != peekPort)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                        // throw the packet away and silently continue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                        DatagramPacket tmp = new DatagramPacket(new byte[1], 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                        getImpl().receive(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        stop = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            // If the security check succeeds, or the datagram is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            // connected then receive the packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            getImpl().receive(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * Gets the local address to which the socket is bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * <p>If there is a security manager, its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * <code>checkConnect</code> method is first called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * with the host address and <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * as its arguments to see if the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @see SecurityManager#checkConnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @return  the local address to which the socket is bound,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *          <code>null</code> if the socket is closed, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *          an <code>InetAddress</code> representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *          {@link InetAddress#isAnyLocalAddress wildcard}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *          address if either the socket is not bound, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *          the security manager <code>checkConnect</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *          method does not allow the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @since   1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    public InetAddress getLocalAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        InetAddress in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            in = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            if (in.isAnyLocalAddress()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                in = InetAddress.anyLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            SecurityManager s = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                s.checkConnect(in.getHostAddress(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            in = InetAddress.anyLocalAddress(); // "0.0.0.0"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * Returns the port number on the local host to which this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * is bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * @return  the port number on the local host to which this socket is bound,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                <code>-1</code> if the socket is closed, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                <code>0</code> if it is not bound yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            return getImpl().getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    /** Enable/disable SO_TIMEOUT with the specified timeout, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *  milliseconds. With this option set to a non-zero timeout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *  a call to receive() for this DatagramSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *  will block for only this amount of time.  If the timeout expires,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *  a <B>java.net.SocketTimeoutException</B> is raised, though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *  DatagramSocket is still valid.  The option <B>must</B> be enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *  prior to entering the blocking operation to have effect.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *  timeout must be > 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *  A timeout of zero is interpreted as an infinite timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @param timeout the specified timeout in milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @throws SocketException if there is an error in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @see #getSoTimeout()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    public synchronized void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * Retrieve setting for SO_TIMEOUT.  0 returns implies that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * option is disabled (i.e., timeout of infinity).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @return the setting for SO_TIMEOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @throws SocketException if there is an error in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * @see #setSoTimeout(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    public synchronized int getSoTimeout() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        if (getImpl() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        /* extra type safety */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            return ((Integer) o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * Sets the SO_SNDBUF option to the specified value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * <tt>DatagramSocket</tt>. The SO_SNDBUF option is used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * network implementation as a hint to size the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * network I/O buffers. The SO_SNDBUF setting may also be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * by the network implementation to determine the maximum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * of the packet that can be sent on this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * As SO_SNDBUF is a hint, applications that want to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * what size the buffer is should call {@link #getSendBufferSize()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Increasing the buffer size may allow multiple outgoing packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * to be queued by the network implementation when the send rate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * is high.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * Note: If {@link #send(DatagramPacket)} is used to send a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * <code>DatagramPacket</code> that is larger than the setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * of SO_SNDBUF then it is implementation specific if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * packet is sent or discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @param size the size to which to set the send buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * size. This value must be greater than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * @exception IllegalArgumentException if the value is 0 or is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @see #getSendBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    public synchronized void setSendBufferSize(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if (!(size > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            throw new IllegalArgumentException("negative send size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Get value of the SO_SNDBUF option for this <tt>DatagramSocket</tt>, that is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * buffer size used by the platform for output on this <tt>DatagramSocket</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @return the value of the SO_SNDBUF option for this <tt>DatagramSocket</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @exception SocketException if there is an error in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @see #setSendBufferSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    public synchronized int getSendBufferSize() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        Object o = getImpl().getOption(SocketOptions.SO_SNDBUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            result = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * Sets the SO_RCVBUF option to the specified value for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * <tt>DatagramSocket</tt>. The SO_RCVBUF option is used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * the network implementation as a hint to size the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * network I/O buffers. The SO_RCVBUF setting may also be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * by the network implementation to determine the maximum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * of the packet that can be received on this socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * Because SO_RCVBUF is a hint, applications that want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * verify what size the buffers were set to should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * {@link #getReceiveBufferSize()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * Increasing SO_RCVBUF may allow the network implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * to buffer multiple packets when packets arrive faster than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * are being received using {@link #receive(DatagramPacket)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * Note: It is implementation specific if a packet larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * than SO_RCVBUF can be received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @param size the size to which to set the receive buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * size. This value must be greater than 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @exception SocketException if there is an error in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @exception IllegalArgumentException if the value is 0 or is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @see #getReceiveBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    public synchronized void setReceiveBufferSize(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            throw new IllegalArgumentException("invalid receive size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * Get value of the SO_RCVBUF option for this <tt>DatagramSocket</tt>, that is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * buffer size used by the platform for input on this <tt>DatagramSocket</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @return the value of the SO_RCVBUF option for this <tt>DatagramSocket</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @exception SocketException if there is an error in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * @see #setReceiveBufferSize(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    public synchronized int getReceiveBufferSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    throws SocketException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        if (o instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            result = ((Integer)o).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * Enable/disable the SO_REUSEADDR socket option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * For UDP sockets it may be necessary to bind more than one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * socket to the same socket address. This is typically for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * purpose of receiving multicast packets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * (See {@link java.net.MulticastSocket}). The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * <tt>SO_REUSEADDR</tt> socket option allows multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * sockets to be bound to the same socket address if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * <tt>SO_REUSEADDR</tt> socket option is enabled prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * to binding the socket using {@link #bind(SocketAddress)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * Note: This functionality is not supported by all existing platforms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * so it is implementation specific whether this option will be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * or not. However, if it is not supported then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * {@link #getReuseAddress()} will always return <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * When a <tt>DatagramSocket</tt> is created the initial setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * of <tt>SO_REUSEADDR</tt> is disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * The behaviour when <tt>SO_REUSEADDR</tt> is enabled or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * disabled after a socket is bound (See {@link #isBound()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * is not defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * @param on  whether to enable or disable the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @exception SocketException if an error occurs enabling or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     *            disabling the <tt>SO_RESUEADDR</tt> socket option,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     *            or the socket is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @see #getReuseAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * @see #bind(SocketAddress)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @see #isBound()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * @see #isClosed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    public synchronized void setReuseAddress(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        // Integer instead of Boolean for compatibility with older DatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        if (oldImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            getImpl().setOption(SocketOptions.SO_REUSEADDR, new Integer(on?-1:0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            getImpl().setOption(SocketOptions.SO_REUSEADDR, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * Tests if SO_REUSEADDR is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @return a <code>boolean</code> indicating whether or not SO_REUSEADDR is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * @see #setReuseAddress(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    public synchronized boolean getReuseAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        Object o = getImpl().getOption(SocketOptions.SO_REUSEADDR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        return ((Boolean)o).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * Enable/disable SO_BROADCAST.
1817
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1034
     *
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1035
     * <p> Some operating systems may require that the Java virtual machine be
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1036
     * started with implementation specific privileges to enable this option or
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1037
     * send broadcast datagrams.
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1038
     *
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1039
     * @param  on
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1040
     *         whether or not to have broadcast turned on.
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1041
     *
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1042
     * @throws  SocketException
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1043
     *          if there is an error in the underlying protocol, such as an UDP
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1044
     *          error.
2deb4cc4d5dc 6755782: It is not clear how DatagramSocket deals with broadcast enabling/disabling
chegar
parents: 715
diff changeset
  1045
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * @see #getBroadcast()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    public synchronized void setBroadcast(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        getImpl().setOption(SocketOptions.SO_BROADCAST, Boolean.valueOf(on));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * Tests if SO_BROADCAST is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * @return a <code>boolean</code> indicating whether or not SO_BROADCAST is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * @exception SocketException if there is an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * in the underlying protocol, such as an UDP error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @see #setBroadcast(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    public synchronized boolean getBroadcast() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        return ((Boolean)(getImpl().getOption(SocketOptions.SO_BROADCAST))).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * Sets traffic class or type-of-service octet in the IP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * datagram header for datagrams sent from this DatagramSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * As the underlying network implementation may ignore this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * value applications should consider it a hint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * <P> The tc <B>must</B> be in the range <code> 0 <= tc <=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * 255</code> or an IllegalArgumentException will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * <p>Notes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * <p>For Internet Protocol v4 the value consists of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * <code>integer</code>, the least significant 8 bits of which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * represent the value of the TOS octet in IP packets sent by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * the socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * RFC 1349 defines the TOS values as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * <LI><CODE>IPTOS_LOWCOST (0x02)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * <LI><CODE>IPTOS_RELIABILITY (0x04)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * <LI><CODE>IPTOS_THROUGHPUT (0x08)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * <LI><CODE>IPTOS_LOWDELAY (0x10)</CODE></LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * The last low order bit is always ignored as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * corresponds to the MBZ (must be zero) bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * Setting bits in the precedence field may result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * SocketException indicating that the operation is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * permitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * for Internet Protocol v6 <code>tc</code> is the value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * would be placed into the sin6_flowinfo field of the IP header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * @param tc        an <code>int</code> value for the bitset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * @throws SocketException if there is an error setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * traffic class or type-of-service
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * @see #getTrafficClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    public synchronized void setTrafficClass(int tc) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        if (tc < 0 || tc > 255)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            throw new IllegalArgumentException("tc is not in range 0 -- 255");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        getImpl().setOption(SocketOptions.IP_TOS, new Integer(tc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * Gets traffic class or type-of-service in the IP datagram
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * header for packets sent from this DatagramSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * As the underlying network implementation may ignore the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * traffic class or type-of-service set using {@link #setTrafficClass(int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * this method may return a different value than was previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * set using the {@link #setTrafficClass(int)} method on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * DatagramSocket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @return the traffic class or type-of-service already set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * @throws SocketException if there is an error obtaining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * traffic class or type-of-service value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * @see #setTrafficClass(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    public synchronized int getTrafficClass() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            throw new SocketException("Socket is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        return ((Integer)(getImpl().getOption(SocketOptions.IP_TOS))).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * Closes this datagram socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * Any thread currently blocked in {@link #receive} upon this socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * will throw a {@link SocketException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * <p> If this socket has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            if (isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            impl.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * Returns whether the socket is closed or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * @return true if the socket has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        synchronized(closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            return closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * Returns the unique {@link java.nio.channels.DatagramChannel} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * associated with this datagram socket, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * <p> A datagram socket will have a channel if, and only if, the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * itself was created via the {@link java.nio.channels.DatagramChannel#open
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * DatagramChannel.open} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * @return  the datagram channel associated with this datagram socket,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     *          or <tt>null</tt> if this socket was not created for a channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    public DatagramChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * User defined factory for all datagram sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    static DatagramSocketImplFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * Sets the datagram socket implementation factory for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * application. The factory can be specified only once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * When an application creates a new datagram socket, the socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * implementation factory's <code>createDatagramSocketImpl</code> method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * called to create the actual datagram socket implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * Passing <code>null</code> to the method is a no-op unless the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * was already set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * <p>If there is a security manager, this method first calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * the security manager's <code>checkSetFactory</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * to ensure the operation is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * @param      fac   the desired factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @exception  IOException  if an I/O error occurs when setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     *              datagram socket factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * @exception  SocketException  if the factory is already defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     *             <code>checkSetFactory</code> method doesn't allow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * @see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     java.net.DatagramSocketImplFactory#createDatagramSocketImpl()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * @see       SecurityManager#checkSetFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    public static synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
       throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            throw new SocketException("factory already defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            security.checkSetFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        factory = fac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
}