All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.net.Socket

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

public class Socket
extends Object
The client Socket class. It uses a SocketImpl to implement the actual socket operations. It is done this way so that you are able to change socket implementations depending on the kind of firewall that is used. You can change socket implementations by setting the SocketImplFactory.


Constructor Index

 o Socket()
Creates an unconnected socket, with the system-default type of SocketImpl.
 o Socket(InetAddress, int)
Creates a stream socket and connects it to the specified address on the specified port.
 o Socket(InetAddress, int, boolean)
Create a "connected" UDP socket (with an unreliable transport layer). Deprecated.
 o Socket(InetAddress, int, InetAddress, int)
Creates a socket and connects it to the specified remote address on the specified remote port.
 o Socket(SocketImpl)
Creates an unconnected Socket with a user-specified SocketImpl.
 o Socket(String, int)
Creates a stream socket and connects it to the specified port on the specified host.
 o Socket(String, int, boolean)
Create a "connected" UDP socket (with an unreliable transport layer). Deprecated.
 o Socket(String, int, InetAddress, int)
Creates a socket and connects it to the specified remote host on the specified remote port.

Method Index

 o close()
Closes the socket.
 o getInetAddress()
Gets the address to which the socket is connected.
 o getInputStream()
Gets an InputStream for this socket.
 o getLocalAddress()
Gets the local address to which the socket is bound.
 o getLocalPort()
Gets the local port to which the socket is bound.
 o getOutputStream()
Gets an OutputStream for this socket.
 o getPort()
Gets the remote port to which the socket is connected.
 o getSoLinger()
Retrive setting for SO_LINGER.
 o getSoTimeout()
Retrive setting for SO_TIMEOUT.
 o getTcpNoDelay()
Retrieve whether TCP_NODELAY is enabled
 o setSocketImplFactory(SocketImplFactory)
Sets the system's client SocketImplFactory.
 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 setTcpNoDelay(boolean)
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
 o toString()
Converts the Socket to a String.

Constructors

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

 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.

 o Socket
  public Socket(String host,
                int port) throws UnknownHostException, IOException
Creates a stream socket and connects it to the specified port on the specified host. The port must be between 0 and 65535, inclusive.

Parameters:
host - the host
port - the port
 o Socket
  public Socket(InetAddress address,
                int port) throws IOException
Creates a stream socket and connects it to the specified address on the specified port. The port must be between 0 and 65535, inclusive.

Parameters:
address - the specified address
port - the specified port
 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
 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
 o Socket
  public Socket(String host,
                int port,
                boolean stream) throws IOException
Note: Socket() is deprecated. Use DatagramSocket instead for UDP transport

Create a "connected" UDP socket (with an unreliable transport layer).

 o Socket
  public Socket(InetAddress host,
                int port,
                boolean stream) throws IOException
Note: Socket() is deprecated. Use DatagramSocket instead for UDP transport

Create a "connected" UDP socket (with an unreliable transport layer).

Methods

 o getInetAddress
  public InetAddress getInetAddress()
Gets the address to which the socket is connected.

 o getLocalAddress
  public InetAddress getLocalAddress()
Gets the local address to which the socket is bound.

 o getPort
  public int getPort()
Gets the remote port to which the socket is connected.

 o getLocalPort
  public int getLocalPort()
Gets the local port to which the socket is bound.

 o getInputStream
  public InputStream getInputStream() throws IOException
Gets an InputStream for this socket.

 o getOutputStream
  public OutputStream getOutputStream() throws IOException
Gets an OutputStream for this socket.

 o setTcpNoDelay
  public void setTcpNoDelay(boolean on) throws SocketException
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).

 o getTcpNoDelay
  public boolean getTcpNoDelay() throws SocketException
Retrieve whether TCP_NODELAY is enabled

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

 o getSoLinger
  public int getSoLinger() throws SocketException
Retrive setting for SO_LINGER. -1 returns implies that the option is disabled.

 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.

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

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

 o toString
  public String toString()
Converts the Socket to a String.

Overrides:
toString in class Object
 o setSocketImplFactory
  public static synchronized void setSocketImplFactory(SocketImplFactory fac) throws IOException
Sets the system's client SocketImplFactory. The factory can be specified only once.

Parameters:
fac - the desired factory
Throws: SocketException
If the factory is already defined.

All Packages  Class Hierarchy  This Package  Previous  Next  Index