Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 22](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/org/apache/http/conn/scheme/SocketFactory#pubmethods)

# SocketFactory

` public interface SocketFactory `   
` `

|---|
| org.apache.http.conn.scheme.SocketFactory |

Known indirect subclasses [LayeredSocketFactory](https://developer.android.com/reference/org/apache/http/conn/scheme/LayeredSocketFactory), [SSLSocketFactory](https://developer.android.com/reference/org/apache/http/conn/ssl/SSLSocketFactory) [LayeredSocketFactory](https://developer.android.com/reference/org/apache/http/conn/scheme/LayeredSocketFactory) *This interface was deprecated in API level 22. Please use `https://developer.android.com/reference/java/net/URL#openConnection()` instead. Please visit [this webpage](http://android-developers.blogspot.com/2011/09/androids-http-clients.html) for further details.* [SSLSocketFactory](https://developer.android.com/reference/org/apache/http/conn/ssl/SSLSocketFactory) *This class was deprecated in API level 22. Please use `https://developer.android.com/reference/java/net/URL#openConnection()` instead. Please visit [this webpage](http://android-developers.blogspot.com/2011/09/androids-http-clients.html) for further details.*   

*** ** * ** ***

<br />

**This interface was deprecated in API level 22.**   

Please use `https://developer.android.com/reference/java/net/URL#openConnection()` instead. Please visit [this webpage](http://android-developers.blogspot.com/2011/09/androids-http-clients.html) for further details.

A factory for creating and connecting sockets. The factory encapsulates the logic for establishing a socket connection.   

Both `https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)` and `https://developer.android.com/reference/java/lang/Object#hashCode()` must be overridden for the correct operation of some connection managers.

## Summary

| ### Public methods ||
|---|---|
| ` abstract https://developer.android.com/reference/java/net/Socket` | ` https://developer.android.com/reference/org/apache/http/conn/scheme/SocketFactory#connectSocket(java.net.Socket,%20java.lang.String,%20int,%20java.net.InetAddress,%20int,%20org.apache.http.params.HttpParams)(https://developer.android.com/reference/java/net/Socket sock, https://developer.android.com/reference/java/lang/String host, int port, https://developer.android.com/reference/java/net/InetAddress localAddress, int localPort, https://developer.android.com/reference/org/apache/http/params/HttpParams params) ` Connects a socket to the given host. |
| ` abstract https://developer.android.com/reference/java/net/Socket` | ` https://developer.android.com/reference/org/apache/http/conn/scheme/SocketFactory#createSocket()() ` Creates a new, unconnected socket. |
| ` abstract boolean` | ` https://developer.android.com/reference/org/apache/http/conn/scheme/SocketFactory#isSecure(java.net.Socket)(https://developer.android.com/reference/java/net/Socket sock) ` Checks whether a socket provides a secure connection. |

## Public methods

### connectSocket

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

```
public abstract Socket connectSocket (Socket sock, 
                String host, 
                int port, 
                InetAddress localAddress, 
                int localPort, 
                HttpParams params)
```

Connects a socket to the given host.

<br />

| Parameters ||
|---|---|
| `sock` | `Socket`: the socket to connect, as obtained from `https://developer.android.com/reference/org/apache/http/conn/scheme/SocketFactory#createSocket()`. `null` indicates that a new socket should be created and connected. <br /> |
| `host` | `String`: the host to connect to <br /> |
| `port` | `int`: the port to connect to on the host <br /> |
| `localAddress` | `InetAddress`: the local address to bind the socket to, or `null` for any <br /> |
| `localPort` | `int`: the port on the local machine, 0 or a negative number for any <br /> |
| `params` | `HttpParams`: additional `https://developer.android.com/reference/org/apache/http/params/HttpParams` for connecting <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/Socket` | the connected socket. The returned object may be different from the `sock` argument if this factory supports a layered protocol. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if an I/O error occurs |
| `https://developer.android.com/reference/java/net/UnknownHostException` | if the IP address of the target host can not be determined |
| `https://developer.android.com/reference/org/apache/http/conn/ConnectTimeoutException` | if the socket cannot be connected within the time limit defined in the `params` |

### createSocket

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

```
public abstract Socket createSocket ()
```

Creates a new, unconnected socket. The socket should subsequently be passed to `https://developer.android.com/reference/org/apache/http/conn/scheme/SocketFactory#connectSocket(java.net.Socket,%20java.lang.String,%20int,%20java.net.InetAddress,%20int,%20org.apache.http.params.HttpParams)`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/Socket` | a new socket <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if an I/O error occurs while creating the socket |

### isSecure

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

```
public abstract boolean isSecure (Socket sock)
```

Checks whether a socket provides a secure connection. The socket must be `https://developer.android.com/reference/org/apache/http/conn/scheme/SocketFactory#connectSocket(java.net.Socket,%20java.lang.String,%20int,%20java.net.InetAddress,%20int,%20org.apache.http.params.HttpParams)` by this factory. The factory will *not* perform I/O operations in this method.   

As a rule of thumb, plain sockets are not secure and TLS/SSL sockets are secure. However, there may be application specific deviations. For example, a plain socket to a host in the same intranet ("trusted zone") could be considered secure. On the other hand, a TLS/SSL socket could be considered insecure based on the cypher suite chosen for the connection.

<br />

| Parameters ||
|---|---|
| `sock` | `Socket`: the connected socket to check <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if the connection of the socket should be considered secure, or `false` if it should not <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if the argument is invalid, for example because it is not a connected socket or was created by a different socket factory. Note that socket factories are *not* required to check these conditions, they may simply return a default value when called with an invalid socket argument. |