📄 socket.java
字号:
*/ public int getLocalPort () { if (impl != null) return impl.getLocalPort(); return -1; } /** * Returns an InputStream for reading from this socket. * * @return The InputStream object * * @exception IOException If an error occurs or Socket is not connected */ public InputStream getInputStream () throws IOException { if (impl != null) return(impl.getInputStream()); throw new IOException("Not connected"); } /** * Returns an OutputStream for writing to this socket. * * @return The OutputStream object * * @exception IOException If an error occurs or Socket is not connected */ public OutputStream getOutputStream () throws IOException { if (impl != null) return impl.getOutputStream(); throw new IOException("Not connected"); } /** * Sets the TCP_NODELAY option on the socket. * * @param on true to enable, false to disable * * @exception SocketException If an error occurs or Socket is not connected */ public void setTcpNoDelay (boolean on) throws SocketException { if (impl == null) throw new SocketException("Not connected"); impl.setOption(SocketOptions.TCP_NODELAY, new Boolean(on)); } /** * Tests whether or not the TCP_NODELAY option is set on the socket. * Returns true if enabled, false if disabled. When on it disables the * Nagle algorithm which means that packets are always send immediatly and * never merged together to reduce network trafic. * * @return Whether or not TCP_NODELAY is set * * @exception SocketException If an error occurs or Socket not connected */ public boolean getTcpNoDelay() throws SocketException { if (impl == null) throw new SocketException("Not connected"); Object on = impl.getOption(SocketOptions.TCP_NODELAY); if (on instanceof Boolean) return(((Boolean)on).booleanValue()); else throw new SocketException("Internal Error"); } /** * Sets the value of the SO_LINGER option on the socket. If the * SO_LINGER option is set on a socket and there is still data waiting to * be sent when the socket is closed, then the close operation will block * until either that data is delivered or until the timeout period * expires. The linger interval is specified in hundreths of a second * (platform specific?) * * @param on true to enable SO_LINGER, false to disable * @param linger The SO_LINGER timeout in hundreths of a second or -1 if * SO_LINGER not set. * * @exception SocketException If an error occurs or Socket not connected */ public void setSoLinger(boolean on, int linger) throws SocketException { if (impl == null) throw new SocketException("No socket created"); if (on == true) { if (linger < 0) throw new IllegalArgumentException("SO_LINGER must be >= 0"); if (linger > 65535) linger = 65535; impl.setOption(SocketOptions.SO_LINGER, new Integer(linger)); } else { impl.setOption(SocketOptions.SO_LINGER, new Boolean(false)); } } /** * Returns the value of the SO_LINGER option on the socket. If the * SO_LINGER option is set on a socket and there is still data waiting to * be sent when the socket is closed, then the close operation will block * until either that data is delivered or until the timeout period * expires. This method either returns the timeouts (in hundredths of * of a second (platform specific?)) if SO_LINGER is set, or -1 if * SO_LINGER is not set. * * @return The SO_LINGER timeout in hundreths of a second or -1 * if SO_LINGER not set * * @exception SocketException If an error occurs or Socket is not connected */ public int getSoLinger() throws SocketException { if (impl == null) throw new SocketException("Not connected"); Object linger = impl.getOption(SocketOptions.SO_LINGER); if (linger instanceof Integer) return(((Integer)linger).intValue()); else return -1; } /** * Sets the value of the SO_TIMEOUT option on the socket. If this value * is set, and an read/write is performed that does not complete within * the timeout period, a short count is returned (or an EWOULDBLOCK signal * would be sent in Unix if no data had been read). A value of 0 for * this option implies that there is no timeout (ie, operations will * block forever). On systems that have separate read and write timeout * values, this method returns the read timeout. This * value is in thousandths of a second (****????*****) * * @param timeout The length of the timeout in thousandth's of a second or * 0 if not set * * @exception SocketException If an error occurs or Socket not connected */ public synchronized void setSoTimeout (int timeout) throws SocketException { if (impl == null) throw new SocketException("Not connected"); if (timeout < 0) throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0"); impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout)); } /** * Returns the value of the SO_TIMEOUT option on the socket. If this value * is set, and an read/write is performed that does not complete within * the timeout period, a short count is returned (or an EWOULDBLOCK signal * would be sent in Unix if no data had been read). A value of 0 for * this option implies that there is no timeout (ie, operations will * block forever). On systems that have separate read and write timeout * values, this method returns the read timeout. This * value is in thousandths of a second (implementation specific?). * * @return The length of the timeout in thousandth's of a second or 0 * if not set * * @exception SocketException If an error occurs or Socket not connected */ public synchronized int getSoTimeout () throws SocketException { if (impl == null) throw new SocketException("Not connected"); 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 * * @since Java 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 Java 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 * * @since Java 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 Java 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"); } /** * Closes the socket. * * @exception IOException If an error occurs */ public synchronized void close () throws IOException { if (impl != null) impl.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 () { 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(); factory = fac; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -