java.net.Socket
java.lang.Object
None
None
JDK 1.0 or later
The Socket class implements stream-based, connection-oriented, reliable data communication. Although Socket objects are often used with the Transmission Control Protocol, commonly known as TCP, they are independent of the actual protocol being used. The Socket class encapsulates client logic that is common to connection-oriented protocols. Sockets are two-way data pipes that are connected on either end to an address and port number. As of JDK 1.1, new constructors allow you to specify the local address and port as well as the remote address and port.
A Socket object uses an object that belongs to a subclass of the abstract class SocketImpl to access protocol-specific logic. A program can specify the subclass of SocketImpl that is used by passing an appropriate SocketImplFactory object to the setSocketImplFactory() method before any Socket objects are created. This feature allows a program to create sockets that are able to accommodate such things as firewalls or even work with different protocols.
public class java.net.Socket extends java.lang.Object { // Constructors public Socket(String host, int port); public Socket(InetAddress address, int port); public Socket(String host, int port, InetAddress localAddr, int localPort); // New in 1.1 public Socket(InetAddress address, int port, InetAddress localAddr, int localPort); // New in 1.1 public Socket(String host, int port, boolean stream); // Deprecated in 1.1 public Socket(InetAddress host, int port, boolean stream); // Deprecated in 1.1 protected Socket(); // New in 1.1 protected Socket(SocketImpl impl); // New in 1.1 // Class Methods public static synchronized void setSocketImplFactory( SocketImplFactory fac); // Instance Methods public synchronized void close(); public InetAddress getInetAddress(); public InputStream getInputStream(); public InetAddress getLocalAddress(); // New in 1.1 public int getLocalPort(); public OutputStream getOutputStream(); public int getPort(); public int getSoLinger(); // New in 1.1 public synchronized int getSoTimeout(); // New in 1.1 public boolean getTcpNoDelay(); // New in 1.1 public void setSoLinger(boolean on, int val); // New in 1.1 public synchronized void setSoTimeout(int timeout); // New in 1.1 public void setTcpNoDelay(boolean on); // New in 1.1 public String toString(); }
public Socket(String host, int port) throws IOException, UnknownHostException
The name of a remote machine.
A port on a remote machine.
If any kind of I/O error occurs.
If the application is not allowed to connect to the given host and port.
If the IP address of the given hostname cannot be determined.
This constructor creates a Socket and connects it to the specified port on the given host.
If a program has specified a socket factory, the createSocketImpl() method of that factory is called to create the actual socket implementation. Otherwise, the constructor creates a plain socket.
The IP address of a remote machine.
A port on a remote machine.
If any kind of I/O error occurs.
If the application is not allowed to connect to the given address and port.
This constructor creates a Socket and connects it to the specified port on the host at the given address.
If a program has specified a socket factory, the createSocketImpl() method of that factory is called to create the actual socket implementation. Otherwise, the constructor creates a plain socket.
public Socket(String host, int port, InetAddress localAddr, int localPort) throws IOException
New as of JDK 1.1
The name of a remote machine.
A port on a remote machine.
An IP address on the local host.
A port on the local host.
If any kind of I/O error occurs.
If the application is not allowed to connect to the given host and port.
This constructor creates a Socket and connects it to the specified port on the given host. The constructor also binds the Socket to the specified local address and port.
If a program has specified a socket factory, the createSocketImpl() method of that factory is called to create the actual socket implementation. Otherwise, the constructor creates a plain socket.
public Socket(InetAddress address, int port, InetAddress localAddr, int localPort) throws IOException
New as of JDK 1.1
The IP address of a remote machine.
A port on a remote machine.
An IP address on the local host.
A port on the local host.
If any kind of I/O error occurs.
If the application is not allowed to connect to the given address and port.
This constructor creates a Socket and connects it to the specified port on the host at the given address. The constructor also binds the Socket to the specified local address and port.
If a program has specified a socket factory, the createSocketImpl() method of that factory is called to create the actual socket implementation. Otherwise, the constructor creates a plain socket.
public Socket(String host, int port, boolean stream) throws IOException
Deprecated as of JDK 1.1
The name of a remote machine.
A port on a remote machine.
A boolean value that indicates if this socket is a stream socket.
If any kind of I/O error occurs.
If the application is not allowed to connect to the given host and port.
This constructor creates a Socket and connects it to the specified port on the given host.
If the stream argument is true, a stream socket is created. Otherwise, a datagram socket is created. This constructor is deprecated as of JDK 1.1; use DatagramSocket for datagrams.
If a program has specified a socket factory, the createSocketImpl() method of that factory is called to create the actual socket implementation. Otherwise, the constructor creates a plain socket.
public Socket(InetAddress address, int port, boolean stream) throws IOException
Deprecated as of JDK 1.1
The IP address of a remote machine.
A port on a remote machine.
A boolean value that indicates if this socket is a stream socket.
If any kind of I/O error occurs.
If the application is not allowed to connect to the given host and port.
This constructor creates a Socket and connects it to the specified port on the host at the given address.
If the stream argument is true, a stream socket is created. Otherwise, a datagram socket is created. This constructor is deprecated as of JDK 1.1; use DatagramSocket for datagrams.
If a program has specified a socket factory, the createSocketImpl() method of that factory is called to create the actual socket implementation. Otherwise, the constructor creates a plain socket.
New as of JDK 1.1
This constructor creates a Socket that uses an instance of the system default SocketImpl subclass for its low-level network access.
New as of JDK 1.1
The socket implementation to use.
This exception is never thrown by this constructor.
This constructor creates a Socket that uses the given object for its low-level network access.
public static synchronized void setSocketImplFactory( SocketImplFactory fac) throws IOException
An object that implements SocketImplFactory.
If the factory has already been defined.
If the application does not have permission to set the factory.
This method sets the SocketImplFactory. This factory produces instances of subclasses of SocketImpl that do the low-level work of sockets. When a Socket constructor is called, the createSocketImpl() method of the factory is called to create the socket implementation.
If any kind of I/O error occurs.
This method closes this socket, releasing any system resources it holds.
The remote IP address to which this Socket is connected.
This method returns the IP address of the remote host to which this socket is connected.
An InputStream that wraps this socket.
If any kind of I/O error occurs.
This method returns an InputStream that reads data from the socket.
New as of JDK 1.1
The local IP address from which this Socket originates.
This method returns the local address that is the origin of the socket.
The local port number from which this Socket originates.
This method returns the local port number that is the origin of the socket.
An OutputStream that wraps this socket.
If any kind of I/O error occurs.
This method returns an OutputStream that sends data through the socket.
The remote port number to which this Socket is connected.
This method returns the port number of the remote host to which this socket is connected.
New as of JDK 1.1
The close time-out value for the socket.
If any kind of socket error occurs.
This method returns the close time-out value for this socket. A value of -1 or 0 indicates that close()closes the socket immediately. A value greater than 0 indicates the amount of time, in seconds, that close() blocks, waiting for unsent data to be sent.
New as of JDK 1.1
The read time-out value for the socket.
If any kind of socket error occurs.
This method returns the read time-out value for this socket. A value of zero indicates that the read() method of the associated InputStream waits indefinitely for an incoming packet, while a non-zero value indicates the number of milliseconds it waits before throwing an InterruptedIOException.
New as of JDK 1.1
true if Nagle's algorithm is disabled for this connection; false otherwise.
If any kind of socket error occurs.
This method indicates whether Nagle's algorithm is disabled for this socket or not. Said another way, it indicates whether the TCPNODELAY option is enabled or not.
In essence, Nagle's algorithm takes small outgoing packets that are closely spaced in time and combines them into larger packets. This improves overall efficiency, since each packet has a certain amount of overhead; however, it does so at the expense of immediacy.
public void setSoLinger(boolean on, int val) throws SocketException
New as of JDK 1.1
A boolean value that specifies whether or not close() blocks
The new close time-out value, in seconds, for this socket.
If any kind of socket error occurs.
This method sets the close timeout value for this socket. If val is -1 or 0, or if on is false, close() closes the socket immediately. If on is true and val is greater than 0, val indicates the amount of time, in seconds, that close() blocks, waiting for unsent data to be sent.
public synchronized void setSoTimeout(int timeout) throws SocketException
New as of JDK 1.1
The new read time-out value, in milliseconds, for the socket.
If any kind of socket error occurs.
This method is used to set the time-out value that is used for the read() method of the corresponding InputStream. A non-zero value is the length of time, in milliseconds, that the Socket should wait for data before throwing an InterruptedIOException. A value of zero indicates that the Socket should wait indefinitely. If a timeout value is needed, this method must be called before read().
New as of JDK 1.1
A boolean value that specifies whether or not to disable Nagle's algorithm.
If any kind of socket error occurs.
This method specifies whether Nagle's algorithm is disabled for this socket or not. Said another way, it determines whether the TCPNODELAY option is enabled or not.
In essence, Nagle's algorithm takes small outgoing packets that are closely spaced in time and combines them into larger packets. This improves overall efficiency, since each packet has a certain amount of overhead; however, it does so at the expense of immediacy.
The string representation of this Socket.
Object.toString()
This method returns a String that contains the address and port of this Socket.
Method | Inherited From | Method | Inherited From |
---|---|---|---|
clone() |
Object |
equals(Object) |
Object |
finalize() |
Object |
getClass() |
Object |
hashCode() |
Object |
notify() |
Object |
notifyAll() |
Object |
wait() |
Object |
wait(long) |
Object |
wait(long, int) |
Object |
DatagramSocket, InetAddress, InputStream, IOException, OutputStream, SecurityException, SocketException, SocketImpl, SocketImplFactory, UnknownHostException
This HTML Help has been published using the chm2web software. |