📄 socket.java
字号:
/* * @(#)Socket.java 1.63 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.net;import java.io.InputStream;import java.io.OutputStream;import java.io.IOException;import java.io.InterruptedIOException;import java.security.AccessController;import java.security.PrivilegedExceptionAction;/** * This class implements client sockets (also called just * "sockets"). A socket is an endpoint for communication * between two machines. * <p> * The actual work of the socket is performed by an instance of the * <code>SocketImpl</code> class. An application, by changing * the socket factory that creates the socket implementation, * can configure itself to create sockets appropriate to the local * firewall. * * @author unascribed * @version 1.56, 08/09/01 * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) * @see java.net.SocketImpl * @since JDK1.0 */publicclass Socket { /** * Various states of this socket. */ private boolean created = false; private boolean bound = false; private boolean connected = false; private boolean closed = false; private Object closeLock = new Object(); private boolean shutIn = false; private boolean shutOut = false; /** * The implementation of this Socket. */ SocketImpl impl; /** * Are we using an older SocketImpl? */ private boolean oldImpl = false; /** * Creates an unconnected socket, with the * system-default type of SocketImpl. * * @since JDK1.1 * @revised 1.4 */ public Socket() { setImpl(); } /** * Creates an unconnected Socket with a user-specified * SocketImpl. * <P> * @param impl an instance of a <B>SocketImpl</B> * the subclass wishes to use on the Socket. * * @exception SocketException if there is an error in the underlying protocol, * such as a TCP error. * @since JDK1.1 */ protected Socket(SocketImpl impl) throws SocketException { this.impl = impl; if (impl != null) { checkOldImpl(); this.impl.setSocket(this); } } /** * Creates a stream socket and connects it to the specified port * number on the named host. * <p> * If the specified host is <tt>null</tt> it is the equivalent of * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>. * In other words, it is equivalent to specifying an address of the * loopback interface. </p> * <p> * If the application has specified a server socket factory, that * factory's <code>createSocketImpl</code> method is called to create * the actual socket implementation. Otherwise a "plain" socket is created. * <p> * If there is a security manager, its * <code>checkConnect</code> method is called * with the host address and <code>port</code> * as its arguments. This could result in a SecurityException. * * @param host the host name, or <code>null</code> for the loopback address. * @param port the port number. * * @exception UnknownHostException if the IP address of * the host could not be determined. * * @exception IOException if an I/O error occurs when creating the socket. * @exception SecurityException if a security manager exists and its * <code>checkConnect</code> method doesn't allow the operation. * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) * @see java.net.SocketImpl * @see java.net.SocketImplFactory#createSocketImpl() * @see SecurityManager#checkConnect */ public Socket(String host, int port) throws UnknownHostException, IOException { this(host != null ? new InetSocketAddress(host, port) : new InetSocketAddress(InetAddress.getByName(null), port), new InetSocketAddress(0), true); } /** * Creates a stream socket and connects it to the specified port * number at the specified IP address. * <p> * If the application has specified a socket factory, that factory's * <code>createSocketImpl</code> method is called to create the * actual socket implementation. Otherwise a "plain" socket is created. * <p> * If there is a security manager, its * <code>checkConnect</code> method is called * with the host address and <code>port</code> * as its arguments. This could result in a SecurityException. * * @param address the IP address. * @param port the port number. * @exception IOException if an I/O error occurs when creating the socket. * @exception SecurityException if a security manager exists and its * <code>checkConnect</code> method doesn't allow the operation. * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) * @see java.net.SocketImpl * @see java.net.SocketImplFactory#createSocketImpl() * @see SecurityManager#checkConnect */ public Socket(InetAddress address, int port) throws IOException { this(address != null ? new InetSocketAddress(address, port) : null, new InetSocketAddress(0), true); } /** * Creates a socket and connects it to the specified remote host on * the specified remote port. The Socket will also bind() to the local * address and port supplied. * <p> * If the specified host is <tt>null</tt> it is the equivalent of * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>. * In other words, it is equivalent to specifying an address of the * loopback interface. </p> * <p> * If there is a security manager, its * <code>checkConnect</code> method is called * with the host address and <code>port</code> * as its arguments. This could result in a SecurityException. * * @param host the name of the remote host, or <code>null</code> for the loopback address. * @param port the remote port * @param localAddr the local address the socket is bound to * @param localPort the local port the socket is bound to * @exception IOException if an I/O error occurs when creating the socket. * @exception SecurityException if a security manager exists and its * <code>checkConnect</code> method doesn't allow the operation. * @see SecurityManager#checkConnect * @since JDK1.1 */ public Socket(String host, int port, InetAddress localAddr, int localPort) throws IOException { this(host != null ? new InetSocketAddress(host, port) : new InetSocketAddress(InetAddress.getByName(null), port), new InetSocketAddress(localAddr, localPort), true); } /** * Creates a socket and connects it to the specified remote address on * the specified remote port. The Socket will also bind() to the local * address and port supplied. * <p> * If there is a security manager, its * <code>checkConnect</code> method is called * with the host address and <code>port</code> * as its arguments. This could result in a SecurityException. * * @param address the remote address * @param port the remote port * @param localAddr the local address the socket is bound to * @param localPort the local port the socket is bound to * @exception IOException if an I/O error occurs when creating the socket. * @exception SecurityException if a security manager exists and its * <code>checkConnect</code> method doesn't allow the operation. * @see SecurityManager#checkConnect * @since JDK1.1 */ public Socket(InetAddress address, int port, InetAddress localAddr, int localPort) throws IOException { this(address != null ? new InetSocketAddress(address, port) : null, new InetSocketAddress(localAddr, localPort), true); } /** * Creates a stream socket and connects it to the specified port * number on the named host. * <p> * If the specified host is <tt>null</tt> it is the equivalent of * specifying the address as <tt>{@link java.net.InetAddress#getByName InetAddress.getByName}(null)</tt>. * In other words, it is equivalent to specifying an address of the * loopback interface. </p> * <p> * If the stream argument is <code>true</code>, this creates a * stream socket. If the stream argument is <code>false</code>, it * creates a datagram socket. * <p> * If the application has specified a server socket factory, that * factory's <code>createSocketImpl</code> method is called to create * the actual socket implementation. Otherwise a "plain" socket is created. * <p> * If there is a security manager, its * <code>checkConnect</code> method is called * with the host address and <code>port</code> * as its arguments. This could result in a SecurityException. * <p> * If a UDP socket is used, TCP/IP related socket options will not apply. * * param host the host name. * param port the port number. * param stream a <code>boolean</code> indicating whether this is * a stream socket or a datagram socket. * exception IOException if an I/O error occurs when creating the socket. * exception SecurityException if a security manager exists and its * <code>checkConnect</code> method doesn't allow the operation. * see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) * see java.net.SocketImpl * see java.net.SocketImplFactory#createSocketImpl() * see SecurityManager#checkConnect * deprecated Use DatagramSocket instead for UDP transport. * public Socket(String host, int port, boolean stream) throws IOException { this(host != null ? new InetSocketAddress(host, port) : new InetSocketAddress(InetAddress.getByName(null), port), new InetSocketAddress(0), stream); } */ /** * Creates a socket and connects it to the specified port number at * the specified IP address. * <p> * If the stream argument is <code>true</code>, this creates a * stream socket. If the stream argument is <code>false</code>, it * creates a datagram socket. * <p> * If the application has specified a server socket factory, that * factory's <code>createSocketImpl</code> method is called to create * the actual socket implementation. Otherwise a "plain" socket is created. * * <p>If there is a security manager, its * <code>checkConnect</code> method is called * with <code>host.getHostAddress()</code> and <code>port</code> * as its arguments. This could result in a SecurityException. * <p> * If UDP socket is used, TCP/IP related socket options will not apply. * * param host the IP address. * param port the port number. * param stream if <code>true</code>, create a stream socket; * otherwise, create a datagram socket. * exception IOException if an I/O error occurs when creating the socket. * exception SecurityException if a security manager exists and its * <code>checkConnect</code> method doesn't allow the operation. * see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) * see java.net.SocketImpl * see java.net.SocketImplFactory#createSocketImpl() * see SecurityManager#checkConnect * deprecated Use DatagramSocket instead for UDP transport. * public Socket(InetAddress host, int port, boolean stream) throws IOException { this(host != null ? new InetSocketAddress(host, port) : null, new InetSocketAddress(0), stream); } */ private Socket(SocketAddress address, SocketAddress localAddr, boolean stream) throws IOException { setImpl(); // backward compatibility if (address == null) throw new NullPointerException(); try { createImpl(stream); if (localAddr == null) localAddr = new InetSocketAddress(0); bind(localAddr); if (address != null) connect(address); } catch (SocketException e) { close(); throw e; } } /** * Creates the socket implementation. * * @param stream a <code>boolean</code> value : <code>true</code> for a TCP socket, * <code>false</code> for UDP. * @throws IOException if creation fails * @since 1.4 */ void createImpl(boolean stream) throws SocketException { if (impl == null) setImpl(); try { impl.create(stream); created = true; } catch (IOException e) { throw new SocketException(e.getMessage()); } } private void checkOldImpl() { if (impl == null) return; // SocketImpl.connect() is a protected method, therefore we need to use // getDeclaredMethod, therefore we need permission to access the member try { AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() throws NoSuchMethodException { Class[] cl = new Class[2]; cl[0] = SocketAddress.class; cl[1] = Integer.TYPE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -