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

📄 socket.java

📁 纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

		Object timeout = impl.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 (impl == null)
			throw new SocketException("Not connected");

		if (size <= 0)
			throw new IllegalArgumentException("SO_SNDBUF value must be > 0");

		impl.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 (impl == null)
			throw new SocketException("Not connected");

		Object buf = impl.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 (impl == null)
			throw new SocketException("Not connected");

		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.2
	 */
	public int getReceiveBufferSize() throws SocketException {
		if (impl == null)
			throw new SocketException("Not connected");

		Object buf = impl.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 (impl == null)
			throw new SocketException("Not connected");

		impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(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 (impl == null)
			throw new SocketException("Not connected");

		Object buf = impl.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 (impl != null)
			impl.close();

		if (ch != null)
			ch.close();

		closed = true;
	}

	/**
	 * Converts this <code>Socket</code> to a <code>String</code>.
	 *
	 * @return The <code>String</code> representation of this <code>Socket</code>
	 */
	public String toString() {
		return ("Socket " + impl);
	}

	// Class Methods

	/**
	 * 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.
	 *
	 * @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 (impl != null)
			impl.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 (impl != null)
			impl.shutdownOutput();

		outputShutdown = true;
	}

	/**
	 * Returns the socket channel associated with this socket.
	 *
	 * It returns null if no associated socket exists.
	 *
	 * @since 1.4
	 */
	public SocketChannel getChannel() {
		return ch;
	}

	/**
	 * Checks if the SO_REUSEADDR option 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 reuseaddr = impl.getOption(SocketOptions.SO_REUSEADDR);

		if (!(reuseaddr instanceof Boolean))
			throw new SocketException("Internal Error");

		return ((Boolean)reuseaddr).booleanValue();
	}

	/**
	 * Enables/Disables the SO_REUSEADDR option
	 *
	 * @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));
	}

	/**
	 * Returns the current traffic class
	 *
	 * @exception SocketException If an error occurs
	 *
	 * @see Socket:setTrafficClass
	 *
	 * @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 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 (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));
	}

	/**
	 * Checks if the socket is connected
	 *
	 * @since 1.4
	 */
	public boolean isConnected() {
		return impl.getInetAddress() != null;
	}

	/**
	 * Checks if the socket is already bound.
	 *
	 * @since 1.4
	 */
	public boolean isBound() {
		return getLocalAddress() != null;
	}

	/**
	 * Checks if the socket is closed.
	 * 
	 * @since 1.4
	 */
	public boolean isClosed() {
		return closed;
	}

	/**
	 * Checks if the socket's input stream is shutdown
	 *
	 * @since 1.4
	 */
	public boolean isInputShutdown() {
		return inputShutdown;
	}

	/**
	 * Checks if the socket's output stream is shutdown
	 *
	 * @since 1.4
	 */
	public boolean isOutputShutdown() {
		return outputShutdown;
	}
}

⌨️ 快捷键说明

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