datagramsocket.java

来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 933 行 · 第 1/2 页

JAVA
933
字号
    getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));  }  /**   * This method connects this socket to the specified address and port.   * When a datagram socket is connected, it will only send or receive   * packets to and from the host to which it is connected. A multicast   * socket that is connected may only send and not receive packets.   *    * @param address The address to connect this socket to.   * @param port The port to connect this socket to.   *   * @exception SocketException If an error occurs.   * @exception IllegalArgumentException If address or port are invalid.   * @exception SecurityException If the caller is not allowed to send   * datagrams to or receive from this address and port.   *   * @since 1.2   */  public void connect(InetAddress address, int port)  {    if (address == null)      throw new IllegalArgumentException ("Connect address may not be null");    if ((port < 1) || (port > 65535))      throw new IllegalArgumentException ("Port number is illegal: " + port);    SecurityManager sm = System.getSecurityManager();    if (sm != null)      sm.checkConnect(address.getHostName(), port);    try      {        getImpl().connect (address, port);        remoteAddress = address;        remotePort = port;      }    catch (SocketException e)      {        // This means simply not connected or connect not implemented.      }  }  /**   * This method disconnects this socket from the address/port it was   * connected to.  If the socket was not connected in the first place,   * this method does nothing.   *    * @since 1.2   */  public void disconnect()  {    if (!isConnected())      return;    try      {	getImpl().disconnect();      }    catch (SocketException e)      {	// This cannot happen as we are connected.      }    finally      {	remoteAddress = null;	remotePort = -1;      }  }  /**   * Reads a datagram packet from the socket.  Note that this method   * will block until a packet is received from the network.  On return,   * the passed in <code>DatagramPacket</code> is populated with the data   * received and all the other information about the packet.   *   * @param p A <code>DatagramPacket</code> for storing the data   *   * @exception IOException If an error occurs.   * @exception SocketTimeoutException If setSoTimeout was previously called   * and the timeout has expired.   * @exception PortUnreachableException If the socket is connected to a   * currently unreachable destination. Note, there is no guarantee that the   * exception will be thrown.   * @exception IllegalBlockingModeException If this socket has an associated   * channel, and the channel is in non-blocking mode.   * @exception SecurityException If a security manager exists and its   * checkAccept ethod doesn't allow the receive.   */  public synchronized void receive(DatagramPacket p) throws IOException  {    if (isClosed())      throw new SocketException("socket is closed");        if (remoteAddress != null        && remoteAddress.isMulticastAddress())      throw new IOException        ("Socket connected to a multicast address my not receive");    if (getChannel() != null        && !getChannel().isBlocking ())      throw new IllegalBlockingModeException ();    getImpl().receive(p);    SecurityManager s = System.getSecurityManager();    if (s != null && isConnected ())      s.checkAccept (p.getAddress().getHostName (), p.getPort ());  }  /**   * Sends the specified packet.  The host and port to which the packet   * are to be sent should be set inside the packet.   *   * @param p The datagram packet to send.   *   * @exception IOException If an error occurs.   * @exception SecurityException If a security manager exists and its   * checkMulticast or checkConnect method doesn't allow the send.   * @exception PortUnreachableException If the socket is connected to a   * currently unreachable destination. Note, there is no guarantee that the   * exception will be thrown.   * @exception IllegalBlockingModeException If this socket has an associated   * channel, and the channel is in non-blocking mode.   */  public void send(DatagramPacket p) throws IOException  {    if (isClosed())      throw new SocketException("socket is closed");        // JDK1.2: Don't do security checks if socket is connected; see jdk1.2 api.    SecurityManager s = System.getSecurityManager();    if (s != null && !isConnected ())      {        InetAddress addr = p.getAddress();        if (addr.isMulticastAddress())          s.checkMulticast(addr);        else          s.checkConnect(addr.getHostAddress(), p.getPort());      }    if (isConnected ())      {        if (p.getAddress () != null && (remoteAddress != p.getAddress () ||                                        remotePort != p.getPort ()))          throw new IllegalArgumentException (            "DatagramPacket address does not match remote address" );      }	        // FIXME: if this is a subclass of MulticastSocket,    // use getTimeToLive for TTL val.    if (getChannel() != null        && !getChannel().isBlocking ())      throw new IllegalBlockingModeException ();    getImpl().send(p);  }  /**   * Binds the socket to the given socket address.   *   * @param address The socket address to bind to.   *   * @exception SocketException If an error occurs.   * @exception SecurityException If a security manager exists and   * its checkListen method doesn't allow the operation.   * @exception IllegalArgumentException If address type is not supported.   *   * @since 1.4   */  public void bind (SocketAddress address)    throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");        if (! (address instanceof InetSocketAddress))      throw new IllegalArgumentException("unsupported address type");    InetAddress addr = ((InetSocketAddress) address).getAddress();    int port = ((InetSocketAddress) address).getPort();    if (port < 0 || port > 65535)      throw new IllegalArgumentException("Invalid port: " + port);    SecurityManager s = System.getSecurityManager ();    if (s != null)      s.checkListen(port);    if (addr == null)      addr = InetAddress.ANY_IF;        try      {        getImpl().bind(port, addr);	bound = true;      }    catch (SocketException exception)      {        getImpl().close();        throw exception;      }    catch (RuntimeException exception)      {        getImpl().close();        throw exception;      }    catch (Error error)      {        getImpl().close();        throw error;      }  }  /**   * Checks if the datagram socket is closed.   *   * @return True if socket is closed, false otherwise.   *    * @since 1.4   */  public boolean isClosed()  {    return impl == null;  }  /**   * Returns the datagram channel assoziated with this datagram socket.   *    * @return The associated <code>DatagramChannel</code> object or null   *    * @since 1.4   */  public DatagramChannel getChannel()  {    return null;  }  /**   * Connects the datagram socket to a specified socket address.   *   * @param address The socket address to connect to.   *   * @exception SocketException If an error occurs.   * @exception IllegalArgumentException If address type is not supported.   *   * @since 1.4   */  public void connect (SocketAddress address) throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");        if ( !(address instanceof InetSocketAddress) )      throw new IllegalArgumentException("unsupported address type");    InetSocketAddress tmp = (InetSocketAddress) address;    connect(tmp.getAddress(), tmp.getPort());  }    /**   * Returns the binding state of the socket.   *    * @return True if socket bound, false otherwise.   *    * @since 1.4   */  public boolean isBound()  {    return bound;  }  /**   * Returns the connection state of the socket.   *    * @return True if socket is connected, false otherwise.   *    * @since 1.4   */  public boolean isConnected()  {    return remoteAddress != null;  }  /**   * Returns the SocketAddress of the host this socket is conneted to   * or null if this socket is not connected.   *    * @return The socket address of the remote host if connected or null   *    * @since 1.4   */  public SocketAddress getRemoteSocketAddress()  {    if (!isConnected ())      return null;    return new InetSocketAddress (remoteAddress, remotePort);  }  /**   * Returns the local SocketAddress this socket is bound to.   *   * @return The local SocketAddress or null if the socket is not bound.   *    * @since 1.4   */  public SocketAddress getLocalSocketAddress()  {    if (!isBound())      return null;        return new InetSocketAddress (getLocalAddress(), getLocalPort());  }  /**   * Enables/Disables SO_REUSEADDR.   *   * @param on Whether or not to have SO_REUSEADDR turned on.   *   * @exception SocketException If an error occurs.   *   * @since 1.4   */  public void setReuseAddress(boolean on) throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    getImpl().setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));  }  /**   * Checks if SO_REUSEADDR is enabled.   *   * @return True if SO_REUSEADDR is set on the socket, false otherwise.   *   * @exception SocketException If an error occurs.   *    * @since 1.4   */  public boolean getReuseAddress() throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    Object buf = getImpl().getOption (SocketOptions.SO_REUSEADDR);      if (buf instanceof Boolean)      return ((Boolean) buf).booleanValue();    throw new SocketException("unexpected type");  }  /**   * Enables/Disables SO_BROADCAST   *    * @param enable True if SO_BROADCAST should be enabled, false otherwise.   *   * @exception SocketException If an error occurs   *   * @since 1.4   */  public void setBroadcast(boolean enable) throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    getImpl().setOption(SocketOptions.SO_BROADCAST, new Boolean(enable));  }  /**   * Checks if SO_BROADCAST is enabled   *    * @return Whether SO_BROADCAST is set   *   * @exception SocketException If an error occurs   *    * @since 1.4   */  public boolean getBroadcast() throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    Object buf = getImpl().getOption(SocketOptions.SO_BROADCAST);      if (buf instanceof Boolean)      return ((Boolean) buf).booleanValue();    throw new SocketException("unexpected type");  }  /**   * Sets the traffic class value   *   * @param tc The traffic class   *   * @exception SocketException If an error occurs   * @exception IllegalArgumentException If tc value is illegal   *   * @see DatagramSocket#getTrafficClass()   *    * @since 1.4   */  public void setTrafficClass(int tc)    throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    if (tc < 0 || tc > 255)      throw new IllegalArgumentException();    getImpl().setOption (SocketOptions.IP_TOS, new Integer (tc));  }    /**   * Returns the current traffic class   *    * @return The current traffic class.   *   * @see DatagramSocket#setTrafficClass(int tc)   *   * @exception SocketException If an error occurs   *    * @since 1.4   */  public int getTrafficClass() throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");        Object buf = getImpl().getOption(SocketOptions.IP_TOS);    if (buf instanceof Integer)      return ((Integer) buf).intValue();    throw new SocketException("unexpected type");  }    /**   * Sets the datagram socket implementation factory for the application   *   * @param fac The factory to set   *   * @exception IOException If an error occurs   * @exception SocketException If the factory is already defined   * @exception SecurityException If a security manager exists and its   * checkSetFactory method doesn't allow the operation   */  public static void setDatagramSocketImplFactory    (DatagramSocketImplFactory fac) throws IOException  {    if (factory != null)      throw new SocketException ("DatagramSocketImplFactory already defined");    SecurityManager sm = System.getSecurityManager();    if (sm != null)      sm.checkSetFactory();    factory = fac;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?