socket.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,458 行 · 第 1/4 页

JAVA
1,458
字号
     * @exception  IOException  if an I/O error occurs when creating the     *               output stream or if the socket is not connected.     * @revised 1.4     * @spec JSR-51     */    public OutputStream getOutputStream() throws IOException {	if (isClosed())	    throw new SocketException("Socket is closed");	if (!isConnected())	    throw new SocketException("Socket is not connected");	if (isOutputShutdown())	    throw new SocketException("Socket output is shutdown");	final Socket s = this;	OutputStream os = null;	try {	    os = (OutputStream)		AccessController.doPrivileged(new PrivilegedExceptionAction() {		    public Object run() throws IOException {			return impl.getOutputStream();		    }		});	} catch (java.security.PrivilegedActionException e) {	    throw (IOException) e.getException();	}	return os;    }    /**     * Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).     *     * @param on <code>true</code> to enable TCP_NODELAY,      * <code>false</code> to disable.     *     * @exception SocketException if there is an error      * in the underlying protocol, such as a TCP error.     *      * @since   JDK1.1     *     * @see #getTcpNoDelay()     */    public void setTcpNoDelay(boolean on) throws SocketException {	if (isClosed())	    throw new SocketException("Socket is closed");	getImpl().setOption(SocketOptions.TCP_NODELAY, new Boolean(on));    }    /**     * Tests if TCP_NODELAY is enabled.     *     * @return a <code>boolean</code> indicating whether or not TCP_NODELAY is enabled.     * @exception SocketException if there is an error     * in the underlying protocol, such as a TCP error.      * @since   JDK1.1     * @see #setTcpNoDelay(boolean)     */    public boolean getTcpNoDelay() throws SocketException {	if (isClosed())	    throw new SocketException("Socket is closed");	return ((Boolean) getImpl().getOption(SocketOptions.TCP_NODELAY)).booleanValue();    }    /**     * Enable/disable SO_LINGER with the specified linger time in seconds.      * The maximum timeout value is platform specific.     *     * The setting only affects socket close.     *      * @param on     whether or not to linger on.     * @param linger how long to linger for, if on is true.     * @exception SocketException if there is an error     * in the underlying protocol, such as a TCP error.      * @exception IllegalArgumentException if the linger value is negative.     * @since JDK1.1     * @see #getSoLinger()     */    public void setSoLinger(boolean on, int linger) throws SocketException {	if (isClosed())	    throw new SocketException("Socket is closed");	if (!on) {	    getImpl().setOption(SocketOptions.SO_LINGER, new Boolean(on));	} else {	    if (linger < 0) {		throw new IllegalArgumentException("invalid value for SO_LINGER");	    }            if (linger > 65535)                linger = 65535;	    getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger));	}    }    /**     * Returns setting for SO_LINGER. -1 returns implies that the     * option is disabled.     *     * The setting only affects socket close.     *     * @return the setting for SO_LINGER.     * @exception SocketException if there is an error     * in the underlying protocol, such as a TCP error.      * @since   JDK1.1     * @see #setSoLinger(boolean, int)     */    public int getSoLinger() throws SocketException {	if (isClosed())	    throw new SocketException("Socket is closed");	Object o = getImpl().getOption(SocketOptions.SO_LINGER);	if (o instanceof Integer) {	    return ((Integer) o).intValue();	} else {	    return -1;	}    }    /**     * Send one byte of urgent data on the socket. The byte to be sent is the lowest eight     * bits of the data parameter. The urgent byte is     * sent after any preceding writes to the socket OutputStream     * and before any future writes to the OutputStream.     * @param data The byte of data to send     * @exception IOException if there is an error     *  sending the data.     * @since 1.4     */    public void sendUrgentData (int data) throws IOException  {        if (!getImpl().supportsUrgentData ()) {            throw new SocketException ("Urgent data not supported");        }        getImpl().sendUrgentData (data);    }    /**     * Enable/disable OOBINLINE (receipt of TCP urgent data)     *     * By default, this option is disabled and TCP urgent data received on a      * socket is silently discarded. If the user wishes to receive urgent data, then     * this option must be enabled. When enabled, urgent data is received     * inline with normal data.      * <p>     * Note, only limited support is provided for handling incoming urgent      * data. In particular, no notification of incoming urgent data is provided      * and there is no capability to distinguish between normal data and urgent     * data unless provided by a higher level protocol.     *     * @param on <code>true</code> to enable OOBINLINE,      * <code>false</code> to disable.     *     * @exception SocketException if there is an error      * in the underlying protocol, such as a TCP error.     *      * @since   1.4     *     * @see #getOOBInline()     */    public void setOOBInline(boolean on) throws SocketException {	if (isClosed())	    throw new SocketException("Socket is closed");	getImpl().setOption(SocketOptions.SO_OOBINLINE, new Boolean(on));    }    /**     * Tests if OOBINLINE is enabled.     *     * @return a <code>boolean</code> indicating whether or not OOBINLINE is enabled.     * @exception SocketException if there is an error     * in the underlying protocol, such as a TCP error.      * @since   1.4     * @see #setOOBInline(boolean)     */    public boolean getOOBInline() throws SocketException {	if (isClosed())	    throw new SocketException("Socket is closed");	return ((Boolean) getImpl().getOption(SocketOptions.SO_OOBINLINE)).booleanValue();    }    /**     *  Enable/disable SO_TIMEOUT with the specified timeout, in     *  milliseconds.  With this option set to a non-zero timeout,     *  a read() call on the InputStream associated with this Socket     *  will block for only this amount of time.  If the timeout expires,     *  a <B>java.net.SocketTimeoutException</B> is raised, though the     *  Socket is still valid. The option <B>must</B> be enabled     *  prior to entering the blocking operation to have effect. The     *  timeout must be > 0.     *  A timeout of zero is interpreted as an infinite timeout.     * @param timeout the specified timeout, in milliseconds.     * @exception SocketException if there is an error     * in the underlying protocol, such as a TCP error.      * @since   JDK 1.1     * @see #getSoTimeout()     */    public synchronized void setSoTimeout(int timeout) throws SocketException {	if (isClosed())	    throw new SocketException("Socket is closed");	if (timeout < 0)	  throw new IllegalArgumentException("timeout can't be negative");	getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));    }    /**     * Returns setting for SO_TIMEOUT.  0 returns implies that the     * option is disabled (i.e., timeout of infinity).     * @return the setting for SO_TIMEOUT     * @exception SocketException if there is an error     * in the underlying protocol, such as a TCP error.      * @since   JDK1.1     * @see #setSoTimeout(int)     */    public synchronized int getSoTimeout() throws SocketException {	if (isClosed())	    throw new SocketException("Socket is closed");	Object o = getImpl().getOption(SocketOptions.SO_TIMEOUT);	/* extra type safety */	if (o instanceof Integer) {	    return ((Integer) o).intValue();	} else {	    return 0;	}    }    /**     * Sets the SO_SNDBUF option to the specified value for this     * <tt>Socket</tt>. The SO_SNDBUF option is used by the platform's     * networking code as a hint for the size to set     * the underlying network I/O buffers.     *     * <p>Because SO_SNDBUF is a hint, applications that want to     * verify what size the buffers were set to should call     * {@link #getSendBufferSize()}.     *     * @exception SocketException if there is an error     * in the underlying protocol, such as a TCP error.      *     * @param size the size to which to set the send buffer     * size. This value must be greater than 0.     *     * @exception IllegalArgumentException if the      * value is 0 or is negative.     *     * @see #getSendBufferSize()     * @since 1.2     */    public synchronized void setSendBufferSize(int size)    throws SocketException{	if (!(size > 0)) {	    throw new IllegalArgumentException("negative send size");	}	if (isClosed())	    throw new SocketException("Socket is closed");	getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));    }    /**     * Get value of the SO_SNDBUF option for this <tt>Socket</tt>,      * that is the buffer size used by the platform      * for output on this <tt>Socket</tt>.     * @return the value of the SO_SNDBUF option for this <tt>Socket</tt>.     *     * @exception SocketException if there is an error     * in the underlying protocol, such as a TCP error.      *     * @see #setSendBufferSize(int)     * @since 1.2     */    public synchronized int getSendBufferSize() throws SocketException {	if (isClosed())	    throw new SocketException("Socket is closed");	int result = 0;	Object o = getImpl().getOption(SocketOptions.SO_SNDBUF);	if (o instanceof Integer) {	    result = ((Integer)o).intValue();	}	return result;    }    /**     * Sets the SO_RCVBUF option to the specified value for this     * <tt>Socket</tt>. The SO_RCVBUF option is used by the platform's     * networking code as a hint for the size to set     * the underlying network I/O buffers.     *     * <p>Increasing the receive buffer size can increase the performance of     * network I/O for high-volume connection, while decreasing it can     * help reduce the backlog of incoming data.      *     * <p>Because SO_RCVBUF is a hint, applications that want to     * verify what size the buffers were set to should call     * {@link #getReceiveBufferSize()}.     *     * <p>The value of SO_RCVBUF is also used to set the TCP receive window     * that is advertized to the remote peer. Generally, the window size     * can be modified at any time when a socket is connected. However, if     * a receive window larger than 64K is required then this must be requested     * <B>before</B> the socket is connected to the remote peer. There are two     * cases to be aware of:<p>     * <ol>     * <li>For sockets accepted from a ServerSocket, this must be done by calling     * {@link ServerSocket#setReceiveBufferSize(int)} before the ServerSocket      * is bound to a local address.<p></li>     * <li>For client sockets, setReceiveBufferSize() must be called before     * connecting the socket to its remote peer.<p></li></ol>     * @param size the size to which to set the receive buffer     * size. This value must be greater than 0.     *     * @exception IllegalArgumentException if the value is 0 or is     * negative.     *     * @exception SocketException if there is an error     * in the underlying protocol, such as a TCP error.     *      * @see #getReceiveBufferSize()     * @see ServerSocket#setReceiveBufferSize(int)     * @since 1.2     */    public synchronized void setReceiveBufferSize(int size)    throws SocketException{	if (size <= 0) {	    throw new IllegalArgumentException("invalid receive size");	}	if (isClosed())	    throw new SocketException("Socket is closed");	getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));    }    /**     * Gets the value of the SO_RCVBUF option for this <tt>Socket</tt>,      * that is the buffer size used by the platform for      * input on this <tt>Socket</tt>.     *     * @return the value of the SO_RCVBUF option for this <tt>Socket</tt>.     * @exception SocketException if there is an error     * in the underlying protocol, such as a TCP error.      * @see #setReceiveBufferSize(int)     * @since 1.2     */    public synchronized int getReceiveBufferSize()    throws SocketException{	if (isClosed())	    throw new SocketException("Socket is closed");	int result = 0;	Object o = getImpl().getOption(SocketOptions.SO_RCVBUF);	if (o instanceof Integer) {	    result = ((Integer)o).intValue();	}	return result;    }    /**     * Enable/disable SO_KEEPALIVE.     *      * @param on     whether or not to have socket keep alive turned on.     * @exception SocketException if there is an error     * in the underlying protocol, such as a TCP error.      * @since 1.3      * @see #getKeepAlive()     */    public void setKeepAlive(boolean on) throws SocketException {	if (isClosed())	    throw new SocketException("Socket is closed");        getImpl().setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on));    }    /**     * Tests if SO_KEEPALIVE is enabled.     *

⌨️ 快捷键说明

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