⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datagramsocket.java

📁 俄罗斯高人Mamaich的Pocket gcc编译器(运行在PocketPC上)的全部源代码。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  {    if (impl == null)      throw new SocketException ("Cannot initialize Socket implementation");    if (size < 0)      throw new IllegalArgumentException("Buffer size is less than 0");    impl.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      {        impl.connect (address, port);        remoteAddress = address;        remotePort = port;      }    catch (SocketException e)      {      }  }  /**   * 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()  {    impl.disconnect();    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 The datagram packet to put the incoming data into.   *    * @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 (impl == null)      throw new IOException ("Cannot initialize Socket implementation");    if (remoteAddress != null && remoteAddress.isMulticastAddress ())      throw new IOException (        "Socket connected to a multicast address my not receive");    if (ch != null && !ch.isBlocking ())      throw new IllegalBlockingModeException ();    impl.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  {    // 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 (ch != null && !ch.isBlocking ())      throw new IllegalBlockingModeException ();    impl.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 (! (address instanceof InetSocketAddress))      throw new IllegalArgumentException ();    InetSocketAddress tmp = (InetSocketAddress) address;    SecurityManager s = System.getSecurityManager ();    if (s != null)      s.checkListen(tmp.getPort ());    impl.bind (tmp.getPort (), tmp.getAddress ());  }  /**   * Checks if the datagram socket is closed.   *   * @since 1.4   */  public boolean isClosed()  {    return closed;  }  /**   * Returns the datagram channel assoziated with this datagram socket.   *    * @since 1.4   */  public DatagramChannel getChannel()  {    return ch;  }  /**   * 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 ( !(address instanceof InetSocketAddress) )      throw new IllegalArgumentException (		      "SocketAddress is not InetSocketAddress");    InetSocketAddress tmp = (InetSocketAddress) address;    connect( tmp.getAddress(), tmp.getPort());  }    /**   * Returns the binding state of the socket.   *    * @since 1.4   */  public boolean isBound()  {    try      {        Object bindaddr = impl.getOption (SocketOptions.SO_BINDADDR);      }    catch (SocketException e)      {        return false;      }    return true;  }  /**   * Returns the connection state of the socket.   *    * @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.   *    * @since 1.4   */  public SocketAddress getRemoteSocketAddress()  {    if (!isConnected ())      return null;    return new InetSocketAddress (remoteAddress, remotePort);  }  /**   * Returns the local SocketAddress this socket is bound to   * or null if it is not bound.   *    * @since 1.4   */  public SocketAddress getLocalSocketAddress()  {    InetAddress addr;        try      {        addr = (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);      }    catch (SocketException e)      {        return null;      }    return new InetSocketAddress (addr, impl.localPort);  }  /**   * 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 (impl == null)      throw new SocketException ("Cannot initialize Socket implementation");    impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));  }  /**   * Checks if SO_REUSEADDR is enabled.   *   * @exception SocketException If an error occurs.   *    * @since 1.4   */  public boolean getReuseAddress() throws SocketException  {    if (impl == null)      throw new SocketException ("Cannot initialize Socket implementation");    Object obj = impl.getOption (SocketOptions.SO_REUSEADDR);      if (obj instanceof Boolean)      return(((Boolean) obj).booleanValue ());    else       throw new SocketException ("Unexpected type");  }  /**   * Enables/Disables SO_BROADCAST   *    * @param on Whether or not to have SO_BROADCAST turned on   *   * @exception SocketException If an error occurs   *   * @since 1.4   */  public void setBroadcast(boolean on) throws SocketException  {    if (impl == null)      throw new SocketException ("Cannot initialize Socket implementation");    impl.setOption (SocketOptions.SO_BROADCAST, new Boolean (on));  }  /**   * Checks if SO_BROADCAST is enabled   *    * @exception SocketException If an error occurs   *    * @since 1.4   */  public boolean getBroadcast() throws SocketException  {    if (impl == null)      throw new SocketException ("Cannot initialize Socket implementation");    Object obj = impl.getOption (SocketOptions.SO_BROADCAST);      if (obj instanceof Boolean)      return ((Boolean) obj).booleanValue ();    else       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 (impl == null)      throw new SocketException ("Cannot initialize Socket implementation");    if (tc < 0 || tc > 255)      throw new IllegalArgumentException();    impl.setOption (SocketOptions.IP_TOS, new Integer (tc));  }    /**   * Returns the current traffic class   *    * @see DatagramSocket:setTrafficClass   *   * @exception SocketException If an error occurs   *    * @since 1.4   */  public int getTrafficClass() throws SocketException  {    if (impl == null)      throw new SocketException( "Cannot initialize Socket implementation");    Object obj = impl.getOption(SocketOptions.IP_TOS);    if (obj instanceof Integer)      return ((Integer) obj).intValue ();    else      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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -