Class java.net.Socket
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.net.Socket

Object
   |
   +----java.net.Socket

public class Socket
extends Object
This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines.

The actual work of the socket is performed by an instance of the SocketImpl class. An application, by changing the socket factory that creates the socket implementation, can configure itself to create sockets appropriate to the local firewall.

Version:
1.30, 07/01/98
Author:
unascribed
Since:
JDK1.0
See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl

Constructor Index

 o java.net.Socket()
Creates an unconnected socket, with the system-default type of SocketImpl.
 o java.net.Socket(SocketImpl)
Creates an unconnected Socket with a user-specified SocketImpl.
 o java.net.Socket(String, int)
Creates a stream socket and connects it to the specified port number on the named host.
 o java.net.Socket(InetAddress, int)
Creates a stream socket and connects it to the specified port number at the specified IP address.
 o java.net.Socket(String, int, InetAddress, int)
Creates a socket and connects it to the specified remote host on the specified remote port.
 o java.net.Socket(InetAddress, int, InetAddress, int)
Creates a socket and connects it to the specified remote address on the specified remote port.
 o java.net.Socket(String, int, boolean)
Creates a stream socket and connects it to the specified port number on the named host.
 o java.net.Socket(InetAddress, int, boolean)
Creates a socket and connects it to the specified port number at the specified IP address.

Method Index

 o close()
Closes this socket.
 o getInetAddress()
Returns the address to which the socket is connected.
 o getInputStream()
Returns an input stream for this socket.
 o getLocalAddress()
Gets the local address to which the socket is bound.
 o getLocalPort()
Returns the local port to which this socket is bound.
 o getOutputStream()
Returns an output stream for this socket.
 o getPort()
Returns the remote port to which this socket is connected.
 o getSoLinger()
Returns setting for SO_LINGER.
 o getSoTimeout()
Returns setting for SO_TIMEOUT.
 o getTcpNoDelay()
Tests if TCP_NODELAY is enabled.
 o setSoLinger(boolean, int)
Enable/disable SO_LINGER with the specified linger time.
 o setSoTimeout(int)
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
 o setSocketImplFactory(SocketImplFactory)
Sets the client socket implementation factory for the application.
 o setTcpNoDelay(boolean)
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
 o toString()
Converts this socket to a String.

Constructors

 o Socket
protected Socket()
Creates an unconnected socket, with the system-default type of SocketImpl.

Since:
JDK1.1
 o Socket
protected Socket(SocketImpl impl) throws SocketException
Creates an unconnected Socket with a user-specified SocketImpl.

The impl parameter is an instance of a SocketImpl the subclass wishes to use on the Socket.

Since:
JDK1.1
 o Socket
public Socket(String host,
              int port) throws UnknownHostException, IOException
Creates a stream socket and connects it to the specified port number on the named host.

If the application has specified a server socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

Parameters:
host - the host name.
port - the port number.
Throws: IOException
if an I/O error occurs when creating the socket.
Since:
JDK1.0
See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, createSocketImpl()
 o Socket
public Socket(InetAddress address,
              int port) throws IOException
Creates a stream socket and connects it to the specified port number at the specified IP address.

If the application has specified a socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

Parameters:
address - the IP address.
port - the port number.
Throws: IOException
if an I/O error occurs when creating the socket.
Since:
JDK1.0
See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, createSocketImpl()
 o Socket
public Socket(String host,
              int port,
              InetAddress localAddr,
              int localPort) throws IOException
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.

Parameters:
host - the name of the remote host
port - the remote port
localAddr - the local address the socket is bound to
localPort - the local port the socket is bound to
Since:
JDK1.1
 o Socket
public Socket(InetAddress address,
              int port,
              InetAddress localAddr,
              int localPort) throws IOException
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.

Parameters:
address - the remote address
port - the remote port
localAddr - the local address the socket is bound to
localPort - the local port the socket is bound to
Since:
JDK1.1
 o Socket
public Socket(String host,
              int port,
              boolean stream) throws IOException
Note: Socket() is deprecated. Use DatagramSocket instead for UDP transport.

Creates a stream socket and connects it to the specified port number on the named host.

If the stream argument is true, this creates a stream socket. If the stream argument is false, it creates a datagram socket.

If the application has specified a server socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

Parameters:
host - the host name.
port - the port number.
stream - a boolean indicating whether this is a stream socket or a datagram socket.
Throws: IOException
if an I/O error occurs when creating the socket.
Since:
JDK1.0
See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, createSocketImpl()
 o Socket
public Socket(InetAddress host,
              int port,
              boolean stream) throws IOException
Note: Socket() is deprecated. Use DatagramSocket instead for UDP transport.

Creates a socket and connects it to the specified port number at the specified IP address.

If the stream argument is true, this creates a stream socket. If the stream argument is false, it creates a datagram socket.

If the application has specified a server socket factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket is created.

Parameters:
address - the IP address.
port - the port number.
stream - if true, create a stream socket; otherwise, create a datagram socket.
Throws: IOException
if an I/O error occurs when creating the socket.
Since:
JDK1.0
See Also:
setSocketImplFactory(java.net.SocketImplFactory), SocketImpl, createSocketImpl()

Methods

 o close
public synchronized void close() throws IOException
Closes this socket.

Throws: IOException
if an I/O error occurs when closing this socket.
Since:
JDK1.0
 o getInetAddress
public java.net.InetAddress getInetAddress()
Returns the address to which the socket is connected.

Returns:
the remote IP address to which this socket is connected.
Since:
JDK1.0
 o getInputStream
public java.io.InputStream getInputStream() throws IOException
Returns an input stream for this socket.

Returns:
an input stream for reading bytes from this socket.
Throws: IOException
if an I/O error occurs when creating the input stream.
Since:
JDK1.0
 o getLocalAddress
public java.net.InetAddress getLocalAddress()
Gets the local address to which the socket is bound.

Since:
JDK1.1
 o getLocalPort
public int getLocalPort()
Returns the local port to which this socket is bound.

Returns:
the local port number to which this socket is connected.
Since:
JDK1.0
 o getOutputStream
public java.io.OutputStream getOutputStream() throws IOException
Returns an output stream for this socket.

Returns:
an output stream for writing bytes to this socket.
Throws: IOException
if an I/O error occurs when creating the output stream.
Since:
JDK1.0
 o getPort
public int getPort()
Returns the remote port to which this socket is connected.

Returns:
the remote port number to which this socket is connected.
Since:
JDK1.0
 o getSoLinger
public int getSoLinger() throws SocketException
Returns setting for SO_LINGER. -1 returns implies that the option is disabled.

Since:
JDK1.1
 o getSoTimeout
public synchronized int getSoTimeout() throws SocketException
Returns setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).

Since:
JDK1.1
 o getTcpNoDelay
public boolean getTcpNoDelay() throws SocketException
Tests if TCP_NODELAY is enabled.

Since:
JDK1.1
 o setSoLinger
public void setSoLinger(boolean on,
                        int val) throws SocketException
Enable/disable SO_LINGER with the specified linger time.

Since:
JDK1.1
 o setSoTimeout
public synchronized void setSoTimeout(int timeout) throws SocketException
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.io.InterruptedIOException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

Since:
JDK 1.1
 o setSocketImplFactory
public static synchronized void setSocketImplFactory(SocketImplFactory fac) throws IOException
Sets the client socket implementation factory for the application. The factory can be specified only once.

When an application creates a new client socket, the socket implementation factory's createSocketImpl method is called to create the actual socket implementation.

Parameters:
fac - the desired factory.
Throws: IOException
if an I/O error occurs when setting the socket factory.
Throws: SocketException
if the factory is already defined.
Since:
JDK1.0
See Also:
createSocketImpl()
 o setTcpNoDelay
public void setTcpNoDelay(boolean on) throws SocketException
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).

Since:
JDK1.1
 o toString
public java.lang.String toString()
Converts this socket to a String.

Returns:
a string representation of this socket.
Overrides:
toString in class Object
Since:
JDK1.0

All Packages  Class Hierarchy  This Package  Previous  Next  Index