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

Print this page




  87      * Connection state:
  88      * ST_NOT_CONNECTED = socket not connected
  89      * ST_CONNECTED = socket connected
  90      * ST_CONNECTED_NO_IMPL = socket connected but not at impl level
  91      */
  92     static final int ST_NOT_CONNECTED = 0;
  93     static final int ST_CONNECTED = 1;
  94     static final int ST_CONNECTED_NO_IMPL = 2;
  95 
  96     int connectState = ST_NOT_CONNECTED;
  97 
  98     /*
  99      * Connected address & port
 100      */
 101     InetAddress connectedAddress = null;
 102     int connectedPort = -1;
 103 
 104     /**
 105      * Connects this socket to a remote socket address (IP address + port number).
 106      * Binds socket if not already bound.
 107      * <p>
 108      * @param   address The remote address.
 109      * @param   port    The remote port
 110      * @throws  SocketException if binding the socket fails.
 111      */
 112     private synchronized void connectInternal(InetAddress address, int port) throws SocketException {
 113         if (port < 0 || port > 0xFFFF) {
 114             throw new IllegalArgumentException("connect: " + port);
 115         }
 116         if (address == null) {
 117             throw new IllegalArgumentException("connect: null address");
 118         }
 119         checkAddress (address, "connect");
 120         if (isClosed())
 121             return;
 122         SecurityManager security = System.getSecurityManager();
 123         if (security != null) {
 124             if (address.isMulticastAddress()) {
 125                 security.checkMulticast(address);
 126             } else {
 127                 security.checkConnect(address.getHostAddress(), port);


 321     /**
 322      * Get the {@code DatagramSocketImpl} attached to this socket,
 323      * creating it if necessary.
 324      *
 325      * @return  the {@code DatagramSocketImpl} attached to that
 326      *          DatagramSocket
 327      * @throws SocketException if creation fails.
 328      * @since 1.4
 329      */
 330     DatagramSocketImpl getImpl() throws SocketException {
 331         if (!created)
 332             createImpl();
 333         return impl;
 334     }
 335 
 336     /**
 337      * Binds this DatagramSocket to a specific address and port.
 338      * <p>
 339      * If the address is {@code null}, then the system will pick up
 340      * an ephemeral port and a valid local address to bind the socket.
 341      *<p>
 342      * @param   addr The address and port to bind to.
 343      * @throws  SocketException if any error happens during the bind, or if the
 344      *          socket is already bound.
 345      * @throws  SecurityException  if a security manager exists and its
 346      *             {@code checkListen} method doesn't allow the operation.
 347      * @throws IllegalArgumentException if addr is a SocketAddress subclass
 348      *         not supported by this socket.
 349      * @since 1.4
 350      */
 351     public synchronized void bind(SocketAddress addr) throws SocketException {
 352         if (isClosed())
 353             throw new SocketException("Socket is closed");
 354         if (isBound())
 355             throw new SocketException("already bound");
 356         if (addr == null)
 357             addr = new InetSocketAddress(0);
 358         if (!(addr instanceof InetSocketAddress))
 359             throw new IllegalArgumentException("Unsupported address type!");
 360         InetSocketAddress epoint = (InetSocketAddress) addr;
 361         if (epoint.isUnresolved())


1223      * Sets the datagram socket implementation factory for the
1224      * application. The factory can be specified only once.
1225      * <p>
1226      * When an application creates a new datagram socket, the socket
1227      * implementation factory's {@code createDatagramSocketImpl} method is
1228      * called to create the actual datagram socket implementation.
1229      * <p>
1230      * Passing {@code null} to the method is a no-op unless the factory
1231      * was already set.
1232      *
1233      * <p>If there is a security manager, this method first calls
1234      * the security manager's {@code checkSetFactory} method
1235      * to ensure the operation is allowed.
1236      * This could result in a SecurityException.
1237      *
1238      * @param      fac   the desired factory.
1239      * @exception  IOException  if an I/O error occurs when setting the
1240      *              datagram socket factory.
1241      * @exception  SocketException  if the factory is already defined.
1242      * @exception  SecurityException  if a security manager exists and its
1243      *             {@code checkSetFactory} method doesn't allow the
1244      operation.
1245      * @see
1246      java.net.DatagramSocketImplFactory#createDatagramSocketImpl()
1247      * @see       SecurityManager#checkSetFactory
1248      * @since 1.3
1249      */
1250     public static synchronized void
1251     setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
1252        throws IOException
1253     {
1254         if (factory != null) {
1255             throw new SocketException("factory already defined");
1256         }
1257         SecurityManager security = System.getSecurityManager();
1258         if (security != null) {
1259             security.checkSetFactory();
1260         }
1261         factory = fac;
1262     }
1263 }


  87      * Connection state:
  88      * ST_NOT_CONNECTED = socket not connected
  89      * ST_CONNECTED = socket connected
  90      * ST_CONNECTED_NO_IMPL = socket connected but not at impl level
  91      */
  92     static final int ST_NOT_CONNECTED = 0;
  93     static final int ST_CONNECTED = 1;
  94     static final int ST_CONNECTED_NO_IMPL = 2;
  95 
  96     int connectState = ST_NOT_CONNECTED;
  97 
  98     /*
  99      * Connected address & port
 100      */
 101     InetAddress connectedAddress = null;
 102     int connectedPort = -1;
 103 
 104     /**
 105      * Connects this socket to a remote socket address (IP address + port number).
 106      * Binds socket if not already bound.
 107      *
 108      * @param   address The remote address.
 109      * @param   port    The remote port
 110      * @throws  SocketException if binding the socket fails.
 111      */
 112     private synchronized void connectInternal(InetAddress address, int port) throws SocketException {
 113         if (port < 0 || port > 0xFFFF) {
 114             throw new IllegalArgumentException("connect: " + port);
 115         }
 116         if (address == null) {
 117             throw new IllegalArgumentException("connect: null address");
 118         }
 119         checkAddress (address, "connect");
 120         if (isClosed())
 121             return;
 122         SecurityManager security = System.getSecurityManager();
 123         if (security != null) {
 124             if (address.isMulticastAddress()) {
 125                 security.checkMulticast(address);
 126             } else {
 127                 security.checkConnect(address.getHostAddress(), port);


 321     /**
 322      * Get the {@code DatagramSocketImpl} attached to this socket,
 323      * creating it if necessary.
 324      *
 325      * @return  the {@code DatagramSocketImpl} attached to that
 326      *          DatagramSocket
 327      * @throws SocketException if creation fails.
 328      * @since 1.4
 329      */
 330     DatagramSocketImpl getImpl() throws SocketException {
 331         if (!created)
 332             createImpl();
 333         return impl;
 334     }
 335 
 336     /**
 337      * Binds this DatagramSocket to a specific address and port.
 338      * <p>
 339      * If the address is {@code null}, then the system will pick up
 340      * an ephemeral port and a valid local address to bind the socket.
 341      *
 342      * @param   addr The address and port to bind to.
 343      * @throws  SocketException if any error happens during the bind, or if the
 344      *          socket is already bound.
 345      * @throws  SecurityException  if a security manager exists and its
 346      *             {@code checkListen} method doesn't allow the operation.
 347      * @throws IllegalArgumentException if addr is a SocketAddress subclass
 348      *         not supported by this socket.
 349      * @since 1.4
 350      */
 351     public synchronized void bind(SocketAddress addr) throws SocketException {
 352         if (isClosed())
 353             throw new SocketException("Socket is closed");
 354         if (isBound())
 355             throw new SocketException("already bound");
 356         if (addr == null)
 357             addr = new InetSocketAddress(0);
 358         if (!(addr instanceof InetSocketAddress))
 359             throw new IllegalArgumentException("Unsupported address type!");
 360         InetSocketAddress epoint = (InetSocketAddress) addr;
 361         if (epoint.isUnresolved())


1223      * Sets the datagram socket implementation factory for the
1224      * application. The factory can be specified only once.
1225      * <p>
1226      * When an application creates a new datagram socket, the socket
1227      * implementation factory's {@code createDatagramSocketImpl} method is
1228      * called to create the actual datagram socket implementation.
1229      * <p>
1230      * Passing {@code null} to the method is a no-op unless the factory
1231      * was already set.
1232      *
1233      * <p>If there is a security manager, this method first calls
1234      * the security manager's {@code checkSetFactory} method
1235      * to ensure the operation is allowed.
1236      * This could result in a SecurityException.
1237      *
1238      * @param      fac   the desired factory.
1239      * @exception  IOException  if an I/O error occurs when setting the
1240      *              datagram socket factory.
1241      * @exception  SocketException  if the factory is already defined.
1242      * @exception  SecurityException  if a security manager exists and its
1243      *             {@code checkSetFactory} method doesn't allow the operation.
1244      * @see       java.net.DatagramSocketImplFactory#createDatagramSocketImpl()


1245      * @see       SecurityManager#checkSetFactory
1246      * @since 1.3
1247      */
1248     public static synchronized void
1249     setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
1250        throws IOException
1251     {
1252         if (factory != null) {
1253             throw new SocketException("factory already defined");
1254         }
1255         SecurityManager security = System.getSecurityManager();
1256         if (security != null) {
1257             security.checkSetFactory();
1258         }
1259         factory = fac;
1260     }
1261 }