Package javax.net

Class SocketFactory

java.lang.Object
javax.net.SocketFactory
Direct Known Subclasses:
SSLSocketFactory

public abstract class SocketFactory
extends Object
This abstract class defines methods to create sockets. It can be subclassed to create specific socket types with additional socket-level functionality.
  • Constructor Summary

    Constructors
    Modifier Constructor Description
    protected SocketFactory()
    Creates a new SocketFactory instance.
  • Method Summary

    Modifier and Type Method Description
    Socket createSocket()
    Creates a new socket which is not connected to any remote host.
    abstract Socket createSocket​(String host, int port)
    Creates a new socket which is connected to the remote host specified by the parameters host and port.
    abstract Socket createSocket​(String host, int port, InetAddress localHost, int localPort)
    Creates a new socket which is connected to the remote host specified by the parameters host and port.
    abstract Socket createSocket​(InetAddress host, int port)
    Creates a new socket which is connected to the remote host specified by the InetAddress host.
    abstract Socket createSocket​(InetAddress address, int port, InetAddress localAddress, int localPort)
    Creates a new socket which is connected to the remote host specified by the InetAddress address.
    static SocketFactory getDefault()
    Gets the default socket factory of the system which can be used to create new sockets without creating a subclass of this factory.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SocketFactory

      protected SocketFactory()
      Creates a new SocketFactory instance.
  • Method Details

    • getDefault

      public static SocketFactory getDefault()
      Gets the default socket factory of the system which can be used to create new sockets without creating a subclass of this factory.
      Returns:
      the system default socket factory.
    • createSocket

      public Socket createSocket() throws IOException
      Creates a new socket which is not connected to any remote host. This method has to be overridden by a subclass otherwise a SocketException is thrown.
      Returns:
      the created unconnected socket.
      Throws:
      IOException - if an error occurs while creating a new socket.
    • createSocket

      public abstract Socket createSocket​(String host, int port) throws IOException, UnknownHostException
      Creates a new socket which is connected to the remote host specified by the parameters host and port. The socket is bound to any available local address and port.
      Parameters:
      host - the remote host address the socket has to be connected to.
      port - the port number of the remote host at which the socket is connected.
      Returns:
      the created connected socket.
      Throws:
      IOException - if an error occurs while creating a new socket.
      UnknownHostException - if the specified host is unknown or the IP address could not be resolved.
    • createSocket

      public abstract Socket createSocket​(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException
      Creates a new socket which is connected to the remote host specified by the parameters host and port. The socket is bound to the local network interface specified by the InetAddress localHost on port localPort.
      Parameters:
      host - the remote host address the socket has to be connected to.
      port - the port number of the remote host at which the socket is connected.
      localHost - the local host address the socket is bound to.
      localPort - the port number of the local host at which the socket is bound.
      Returns:
      the created connected socket.
      Throws:
      IOException - if an error occurs while creating a new socket.
      UnknownHostException - if the specified host is unknown or the IP address could not be resolved.
    • createSocket

      public abstract Socket createSocket​(InetAddress host, int port) throws IOException
      Creates a new socket which is connected to the remote host specified by the InetAddress host. The socket is bound to any available local address and port.
      Parameters:
      host - the host address the socket has to be connected to.
      port - the port number of the remote host at which the socket is connected.
      Returns:
      the created connected socket.
      Throws:
      IOException - if an error occurs while creating a new socket.
    • createSocket

      public abstract Socket createSocket​(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException
      Creates a new socket which is connected to the remote host specified by the InetAddress address. The socket is bound to the local network interface specified by the InetAddress localHost on port localPort.
      Parameters:
      address - the remote host address the socket has to be connected to.
      port - the port number of the remote host at which the socket is connected.
      localAddress - the local host address the socket is bound to.
      localPort - the port number of the local host at which the socket is bound.
      Returns:
      the created connected socket.
      Throws:
      IOException - if an error occurs while creating a new socket.