📄 socket.java
字号:
/* Socket.java -- Client socket implementation Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version. GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.net;import java.io.*;import java.nio.channels.SocketChannel;import java.nio.channels.IllegalBlockingModeException;/* Written using on-line Java Platform 1.2 API Specification. * Status: I believe all methods are implemented. *//** * This class models a client site socket. A socket is a TCP/IP endpoint * for network communications conceptually similar to a file handle. * <p> * This class does not actually do any work. Instead, it redirects all of * its calls to a socket implementation object which implements the * <code>SocketImpl</code> interface. The implementation class is * instantiated by factory class that implements the * <code>SocketImplFactory interface</code>. A default * factory is provided, however the factory may be set by a call to * the <code>setSocketImplFactory</code> method. Note that this may only be * done once per virtual machine. If a subsequent attempt is made to set the * factory, a <code>SocketException</code> will be thrown. * * @author Aaron M. Renn (arenn@urbanophile.com) * @author Per Bothner (bothner@cygnus.com) */public class Socket{ // Class Variables /** * This is the user SocketImplFactory for this class. If this variable is * null, a default factory is used. */ static SocketImplFactory factory; // Instance Variables /** * The implementation object to which calls are redirected */ SocketImpl impl; private boolean inputShutdown; private boolean outputShutdown; SocketChannel ch; // this field must have been set if created by SocketChannel private boolean closed = false; // Constructors /** * Initializes a new instance of <code>Socket</code> object without * connecting to a remote host. This useful for subclasses of socket that * might want this behavior. * * @specnote This constructor is public since JDK 1.4 * @since 1.1 */ public Socket () { if (factory != null) impl = factory.createSocketImpl(); else impl = new PlainSocketImpl(); inputShutdown = false; outputShutdown = false; } /** * Initializes a new instance of <code>Socket</code> object without * connecting to a remote host. This is useful for subclasses of socket * that might want this behavior. * <p> * Additionally, this socket will be created using the supplied * implementation class instead the default class or one returned by a * factory. This value can be <code>null</code>, but if it is, all instance * methods in <code>Socket</code> should be overridden because most of them * rely on this value being populated. * * @param impl The <code>SocketImpl</code> to use for this * <code>Socket</code> * * @exception SocketException If an error occurs * * @since 1.1 */ protected Socket (SocketImpl impl) throws SocketException { this.impl = impl; this.inputShutdown = false; this.outputShutdown = false; } /** * Initializes a new instance of <code>Socket</code> and connects to the * hostname and port specified as arguments. * * @param host The name of the host to connect to * @param port The port number to connect to * * @exception UnknownHostException If the hostname cannot be resolved to a * network address. * @exception IOException If an error occurs * @exception SecurityException If a security manager exists and its * checkConnect method doesn't allow the operation */ public Socket (String host, int port) throws UnknownHostException, IOException { this(InetAddress.getByName(host), port, null, 0, true); } /** * Initializes a new instance of <code>Socket</code> and connects to the * address and port number specified as arguments. * * @param address The address to connect to * @param port The port number to connect to * * @exception IOException If an error occurs * @exception SecurityException If a security manager exists and its * checkConnect method doesn't allow the operation */ public Socket (InetAddress address, int port) throws IOException { this(address, port, null, 0, true); } /** * Initializes a new instance of <code>Socket</code> that connects to the * named host on the specified port and binds to the specified local address * and port. * * @param host The name of the remote host to connect to. * @param port The remote port to connect to. * @param loadAddr The local address to bind to. * @param localPort The local port to bind to. * * @exception SecurityException If the <code>SecurityManager</code> * exists and does not allow a connection to the specified host/port or * binding to the specified local host/port. * @exception IOException If a connection error occurs. * * @since 1.1 */ public Socket (String host, int port, InetAddress localAddr, int localPort) throws IOException { this(InetAddress.getByName(host), port, localAddr, localPort, true); } /** * Initializes a new instance of <code>Socket</code> and connects to the * address and port number specified as arguments, plus binds to the * specified local address and port. * * @param address The remote address to connect to * @param port The remote port to connect to * @param localAddr The local address to connect to * @param localPort The local port to connect to * * @exception IOException If an error occurs * @exception SecurityException If a security manager exists and its * checkConnect method doesn't allow the operation * * @since 1.1 */ public Socket (InetAddress address, int port, InetAddress localAddr, int localPort) throws IOException { this(address, port, localAddr, localPort, true); } /** * Initializes a new instance of <code>Socket</code> and connects to the * hostname and port specified as arguments. If the stream argument is set * to <code>true</code>, then a stream socket is created. If it is * <code>false</code>, a datagram socket is created. * * @param host The name of the host to connect to * @param port The port to connect to * @param stream <code>true</code> for a stream socket, <code>false</code> * for a datagram socket * * @exception IOException If an error occurs * @exception SecurityException If a security manager exists and its * checkConnect method doesn't allow the operation * * @deprecated Use the <code>DatagramSocket</code> class to create * datagram oriented sockets. */ public Socket (String host, int port, boolean stream) throws IOException { this(InetAddress.getByName(host), port, null, 0, stream); } /** * Initializes a new instance of <code>Socket</code> and connects to the * address and port number specified as arguments. If the stream param is * <code>true</code>, a stream socket will be created, otherwise a datagram * socket is created. * * @param host The address to connect to * @param port The port number to connect to * @param stream <code>true</code> to create a stream socket, * <code>false</code> to create a datagram socket. * * @exception IOException If an error occurs * @exception SecurityException If a security manager exists and its * checkConnect method doesn't allow the operation * * @deprecated Use the <code>DatagramSocket</code> class to create * datagram oriented sockets. */ public Socket (InetAddress host, int port, boolean stream) throws IOException { this(host, port, null, 0, stream); } /** * This constructor is where the real work takes place. Connect to the * specified address and port. Use default local values if not specified, * otherwise use the local host and port passed in. Create as stream or * datagram based on "stream" argument. * <p> * * @param raddr The remote address to connect to * @param rport The remote port to connect to * @param laddr The local address to connect to * @param lport The local port to connect to * @param stream true for a stream socket, false for a datagram socket * * @exception IOException If an error occurs * @exception SecurityException If a security manager exists and its * checkConnect method doesn't allow the operation */ private Socket(InetAddress raddr, int rport, InetAddress laddr, int lport, boolean stream) throws IOException { this(); this.inputShutdown = false; this.outputShutdown = false; if (impl == null) throw new IOException("Cannot initialize Socket implementation"); SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkConnect(raddr.getHostName(), rport); impl.create(stream); // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port, // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as // that default. JDK 1.2 doc infers not to do a bind. if (laddr != null) impl.bind(laddr, lport); if (raddr != null) impl.connect(raddr, rport); } /** * Binds the socket to the givent local address/port * * @param bindpoint The address/port to bind to * * @exception IOException If an error occurs * @exception SecurityException If a security manager exists and its * checkConnect method doesn't allow the operation * @exception IllegalArgumentException If the address type is not supported * * @since 1.4 */ public void bind (SocketAddress bindpoint) throws IOException { if (closed) throw new SocketException ("Socket is closed"); if ( !(bindpoint instanceof InetSocketAddress)) throw new IllegalArgumentException (); InetSocketAddress tmp = (InetSocketAddress) bindpoint; impl.bind (tmp.getAddress(), tmp.getPort()); } /** * Connects the socket with a remote address. * * @param endpoint The address to connect to * * @exception IOException If an error occurs * @exception IllegalArgumentException If the addess type is not supported * @exception IllegalBlockingModeException If this socket has an associated * channel, and the channel is in non-blocking mode * * @since 1.4 */ public void connect (SocketAddress endpoint) throws IOException { if (closed) throw new SocketException ("Socket is closed"); if (! (endpoint instanceof InetSocketAddress)) throw new IllegalArgumentException ("Address type not supported"); if (ch != null && !ch.isBlocking ()) throw new IllegalBlockingModeException (); impl.connect (endpoint, 0); } /** * Connects the socket with a remote address. A timeout of zero is * interpreted as an infinite timeout. The connection will then block * until established or an error occurs. * * @param endpoint The address to connect to * * @exception IOException If an error occurs * @exception IllegalArgumentException If the address type is not supported * @exception IllegalBlockingModeException If this socket has an associated * channel, and the channel is in non-blocking mode * @exception SocketTimeoutException If the timeout is reached * * @since 1.4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -