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

📄 serversocket.java

📁 kaffe Java 解释器语言,源码,Java的子集系统,开放源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    return impl.getLocalPort();  }  /**   * Returns the local socket address   *   * @since 1.4   */  public SocketAddress getLocalSocketAddress()  {    if (!isBound())      return null;        return new InetSocketAddress(getInetAddress(), getLocalPort());  }  /**   * Accepts a new connection and returns a connected <code>Socket</code>    * instance representing that connection.  This method will block until a    * connection is available.   *   * @exception IOException If an error occurs   * @exception SecurityException If a security manager exists and its   * checkListen method doesn't allow the operation   * @exception IllegalBlockingModeException If this socket has an associated   * channel, and the channel is in non-blocking mode   * @exception SocketTimeoutException If a timeout was previously set with   * setSoTimeout and the timeout has been reached   */  public Socket accept () throws IOException  {    SecurityManager sm = System.getSecurityManager ();    if (sm != null)      sm.checkListen (impl.getLocalPort ());    Socket socket = new Socket();    implAccept (socket);    return socket;  }  /**   * This protected method is used to help subclasses override    * <code>ServerSocket.accept()</code>.  The passed in socket will be   * connected when this method returns.   *   * @param socket The socket that is used for the accepted connection   *   * @exception IOException If an error occurs   * @exception IllegalBlockingModeException If this socket has an associated   * channel, and the channel is in non-blocking mode   *   * @since 1.1   */  protected final void implAccept (Socket socket)    throws IOException  {    if (isClosed())      throw new SocketException("ServerSocket is closed");        if (getChannel() != null        && !getChannel().isBlocking())      throw new IllegalBlockingModeException();	        impl.accept(socket.getImpl());  }  /**   * Closes this socket and stops listening for connections   *   * @exception IOException If an error occurs   */  public void close () throws IOException  {    if (isClosed())      return;        impl.close();    impl = null;    bound = false;    if (getChannel() != null)      getChannel().close();  }  /**   * Returns the unique ServerSocketChannel object   * associated with this socket, if any.   *   * The socket only has a ServerSocketChannel if its created   * by ServerSocketChannel.open.   *    * @since 1.4   */  public ServerSocketChannel getChannel()  {    return null;  }  /**   * Returns true when the socket is bound, otherwise false   *    * @since 1.4   */  public boolean isBound()  {    return bound;  }  /**   * Returns true if the socket is closed, otherwise false   *    * @since 1.4   */  public boolean isClosed()  {    return impl == null;  }  /**   * Sets the value of SO_TIMEOUT.  A value of 0 implies that SO_TIMEOUT is   * disabled (ie, operations never time out).  This is the number of    * milliseconds a socket operation can block before an   * InterruptedIOException is thrown.   *   * @param timeout The new SO_TIMEOUT value   *   * @exception SocketException If an error occurs   *   * @since 1.1   */  public void setSoTimeout (int timeout) throws SocketException  {    if (isClosed())      throw new SocketException("ServerSocket is closed");        if (timeout < 0)      throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");    impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));  }  /**   * Retrieves the current value of the SO_TIMEOUT setting.  A value of 0   * implies that SO_TIMEOUT is disabled (ie, operations never time out).   * This is the number of milliseconds a socket operation can block before   * an InterruptedIOException is thrown.   *   * @return The value of SO_TIMEOUT   *   * @exception IOException If an error occurs   *   * @since 1.1   */  public int getSoTimeout () throws IOException  {    if (isClosed())      throw new SocketException("ServerSocket is closed");        Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);    if (!(timeout instanceof Integer))      throw new IOException("Internal Error");    return ((Integer)timeout).intValue();  }  /**   * Enables/Disables the SO_REUSEADDR option   *    * @exception SocketException If an error occurs   *    * @since 1.4   */  public void setReuseAddress (boolean on)    throws SocketException  {    if (isClosed())      throw new SocketException("ServerSocket is closed");        impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));  }  /**   * Checks if the SO_REUSEADDR option is enabled   *    * @exception SocketException If an error occurs   *    * @since 1.4   */  public boolean getReuseAddress()    throws SocketException  {    if (isClosed())      throw new SocketException("ServerSocket is closed");        Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR);    if (!(reuseaddr instanceof Boolean))      throw new SocketException ("Internal Error");        return ((Boolean) reuseaddr).booleanValue ();  }  /**   * This method sets the value for the system level socket option   * SO_RCVBUF to the specified value.  Note that valid values for this   * option are specific to a given operating system.   *    * @param size The new receive buffer size.   *    * @exception SocketException If an error occurs or Socket is not connected   * @exception IllegalArgumentException If size is 0 or negative   *   * @since 1.4   */  public void setReceiveBufferSize (int size)    throws SocketException  {    if (isClosed())      throw new SocketException("ServerSocket is closed");        if (size <= 0)      throw new IllegalArgumentException ("SO_RCVBUF value must be > 0");    impl.setOption (SocketOptions.SO_RCVBUF, new Integer (size));  }  /**   * This method returns the value of the system level socket option   * SO_RCVBUF, which is used by the operating system to tune buffer   * sizes for data transfers.   *    * @return The receive buffer size.   *                * @exception SocketException If an error occurs or Socket is not connected   *    * @since 1.4   */  public int getReceiveBufferSize ()    throws SocketException  {    if (isClosed())      throw new SocketException("ServerSocket is closed");        Object buf = impl.getOption (SocketOptions.SO_RCVBUF);    if (!(buf instanceof Integer))      throw new SocketException ("Internal Error: Unexpected type");        return ((Integer) buf).intValue ();  }  /**   * Returns the value of this socket as a <code>String</code>.    *   * @return This socket represented as a <code>String</code>.   */  public String toString ()  {    if (!isBound())      return "ServerSocket[unbound]";        return ("ServerSocket[addr=" + getInetAddress()	    + ",port=" + impl.getPort()	    + ",localport=" + impl.getLocalPort()	    + "]");  }  /**   * Sets the <code>SocketImplFactory</code> for all    * <code>ServerSocket</code>'s.  This may only be done   * once per virtual machine.  Subsequent attempts will generate an   * exception.  Note that a <code>SecurityManager</code> check is made prior   * to setting the factory.  If insufficient privileges exist to set the   * factory, an exception will be thrown   *   * @exception SecurityException If this operation is not allowed by the   * <code>SecurityManager</code>.   * @exception SocketException If the factory object is already defined   * @exception IOException If any other error occurs   */  public static synchronized void setSocketFactory (SocketImplFactory fac)    throws IOException  {    factory = fac;  }}

⌨️ 快捷键说明

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