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

📄 socket.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    if (isClosed())      throw new SocketException("socket is closed");    Object timeout = getImpl().getOption(SocketOptions.SO_TIMEOUT);    if (timeout instanceof Integer)      return (((Integer) timeout).intValue());    else      return 0;  }  /**   * This method sets the value for the system level socket option   * SO_SNDBUF to the specified value.  Note that valid values for this   * option are specific to a given operating system.   *   * @param size The new send buffer size.   *   * @exception SocketException If an error occurs or Socket not connected   * @exception IllegalArgumentException If size is 0 or negative   *   * @since 1.2   */  public void setSendBufferSize(int size) throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    if (size <= 0)      throw new IllegalArgumentException("SO_SNDBUF value must be > 0");    getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));  }  /**   * This method returns the value of the system level socket option   * SO_SNDBUF, which is used by the operating system to tune buffer   * sizes for data transfers.   *   * @return The send buffer size.   *   * @exception SocketException If an error occurs or socket not connected   *   * @since 1.2   */  public int getSendBufferSize() throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    Object buf = getImpl().getOption(SocketOptions.SO_SNDBUF);    if (buf instanceof Integer)      return (((Integer) buf).intValue());    else      throw new SocketException("Internal Error: Unexpected type");  }  /**   * 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.2   */  public void setReceiveBufferSize(int size) throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    if (size <= 0)      throw new IllegalArgumentException("SO_RCVBUF value must be > 0");    getImpl().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.2   */  public int getReceiveBufferSize() throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    Object buf = getImpl().getOption(SocketOptions.SO_RCVBUF);    if (buf instanceof Integer)      return (((Integer) buf).intValue());    else      throw new SocketException("Internal Error: Unexpected type");  }  /**   * This method sets the value for the socket level socket option   * SO_KEEPALIVE.   *   * @param on True if SO_KEEPALIVE should be enabled   *   * @exception SocketException If an error occurs or Socket is not connected   *   * @since 1.3   */  public void setKeepAlive(boolean on) throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    getImpl().setOption(SocketOptions.SO_KEEPALIVE, Boolean.valueOf(on));  }  /**   * This method returns the value of the socket level socket option   * SO_KEEPALIVE.   *   * @return The setting   *   * @exception SocketException If an error occurs or Socket is not connected   *   * @since 1.3   */  public boolean getKeepAlive() throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    Object buf = getImpl().getOption(SocketOptions.SO_KEEPALIVE);    if (buf instanceof Boolean)      return (((Boolean) buf).booleanValue());    else      throw new SocketException("Internal Error: Unexpected type");  }  /**   * Closes the socket.   *   * @exception IOException If an error occurs   */  public synchronized void close() throws IOException  {    if (isClosed())      return;    getImpl().close();    impl = null;    bound = false;    if (getChannel() != null)      getChannel().close();  }  /**   * Converts this <code>Socket</code> to a <code>String</code>.   *   * @return The <code>String</code> representation of this <code>Socket</code>   */  public String toString()  {    try      {	if (isConnected())	  return ("Socket[addr=" + getImpl().getInetAddress() + ",port="	         + getImpl().getPort() + ",localport="	         + getImpl().getLocalPort() + "]");      }    catch (SocketException e)      {	// This cannot happen as we are connected.      }    return "Socket[unconnected]";  }  /**   * Sets the <code>SocketImplFactory</code>.  This may be done only once per   * virtual machine.  Subsequent attempts will generate a   * <code>SocketException</code>.  Note that a <code>SecurityManager</code>   * check is made prior to setting the factory.  If   * insufficient privileges exist to set the factory, then an   * <code>IOException</code> will be thrown.   *   * @param fac the factory to set   *   * @exception SecurityException If the <code>SecurityManager</code> does   * not allow this operation.   * @exception SocketException If the SocketImplFactory is already defined   * @exception IOException If any other error occurs   */  public static synchronized void setSocketImplFactory(SocketImplFactory fac)    throws IOException  {    // See if already set    if (factory != null)      throw new SocketException("SocketImplFactory already defined");    // Check permissions    SecurityManager sm = System.getSecurityManager();    if (sm != null)      sm.checkSetFactory();    if (fac == null)      throw new SocketException("SocketImplFactory cannot be null");    factory = fac;  }  /**   * Closes the input side of the socket stream.   *   * @exception IOException If an error occurs.   *   * @since 1.3   */  public void shutdownInput() throws IOException  {    if (isClosed())      throw new SocketException("socket is closed");    getImpl().shutdownInput();    inputShutdown = true;  }  /**   * Closes the output side of the socket stream.   *   * @exception IOException If an error occurs.   *   * @since 1.3   */  public void shutdownOutput() throws IOException  {    if (isClosed())      throw new SocketException("socket is closed");    getImpl().shutdownOutput();    outputShutdown = true;  }  /**   * Returns the socket channel associated with this socket.   *   * @return the associated socket channel,   * null if no associated channel exists   *   * @since 1.4   */  public SocketChannel getChannel()  {    return null;  }  /**   * Checks if the SO_REUSEADDR option is enabled   *   * @return True if SO_REUSEADDR is set, 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 reuseaddr = getImpl().getOption(SocketOptions.SO_REUSEADDR);    if (! (reuseaddr instanceof Boolean))      throw new SocketException("Internal Error");    return ((Boolean) reuseaddr).booleanValue();  }  /**   * Enables/Disables the SO_REUSEADDR option   *   * @param reuseAddress true if SO_REUSEADDR should be enabled,   * false otherwise   *   * @exception SocketException If an error occurs   *   * @since 1.4   */  public void setReuseAddress(boolean reuseAddress) throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    getImpl().setOption(SocketOptions.SO_REUSEADDR,                        Boolean.valueOf(reuseAddress));  }  /**   * Returns the current traffic class   *   * @return The current traffic class.   *   * @exception SocketException If an error occurs   *   * @see Socket#setTrafficClass(int tc)   *   * @since 1.4   */  public int getTrafficClass() throws SocketException  {    if (isClosed())      throw new SocketException("socket is closed");    Object obj = getImpl().getOption(SocketOptions.IP_TOS);    if (obj instanceof Integer)      return ((Integer) obj).intValue();    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 Socket#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));  }  /**   * Checks if the socket is connected   *   * @return True if socket is connected, false otherwise.   *   * @since 1.4   */  public boolean isConnected()  {    try      {	if (getImpl() == null)	  return false;	return getImpl().getInetAddress() != null;      }    catch (SocketException e)      {	return false;      }  }  /**   * Checks if the socket is already bound.   *   * @return True if socket is bound, false otherwise.   *   * @since 1.4   */  public boolean isBound()  {    return bound;  }  /**   * Checks if the socket is closed.   *   * @return True if socket is closed, false otherwise.   *   * @since 1.4   */  public boolean isClosed()  {    return impl == null;  }  /**   * Checks if the socket's input stream is shutdown   *   * @return True if input is shut down.   *   * @since 1.4   */  public boolean isInputShutdown()  {    return inputShutdown;  }  /**   * Checks if the socket's output stream is shutdown   *   * @return True if output is shut down.   *   * @since 1.4   */  public boolean isOutputShutdown()  {    return outputShutdown;  }}

⌨️ 快捷键说明

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