📄 socket.java
字号:
* @return a <code>boolean</code> indicating whether or not SO_KEEPALIVE is enabled. * @exception SocketException if there is an error * in the underlying protocol, such as a TCP error. * @since 1.3 * @see #setKeepAlive(boolean) */ public boolean getKeepAlive() throws SocketException { if (isClosed()) throw new SocketException("Socket is closed"); return ((Boolean) getImpl().getOption(SocketOptions.SO_KEEPALIVE)).booleanValue(); } /** * Sets traffic class or type-of-service octet in the IP * header for packets sent from this Socket. * As the underlying network implementation may ignore this * value applications should consider it a hint. * * <P> The tc <B>must</B> be in the range <code> 0 <= tc <= * 255</code> or an IllegalArgumentException will be thrown. * <p>Notes: * <p> for Internet Protocol v4 the value consists of an octet * with precedence and TOS fields as detailed in RFC 1349. The * TOS field is bitset created by bitwise-or'ing values such * the following :- * <p> * <UL> * <LI><CODE>IPTOS_LOWCOST (0x02)</CODE></LI> * <LI><CODE>IPTOS_RELIABILITY (0x04)</CODE></LI> * <LI><CODE>IPTOS_THROUGHPUT (0x08)</CODE></LI> * <LI><CODE>IPTOS_LOWDELAY (0x10)</CODE></LI> * </UL> * The last low order bit is always ignored as this * corresponds to the MBZ (must be zero) bit. * <p> * Setting bits in the precedence field may result in a * SocketException indicating that the operation is not * permitted. * <p> * for Internet Protocol v6 <code>tc</code> is the value that * would be placed into the sin6_flowinfo field of the IP header. * * @param tc an <code>int</code> value for the bitset. * @throws SocketException if there is an error setting the * traffic class or type-of-service * @since 1.4 * @see #getTrafficClass */ public void setTrafficClass(int tc) throws SocketException { if (tc < 0 || tc > 255) throw new IllegalArgumentException("tc is not in range 0 -- 255"); if (isClosed()) throw new SocketException("Socket is closed"); getImpl().setOption(SocketOptions.IP_TOS, new Integer(tc)); } /** * Gets traffic class or type-of-service in the IP header * for packets sent from this Socket * <p> * As the underlying network implementation may ignore the * traffic class or type-of-service set using {@link #setTrafficClass()} * this method may return a different value than was previously * set using the {@link #setTrafficClass()} method on this Socket. * * @return the traffic class or type-of-service already set * @throws SocketException if there is an error obtaining the * traffic class or type-of-service value. * @since 1.4 * @see #setTrafficClass */ public int getTrafficClass() throws SocketException { return ((Integer) (getImpl().getOption(SocketOptions.IP_TOS))).intValue(); } /** * Enable/disable the SO_REUSEADDR socket option. * <p> * When a TCP connection is closed the connection may remain * in a timeout state for a period of time after the connection * is closed (typically known as the <tt>TIME_WAIT</tt> state * or <tt>2MSL</tt> wait state). * For applications using a well known socket address or port * it may not be possible to bind a socket to the required * <tt>SocketAddress</tt> if there is a connection in the * timeout state involving the socket address or port. * <p> * Enabling <tt>SO_REUSEADDR</tt> prior to binding the socket * using {@link #bind(SocketAddress)} allows the socket to be * bound even though a previous connection is in a timeout * state. * <p> * When a <tt>Socket</tt> is created the initial setting * of <tt>SO_REUSEADDR</tt> is disabled. * <p> * The behaviour when <tt>SO_REUSEADDR</tt> is enabled or * disabled after a socket is bound (See {@link #isBound()}) * is not defined. * * @param on whether to enable or disable the socket option * @exception SocketException if an error occurs enabling or * disabling the <tt>SO_RESUEADDR</tt> socket option, * or the socket is closed. * @since 1.4 * @see #getReuseAddress() * @see #bind(SocketAddress) * @see #isClosed() * @see #isBound() */ public void setReuseAddress(boolean on) throws SocketException { if (isClosed()) throw new SocketException("Socket is closed"); getImpl().setOption(SocketOptions.SO_REUSEADDR, new Boolean(on)); } /** * Tests if SO_REUSEADDR is enabled. * * @return a <code>boolean</code> indicating whether or not SO_REUSEADDR is enabled. * @exception SocketException if there is an error * in the underlying protocol, such as a TCP error. * @since 1.4 * @see #setReuseAddress(boolean) */ public boolean getReuseAddress() throws SocketException { if (isClosed()) throw new SocketException("Socket is closed"); return ((Boolean) (getImpl().getOption(SocketOptions.SO_REUSEADDR))).booleanValue(); } /** * Closes this socket. * <p> * Any thread currently blocked in an I/O operation upon this socket * will throw a {@link SocketException}. * <p> * Once a socket has been closed, it is not available for further networking * use (i.e. can't be reconnected or rebound). A new socket needs to be * created. * * <p> If this socket has an associated channel then the channel is closed * as well. * * @exception IOException if an I/O error occurs when closing this socket. * @revised 1.4 * @spec JSR-51 * @see #isClosed */ public synchronized void close() throws IOException { synchronized(closeLock) { if (isClosed()) return; if (created) impl.close(); closed = true; } } /** * Places the input stream for this socket at "end of stream". * Any data sent to the input stream side of the socket is acknowledged * and then silently discarded. * <p> * If you read from a socket input stream after invoking * shutdownInput() on the socket, the stream will return EOF. * * @exception IOException if an I/O error occurs when shutting down this * socket. * * @since 1.3 * @see java.net.Socket#shutdownOutput() * @see java.net.Socket#close() * @see java.net.Socket#setSoLinger(boolean, int) * @see #isInputShutdown */ public void shutdownInput() throws IOException { if (isClosed()) throw new SocketException("Socket is closed"); if (!isConnected()) throw new SocketException("Socket is not connected"); if (isInputShutdown()) throw new SocketException("Socket input is already shutdown"); getImpl().shutdownInput(); shutIn = true; } /** * Disables the output stream for this socket. * For a TCP socket, any previously written data will be sent * followed by TCP's normal connection termination sequence. * * If you write to a socket output stream after invoking * shutdownOutput() on the socket, the stream will throw * an IOException. * * @exception IOException if an I/O error occurs when shutting down this * socket. * * @since 1.3 * @see java.net.Socket#shutdownInput() * @see java.net.Socket#close() * @see java.net.Socket#setSoLinger(boolean, int) * @see #isOutputShutdown */ public void shutdownOutput() 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 already shutdown"); getImpl().shutdownOutput(); shutOut = true; } /** * Converts this socket to a <code>String</code>. * * @return a string representation of this socket. */ public String toString() { try { if (isConnected()) return "Socket[addr=" + getImpl().getInetAddress() + ",port=" + getImpl().getPort() + ",localport=" + getImpl().getLocalPort() + "]"; } catch (SocketException e) { } return "Socket[unconnected]"; } /** * Returns the connection state of the socket. * * @return true if the socket successfuly connected to a server * @since 1.4 */ public boolean isConnected() { // Before 1.3 Sockets were always connected during creation return connected || oldImpl; } /** * Returns the binding state of the socket. * * @return true if the socket successfuly bound to an address * @since 1.4 * @see #bind */ public boolean isBound() { // Before 1.3 Sockets were always bound during creation return bound || oldImpl; } /** * Returns the closed state of the socket. * * @return true if the socket has been closed * @since 1.4 * @see #close */ public boolean isClosed() { synchronized(closeLock) { return closed; } } /** * Returns wether the read-half of the socket connection is closed. * * @return true if the input of the socket has been shutdown * @since 1.4 * @see #shutdownInput */ public boolean isInputShutdown() { return shutIn; } /** * Returns wether the write-half of the socket connection is closed. * * @return true if the output of the socket has been shutdown * @since 1.4 * @see #shutdownOutput */ public boolean isOutputShutdown() { return shutOut; } /** * The factory for all client sockets. */ private static SocketImplFactory factory = null; private static synchronized void checkSocks() { int port = -1; String socksPort = null; String useSocks = null; if (factory == null) { useSocks = (String) AccessController.doPrivileged( new sun.security.action.GetPropertyAction("socksProxyHost")); if (useSocks == null || useSocks.length() <= 0) return; socksPort = (String) AccessController.doPrivileged( new sun.security.action.GetPropertyAction("socksProxyPort")); if (socksPort != null && socksPort.length() > 0) { try { port = Integer.parseInt(socksPort); } catch (Exception e) { port = -1; } } if (useSocks != null) factory = new SocksSocketImplFactory(useSocks, port); } else if (factory instanceof SocksSocketImplFactory) { useSocks = (String) AccessController.doPrivileged( new sun.security.action.GetPropertyAction("socksProxyHost")); if (useSocks == null || useSocks.length() <= 0) factory = null; } } /** * Sets the client socket implementation factory for the * application. The factory can be specified only once. * <p> * When an application creates a new client socket, the socket * implementation factory's <code>createSocketImpl</code> method is * called to create the actual socket implementation. * * <p>If there is a security manager, this method first calls * the security manager's <code>checkSetFactory</code> method * to ensure the operation is allowed. * This could result in a SecurityException. * * @param fac the desired factory. * @exception IOException if an I/O error occurs when setting the * socket factory. * @exception SocketException if the factory is already defined. * @exception SecurityException if a security manager exists and its * <code>checkSetFactory</code> method doesn't allow the operation. * @see java.net.SocketImplFactory#createSocketImpl() * @see SecurityManager#checkSetFactory */ public static synchronized void setSocketImplFactory(SocketImplFactory fac) throws IOException { if (factory != null) { throw new SocketException("factory already defined"); } SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkSetFactory(); } factory = fac; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -