# SocketFactory

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Protected Ctors](https://developer.android.com/reference/javax/net/SocketFactory#proctors)\|[Methods](https://developer.android.com/reference/javax/net/SocketFactory#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/javax/net/SocketFactory#inhmethods)  

# SocketFactory

`
public


abstract
class
SocketFactory
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|-------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | javax.net.SocketFactory |

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Known direct subclasses [SSLSocketFactory](https://developer.android.com/reference/javax/net/ssl/SSLSocketFactory) |--------------------------------------------------------------------------------------------|-----------------------------------------| | [SSLSocketFactory](https://developer.android.com/reference/javax/net/ssl/SSLSocketFactory) | `SSLSocketFactory`s create`SSLSocket`s. | |

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Known indirect subclasses [SSLCertificateSocketFactory](https://developer.android.com/reference/android/net/SSLCertificateSocketFactory) |----------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [SSLCertificateSocketFactory](https://developer.android.com/reference/android/net/SSLCertificateSocketFactory) | *This class was deprecated in API level 29. This class has less error-prone replacements using standard APIs. To create an`SSLSocket`, obtain an[SSLSocketFactory](https://developer.android.com/reference/javax/net/ssl/SSLSocketFactory)from[SSLSocketFactory.getDefault()](https://developer.android.com/reference/javax/net/ssl/SSLSocketFactory#getDefault())or[SSLContext.getSocketFactory()](https://developer.android.com/reference/javax/net/ssl/SSLContext#getSocketFactory()). To verify hostnames, pass`"HTTPS"`to[SSLParameters.setEndpointIdentificationAlgorithm(String)](https://developer.android.com/reference/javax/net/ssl/SSLParameters#setEndpointIdentificationAlgorithm(java.lang.String)). To enable ALPN, use[SSLParameters.setApplicationProtocols(String[])](https://developer.android.com/reference/javax/net/ssl/SSLParameters#setApplicationProtocols(java.lang.String[])). To enable SNI, use[SSLParameters.setServerNames(java.util.List)](https://developer.android.com/reference/javax/net/ssl/SSLParameters#setServerNames(java.util.List<javax.net.ssl.SNIServerName>)).* | |

<br />

*** ** * ** ***

This class creates sockets. It may be subclassed by other factories, which create particular subclasses of sockets and thus provide a general framework for the addition of public socket-level functionality.

Socket factories are a simple way to capture a variety of policies related to the sockets being constructed, producing such sockets in a way which does not require special configuration of the code which asks for the sockets:

- Due to polymorphism of both factories and sockets, different kinds of sockets can be used by the same application code just by passing it different kinds of factories.
- Factories can themselves be customized with parameters used in socket construction. So for example, factories could be customized to return sockets with different networking timeouts or security parameters already configured.
- The sockets returned to the application can be subclasses of java.net.Socket, so that they can directly expose new APIs for features such as compression, security, record marking, statistics collection, or firewall tunneling.

Factory classes are specified by environment-specific configuration mechanisms. For example, the*getDefault*method could return a factory that was appropriate for a particular user or applet, and a framework could use a factory customized to its own purposes.  
**See also:**

- [ServerSocketFactory](https://developer.android.com/reference/javax/net/ServerSocketFactory)

## Summary

|                                                      ### Protected constructors                                                       ||
|------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[SocketFactory](https://developer.android.com/reference/javax/net/SocketFactory#SocketFactory())`() ` Creates a`SocketFactory`. |

|                                                                                                                                                                                                                                                                          ### Public methods                                                                                                                                                                                                                                                                          ||
|--------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` abstract `[Socket](https://developer.android.com/reference/java/net/Socket)              | ` `[createSocket](https://developer.android.com/reference/javax/net/SocketFactory#createSocket(java.lang.String,%20int))`(`[String](https://developer.android.com/reference/java/lang/String)` host, int port) ` Creates a socket and connects it to the specified remote host at the specified remote port.                                                                                                                                                             |
| ` abstract `[Socket](https://developer.android.com/reference/java/net/Socket)              | ` `[createSocket](https://developer.android.com/reference/javax/net/SocketFactory#createSocket(java.net.InetAddress,%20int))`(`[InetAddress](https://developer.android.com/reference/java/net/InetAddress)` host, int port) ` Creates a socket and connects it to the specified port number at the specified address.                                                                                                                                                    |
| ` abstract `[Socket](https://developer.android.com/reference/java/net/Socket)              | ` `[createSocket](https://developer.android.com/reference/javax/net/SocketFactory#createSocket(java.net.InetAddress,%20int,%20java.net.InetAddress,%20int))`(`[InetAddress](https://developer.android.com/reference/java/net/InetAddress)` address, int port, `[InetAddress](https://developer.android.com/reference/java/net/InetAddress)` localAddress, int localPort) ` Creates a socket and connect it to the specified remote address on the specified remote port. |
| ` abstract `[Socket](https://developer.android.com/reference/java/net/Socket)              | ` `[createSocket](https://developer.android.com/reference/javax/net/SocketFactory#createSocket(java.lang.String,%20int,%20java.net.InetAddress,%20int))`(`[String](https://developer.android.com/reference/java/lang/String)` host, int port, `[InetAddress](https://developer.android.com/reference/java/net/InetAddress)` localHost, int localPort) ` Creates a socket and connects it to the specified remote host on the specified remote port.                      |
| ` `[Socket](https://developer.android.com/reference/java/net/Socket)                       | ` `[createSocket](https://developer.android.com/reference/javax/net/SocketFactory#createSocket())`() ` Creates an unconnected socket.                                                                                                                                                                                                                                                                                                                                    |
| ` static `[SocketFactory](https://developer.android.com/reference/javax/net/SocketFactory) | ` `[getDefault](https://developer.android.com/reference/javax/net/SocketFactory#getDefault())`() ` Returns a copy of the environment's default socket factory.                                                                                                                                                                                                                                                                                                           |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Protected constructors

### SocketFactory

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
protected SocketFactory ()
```

Creates a`SocketFactory`.

<br />

## Public methods

### createSocket

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract Socket createSocket (String host, 
                int port)
```

Creates a socket and connects it to the specified remote host at the specified remote port. This socket is configured using the socket options established for this factory.

If there is a security manager, its`checkConnect`method is called with the host address and`port`as its arguments. This could result in a SecurityException.

<br />

|                                               Parameters                                               ||
|--------|------------------------------------------------------------------------------------------------|
| `host` | `String`: the server host name with which to connect, or`null`for the loopback address. <br /> |
| `port` | `int`: the server port <br />                                                                  |

|                                        Returns                                        ||
|-------------------------------------------------------------------|--------------------|
| [Socket](https://developer.android.com/reference/java/net/Socket) | the`Socket` <br /> |

|                                                                                                           Throws                                                                                                            ||
|--------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)                             | if an I/O error occurs when creating the socket                                                                     |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException)               | if a security manager exists and its`checkConnect`method doesn't allow the operation.                               |
| [UnknownHostException](https://developer.android.com/reference/java/net/UnknownHostException)          | if the host is not known                                                                                            |
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if the port parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive. |

**See also:**

- [SecurityManager.checkConnect(String, int)](https://developer.android.com/reference/java/lang/SecurityManager#checkConnect(java.lang.String,%20int))
- [Socket.Socket(String, int)](https://developer.android.com/reference/java/net/Socket#Socket(java.lang.String,%20int))  

### createSocket

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract Socket createSocket (InetAddress host, 
                int port)
```

Creates a socket and connects it to the specified port number at the specified address. This socket is configured using the socket options established for this factory.

If there is a security manager, its`checkConnect`method is called with the host address and`port`as its arguments. This could result in a SecurityException.

<br />

|                  Parameters                   ||
|--------|---------------------------------------|
| `host` | `InetAddress`: the server host <br /> |
| `port` | `int`: the server port <br />         |

|                                        Returns                                        ||
|-------------------------------------------------------------------|--------------------|
| [Socket](https://developer.android.com/reference/java/net/Socket) | the`Socket` <br /> |

|                                                                                                           Throws                                                                                                            ||
|--------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)                             | if an I/O error occurs when creating the socket                                                                     |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException)               | if a security manager exists and its`checkConnect`method doesn't allow the operation.                               |
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if the port parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive. |
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)         | if`host`is null.                                                                                                    |

**See also:**

- [SecurityManager.checkConnect(String, int)](https://developer.android.com/reference/java/lang/SecurityManager#checkConnect(java.lang.String,%20int))
- [Socket.Socket(java.net.InetAddress, int)](https://developer.android.com/reference/java/net/Socket#Socket(java.net.InetAddress,%20int))  

### createSocket

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract Socket createSocket (InetAddress address, 
                int port, 
                InetAddress localAddress, 
                int localPort)
```

Creates a socket and connect it to the specified remote address on the specified remote port. The socket will also be bound to the local address and port suplied. The socket is configured using the socket options established for this factory.

If there is a security manager, its`checkConnect`method is called with the host address and`port`as its arguments. This could result in a SecurityException.

<br />

|                            Parameters                            ||
|----------------|--------------------------------------------------|
| `address`      | `InetAddress`: the server network address <br /> |
| `port`         | `int`: the server port <br />                    |
| `localAddress` | `InetAddress`: the client network address <br /> |
| `localPort`    | `int`: the client port <br />                    |

|                                        Returns                                        ||
|-------------------------------------------------------------------|--------------------|
| [Socket](https://developer.android.com/reference/java/net/Socket) | the`Socket` <br /> |

|                                                                                                                       Throws                                                                                                                       ||
|--------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)                             | if an I/O error occurs when creating the socket                                                                                            |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException)               | if a security manager exists and its`checkConnect`method doesn't allow the operation.                                                      |
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if the port parameter or localPort parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive. |
| [NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException)         | if`address`is null.                                                                                                                        |

**See also:**

- [SecurityManager.checkConnect(String, int)](https://developer.android.com/reference/java/lang/SecurityManager#checkConnect(java.lang.String,%20int))
- [Socket.Socket(java.net.InetAddress, int, java.net.InetAddress, int)](https://developer.android.com/reference/java/net/Socket#Socket(java.net.InetAddress,%20int,%20java.net.InetAddress,%20int))  

### createSocket

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public abstract Socket createSocket (String host, 
                int port, 
                InetAddress localHost, 
                int localPort)
```

Creates a socket and connects it to the specified remote host on the specified remote port. The socket will also be bound to the local address and port supplied. This socket is configured using the socket options established for this factory.

If there is a security manager, its`checkConnect`method is called with the host address and`port`as its arguments. This could result in a SecurityException.

<br />

|                                                 Parameters                                                  ||
|-------------|------------------------------------------------------------------------------------------------|
| `host`      | `String`: the server host name with which to connect, or`null`for the loopback address. <br /> |
| `port`      | `int`: the server port <br />                                                                  |
| `localHost` | `InetAddress`: the local address the socket is bound to <br />                                 |
| `localPort` | `int`: the local port the socket is bound to <br />                                            |

|                                        Returns                                        ||
|-------------------------------------------------------------------|--------------------|
| [Socket](https://developer.android.com/reference/java/net/Socket) | the`Socket` <br /> |

|                                                                                                                       Throws                                                                                                                       ||
|--------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException)                             | if an I/O error occurs when creating the socket                                                                                            |
| [SecurityException](https://developer.android.com/reference/java/lang/SecurityException)               | if a security manager exists and its`checkConnect`method doesn't allow the operation.                                                      |
| [UnknownHostException](https://developer.android.com/reference/java/net/UnknownHostException)          | if the host is not known                                                                                                                   |
| [IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException) | if the port parameter or localPort parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive. |

**See also:**

- [SecurityManager.checkConnect(String, int)](https://developer.android.com/reference/java/lang/SecurityManager#checkConnect(java.lang.String,%20int))
- [Socket.Socket(String, int, java.net.InetAddress, int)](https://developer.android.com/reference/java/net/Socket#Socket(java.lang.String,%20int,%20java.net.InetAddress,%20int))  

### createSocket

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public Socket createSocket ()
```

Creates an unconnected socket.

<br />

|                                             Returns                                              ||
|-------------------------------------------------------------------|-------------------------------|
| [Socket](https://developer.android.com/reference/java/net/Socket) | the unconnected socket <br /> |

|                                                   Throws                                                    ||
|----------------------------------------------------------------------------|---------------------------------|
| [IOException](https://developer.android.com/reference/java/io/IOException) | if the socket cannot be created |

**See also:**

- [Socket.connect(java.net.SocketAddress)](https://developer.android.com/reference/java/net/Socket#connect(java.net.SocketAddress))
- [Socket.connect(java.net.SocketAddress, int)](https://developer.android.com/reference/java/net/Socket#connect(java.net.SocketAddress,%20int))
- [Socket.Socket()](https://developer.android.com/reference/java/net/Socket#Socket())  

### getDefault

Added in[API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static SocketFactory getDefault ()
```

Returns a copy of the environment's default socket factory.

<br />

|                                                       Returns                                                       ||
|----------------------------------------------------------------------------------|-----------------------------------|
| [SocketFactory](https://developer.android.com/reference/javax/net/SocketFactory) | the default`SocketFactory` <br /> |