    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.

##

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

##

close()
  Closes the socket.
getInetAddress()
  Gets the address to which the socket is connected.
getInputStream()
  Gets an InputStream for this socket.
getLocalAddress()
  Gets the local address to which the socket is bound.
getLocalPort()
  Gets the local port to which the socket is bound.
getOutputStream()
  Gets an OutputStream for this socket.
getPort()
  Gets the remote port to which the socket is connected.
getSoLinger()
  Retrive setting for SO_LINGER.
getSoTimeout()
  Retrive setting for SO_TIMEOUT.
getTcpNoDelay()
  Retrieve whether TCP_NODELAY is enabled
setSocketImplFactory(SocketImplFactory)
  Sets the system's client SocketImplFactory.
setSoLinger(boolean, int)
  Enable/disable SO_LINGER with the specified linger time.
setSoTimeout(int)
  Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
setTcpNoDelay(boolean)
  Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
toString()
  Converts the Socket to a String.

##

Socket

      protected Socket()

  Creates an unconnected socket, with the system-default type of SocketImpl.

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.

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

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

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

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

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).

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).

##

getInetAddress

      public InetAddress getInetAddress()

  Gets the address to which the socket is connected.

getLocalAddress

      public InetAddress getLocalAddress()

  Gets the local address to which the socket is bound.

getPort

      public int getPort()

  Gets the remote port to which the socket is connected.

getLocalPort

      public int getLocalPort()

  Gets the local port to which the socket is bound.

getInputStream

      public InputStream getInputStream() throws IOException

  Gets an InputStream for this socket.

getOutputStream

      public OutputStream getOutputStream() throws IOException

  Gets an OutputStream for this socket.

setTcpNoDelay

      public void setTcpNoDelay(boolean on) throws SocketException

  Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).

getTcpNoDelay

      public boolean getTcpNoDelay() throws SocketException

  Retrieve whether TCP_NODELAY is enabled

setSoLinger

      public void setSoLinger(boolean on,
                              int val) throws SocketException

  Enable/disable SO_LINGER with the specified linger time.

getSoLinger

      public int getSoLinger() throws SocketException

  Retrive setting for SO_LINGER. -1 returns implies that the option is disabled.

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.

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).

close

      public synchronized void close() throws IOException

  Closes the socket.

toString

      public String toString()

  Converts the Socket to a String.

  Overrides:
    toString in class Object

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