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