java.net
Class Socket

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

public final 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 Summary
Socket(InetAddress address, int port)
          Creates a stream socket and connects it to the specified address on the specified port.
Socket(InetAddress address, int port, boolean stream)
          Creates a socket and connects it to the specified address on the specified port.
Socket(String host, int port)
          Creates a stream socket and connects it to the specified port on the specified host.
Socket(String host, int port, boolean stream)
          Creates a socket and connects it to the specified port on the specified host.
 
Method Summary
 void close()
          Closes the socket.
 InetAddress getInetAddress()
          Gets the address to which the socket is connected.
 InputStream getInputStream()
          Gets an InputStream for this socket.
 int getLocalPort()
          Gets the local port to which the socket is connected.
 OutputStream getOutputStream()
          Gets an OutputStream for this socket.
 int getPort()
          Gets the remote port to which the socket is connected.
static void setSocketImplFactory(SocketImplFactory fac)
          Sets the system's client SocketImplFactory.
 String toString()
          Converts the Socket to a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

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.

Parameters:
host - the host
port - the port

Socket

public Socket(String host,
              int port,
              boolean stream)
       throws IOException
Creates a socket and connects it to the specified port on the specified host. The last argument lets you specify whether you want a stream or datagram socket.

Parameters:
host - the specified host
port - the specified port
stream - a boolean indicating whether this is a stream or datagram socket

Socket

public Socket(InetAddress address,
              int port)
       throws IOException
Creates a stream socket and connects it to the specified address on the specified port.

Parameters:
address - the specified address
port - the specified port

Socket

public Socket(InetAddress address,
              int port,
              boolean stream)
       throws IOException
Creates a socket and connects it to the specified address on the specified port. The last argument lets you specify whether you want a stream or datagram socket.

Parameters:
address - the specified address
port - the specified port
stream - a boolean indicating whether this is a stream or datagram socket
Method Detail

getInetAddress

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


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


getInputStream

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

IOException

getOutputStream

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

IOException

close

public void close()
           throws IOException
Closes the socket.

IOException

toString

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

Overrides:
toString in class Object

setSocketImplFactory

public static 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.
IOException