## SoupSocket SoupSocket — A network socket |

## Functions

SoupSocket * | soup_socket_new ()
void | (*SoupSocketCallback) ()
void | soup_socket_connect_async ()
guint | soup_socket_connect_sync ()
gboolean | soup_socket_listen ()
gboolean | soup_socket_start_ssl ()
gboolean | soup_socket_start_proxy_ssl ()
gboolean | soup_socket_is_ssl ()
void | soup_socket_disconnect ()
gboolean | soup_socket_is_connected ()
SoupAddress * | soup_socket_get_local_address ()
SoupAddress * | soup_socket_get_remote_address ()
int | soup_socket_get_fd ()
SoupSocketIOStatus | soup_socket_read ()
SoupSocketIOStatus | soup_socket_read_until ()
SoupSocketIOStatus | soup_socket_write ()

## Properties

gpointer | async-context | Read / Write / Construct Only
int | fd | Read / Write / Construct Only
GSocket * | gsocket | Write / Construct Only
GIOStream * | iostream | Write / Construct Only
gboolean | ipv6-only | Read / Write
gboolean | is-server | Read
SoupAddress * | local-address | Read / Write / Construct Only
gboolean | non-blocking | Read / Write
SoupAddress * | remote-address | Read / Write / Construct Only
SoupSocketProperties * | socket-properties | Write
gpointer | ssl-creds | Read / Write
gboolean | ssl-fallback | Read / Write / Construct Only
gboolean | ssl-strict | Read / Write / Construct Only
guint | timeout | Read / Write
GTlsCertificate * | tls-certificate | Read
GTlsCertificateFlags | tls-errors | Read
gboolean | trusted-certificate | Read
gboolean | use-thread-context | Read / Write / Construct Only

## Signals

void | disconnected | Run Last
void | event | Run Last
void | new-connection | Run First
void | readable | Run Last
void | writable | Run Last

## Types and Values

| SoupSocket
enum | SoupSocketIOStatus
#define | SOUP_SOCKET_LOCAL_ADDRESS
#define | SOUP_SOCKET_REMOTE_ADDRESS
#define | SOUP_SOCKET_FLAG_NONBLOCKING
#define | SOUP_SOCKET_IS_SERVER
#define | SOUP_SOCKET_SSL_CREDENTIALS
#define | SOUP_SOCKET_ASYNC_CONTEXT
#define | SOUP_SOCKET_TIMEOUT
#define | SOUP_SOCKET_SSL_FALLBACK
#define | SOUP_SOCKET_SSL_STRICT
#define | SOUP_SOCKET_TLS_CERTIFICATE
#define | SOUP_SOCKET_TLS_ERRORS
#define | SOUP_SOCKET_TRUSTED_CERTIFICATE
#define | SOUP_SOCKET_USE_THREAD_CONTEXT

## Object Hierarchy

        GObject
        ╰── SoupSocket

## Implemented Interfaces

SoupSocket implements GInitable.

## Includes

    #include <libsoup/soup.h>

## Description

SoupSocket is libsoup's TCP socket type. While it is primarily intended for internal use, SoupSockets are exposed in the API in various places, and some of their methods (eg, `soup_socket_get_remote_address()`) may be useful to applications.

## Functions

### soup_socket_new ()

    SoupSocket *
    soup_socket_new (const char *optname1,
                     ...);

Creates a new (disconnected) socket

#### Parameters

optname1 | name of first property to set (or `NULL`) |
... | value of `optname1` , followed by additional property/value pairs |

#### Returns

the new socket

### SoupSocketCallback ()

    void
    (*SoupSocketCallback) (SoupSocket *sock,
                           guint status,
                           gpointer user_data);

The callback function passed to `soup_socket_connect_async()`.

#### Parameters

sock | the SoupSocket |
status | an HTTP status code indicating success or failure |
user_data | the data passed to `soup_socket_connect_async()` |

### soup_socket_connect_async ()

    void
    soup_socket_connect_async (SoupSocket *sock,
                               GCancellable *cancellable,
                               SoupSocketCallback callback,
                               gpointer user_data);

Begins asynchronously connecting to `sock` 's remote address. The socket will call `callback` when it succeeds or fails (but not before returning from this function).

If `cancellable` is non-`NULL`, it can be used to cancel the connection. `callback` will still be invoked in this case, with a status of `SOUP_STATUS_CANCELLED`.

#### Parameters

sock | a client SoupSocket (which must not already be connected) |
cancellable | a GCancellable, or `NULL` |
callback | callback to call after connecting. | [scope async]
user_data | data to pass to `callback` |

### soup_socket_connect_sync ()

    guint
    soup_socket_connect_sync (SoupSocket *sock,
                              GCancellable *cancellable);

Attempt to synchronously connect `sock` to its remote address.

If `cancellable` is non-`NULL`, it can be used to cancel the connection, in which case `soup_socket_connect_sync()` will return `SOUP_STATUS_CANCELLED`.

#### Parameters

sock | a client SoupSocket (which must not already be connected) |
cancellable | a GCancellable, or `NULL` |

#### Returns

a success or failure code.

### soup_socket_listen ()

    gboolean
    soup_socket_listen (SoupSocket *sock);

Makes `sock` start listening on its local address. When connections come in, `sock` will emit “new_connection”.

#### Parameters

sock | a server SoupSocket (which must not already be connected or listening) |

#### Returns

whether or not `sock` is now listening.

### soup_socket_start_ssl ()

    gboolean
    soup_socket_start_ssl (SoupSocket *sock,
                           GCancellable *cancellable);

Starts using SSL on `socket` .

#### Parameters

sock | the socket |
cancellable | a GCancellable |

#### Returns

success or failure

### soup_socket_start_proxy_ssl ()

    gboolean
    soup_socket_start_proxy_ssl (SoupSocket *sock,
                                 const char *ssl_host,
                                 GCancellable *cancellable);

Starts using SSL on `socket` , expecting to find a host named `ssl_host` .

#### Parameters

sock | the socket |
ssl_host | hostname of the SSL server |
cancellable | a GCancellable |

#### Returns

success or failure

### soup_socket_is_ssl ()

    gboolean
    soup_socket_is_ssl (SoupSocket *sock);

Tests if `sock` is doing (or has attempted to do) SSL.

#### Parameters

sock | a SoupSocket |

#### Returns

`TRUE` if `sock` has SSL credentials set

### soup_socket_disconnect ()

    void
    soup_socket_disconnect (SoupSocket *sock);

Disconnects `sock` . Any further read or write attempts on it will fail.

#### Parameters

sock | a SoupSocket |

### soup_socket_is_connected ()

    gboolean
    soup_socket_is_connected (SoupSocket *sock);

Tests if `sock` is connected to another host

#### Parameters

sock | a SoupSocket |

#### Returns

`TRUE` or `FALSE`.

### soup_socket_get_local_address ()

    SoupAddress *
    soup_socket_get_local_address (SoupSocket *sock);

Returns the SoupAddress corresponding to the local end of `sock` .

Calling this method on an unconnected socket is considered to be an error, and produces undefined results.

#### Parameters

sock | a SoupSocket |

#### Returns

the SoupAddress.

[transfer none]

### soup_socket_get_remote_address ()

    SoupAddress *
    soup_socket_get_remote_address (SoupSocket *sock);

Returns the SoupAddress corresponding to the remote end of `sock` .

Calling this method on an unconnected socket is considered to be an error, and produces undefined results.

#### Parameters

sock | a SoupSocket |

#### Returns

the SoupAddress.

[transfer none]

### soup_socket_get_fd ()

    int
    soup_socket_get_fd (SoupSocket *sock);

Gets `sock` 's underlying file descriptor.

Note that fiddling with the file descriptor may break the SoupSocket.

#### Parameters

sock | a SoupSocket |

#### Returns

`sock` 's file descriptor.

### soup_socket_read ()

    SoupSocketIOStatus
    soup_socket_read (SoupSocket *sock,
                      gpointer buffer,
                      gsize len,
                      gsize *nread,
                      GCancellable *cancellable,
                      GError **error);

Attempts to read up to `len` bytes from `sock` into `buffer` . If some data is successfully read, `soup_socket_read()` will return `SOUP_SOCKET_OK`, and *`nread` will contain the number of bytes actually read (which may be less than `len` ).

If `sock` is non-blocking, and no data is available, the return value will be `SOUP_SOCKET_WOULD_BLOCK`. In this case, the caller can connect to the “readable” signal to know when there is more data to read. (NB: You MUST read all available data off the socket first. “readable” is only emitted after `soup_socket_read()` returns `SOUP_SOCKET_WOULD_BLOCK`, and it is only emitted once. See the documentation for “non-blocking”.)

#### Parameters

sock | the socket |
buffer | buffer to read into. | [array length=len][element-type guint8]
len | size of `buffer` in bytes |
nread | on return, the number of bytes read into `buffer` . | [out]
cancellable | a GCancellable, or `NULL` |
error | error pointer |

#### Returns

a SoupSocketIOStatus, as described above (or `SOUP_SOCKET_EOF` if the socket is no longer connected, or `SOUP_SOCKET_ERROR` on any other error, in which case `error` will also be set).

### soup_socket_read_until ()

    SoupSocketIOStatus
    soup_socket_read_until (SoupSocket *sock,
                            gpointer buffer,
                            gsize len,
                            gconstpointer boundary,
                            gsize boundary_len,
                            gsize *nread,
                            gboolean *got_boundary,
                            GCancellable *cancellable,
                            GError **error);

Like `soup_socket_read()`, but reads no further than the first occurrence of `boundary` . (If the boundary is found, it will be included in the returned data, and *`got_boundary` will be set to `TRUE`.) Any data after the boundary will returned in future reads.

soup_socket_read_until() will almost always return fewer than `len` bytes: if the boundary is found, then it will only return the bytes up until the end of the boundary, and if the boundary is not found, then it will leave the last `(boundary_len - 1)` bytes in its internal buffer, in case they form the start of the boundary string. Thus, `len` normally needs to be at least 1 byte longer than `boundary_len` if you want to make any progress at all.

#### Parameters

sock | the socket |
buffer | buffer to read into. | [array length=len][element-type guint8]
len | size of `buffer` in bytes |
boundary | boundary to read until |
boundary_len | length of `boundary` in bytes |
nread | on return, the number of bytes read into `buffer` . | [out]
got_boundary | on return, whether or not the data in `buffer` ends with the boundary string. | [out]
cancellable | a GCancellable, or `NULL` |
error | error pointer |

#### Returns

as for `soup_socket_read()`

### soup_socket_write ()

    SoupSocketIOStatus
    soup_socket_write (SoupSocket *sock,
                       gconstpointer buffer,
                       gsize len,
                       gsize *nwrote,
                       GCancellable *cancellable,
                       GError **error);

Attempts to write `len` bytes from `buffer` to `sock` . If some data is successfully written, the return status will be `SOUP_SOCKET_OK`, and *`nwrote` will contain the number of bytes actually written (which may be less than `len` ).

If `sock` is non-blocking, and no data could be written right away, the return value will be `SOUP_SOCKET_WOULD_BLOCK`. In this case, the caller can connect to the “writable” signal to know when more data can be written. (NB: “writable” is only emitted after `soup_socket_write()` returns `SOUP_SOCKET_WOULD_BLOCK`, and it is only emitted once. See the documentation for “non-blocking”.)

#### Parameters

sock | the socket |
buffer | data to write. | [array length=len][element-type guint8]
len | size of `buffer` , in bytes |
nwrote | on return, number of bytes written. | [out]
cancellable | a GCancellable, or `NULL` |
error | error pointer |

#### Returns

a SoupSocketIOStatus, as described above (or `SOUP_SOCKET_EOF` or `SOUP_SOCKET_ERROR`. `error` will be set if the return value is `SOUP_SOCKET_ERROR`.)

## Types and Values

### SoupSocket

    typedef struct _SoupSocket SoupSocket;

### enum SoupSocketIOStatus

Return value from the SoupSocket IO methods.

#### Members

SOUP_SOCKET_OK | Success |
SOUP_SOCKET_WOULD_BLOCK | Cannot read/write any more at this time |
SOUP_SOCKET_EOF | End of file |
SOUP_SOCKET_ERROR | Other error |

### SOUP_SOCKET_LOCAL_ADDRESS

    #define SOUP_SOCKET_LOCAL_ADDRESS       "local-address"

Alias for the “local-address” property. (Address of local end of socket.)

### SOUP_SOCKET_REMOTE_ADDRESS

    #define SOUP_SOCKET_REMOTE_ADDRESS      "remote-address"

Alias for the “remote-address” property. (Address of remote end of socket.)

### SOUP_SOCKET_FLAG_NONBLOCKING

    #define SOUP_SOCKET_FLAG_NONBLOCKING    "non-blocking"

Alias for the “non-blocking” property. (Whether or not the socket uses non-blocking I/O.)

### SOUP_SOCKET_IS_SERVER

    #define SOUP_SOCKET_IS_SERVER           "is-server"

Alias for the “is-server” property, qv.

### SOUP_SOCKET_SSL_CREDENTIALS

    #define SOUP_SOCKET_SSL_CREDENTIALS     "ssl-creds"

Alias for the “ssl-creds” property. (SSL credential information.)

### SOUP_SOCKET_ASYNC_CONTEXT

    #define SOUP_SOCKET_ASYNC_CONTEXT       "async-context"

Alias for the “async-context” property. (The socket's GMainContext.)

### SOUP_SOCKET_TIMEOUT

    #define SOUP_SOCKET_TIMEOUT             "timeout"

Alias for the “timeout” property. (The timeout in seconds for blocking socket I/O operations.)

### SOUP_SOCKET_SSL_FALLBACK

    #define SOUP_SOCKET_SSL_FALLBACK        "ssl-fallback"

Alias for the “ssl-fallback” property.

### SOUP_SOCKET_SSL_STRICT

    #define SOUP_SOCKET_SSL_STRICT          "ssl-strict"

Alias for the “ssl-strict” property.

### SOUP_SOCKET_TLS_CERTIFICATE

    #define SOUP_SOCKET_TLS_CERTIFICATE     "tls-certificate"

Alias for the “tls-certificate” property. Note that this property's value is only useful if the socket is for a TLS connection, and only reliable after some data has been transferred to or from it.

Since: 2.34

### SOUP_SOCKET_TLS_ERRORS

    #define SOUP_SOCKET_TLS_ERRORS          "tls-errors"

Alias for the “tls-errors” property. Note that this property's value is only useful if the socket is for a TLS connection, and only reliable after some data has been transferred to or from it.

Since: 2.34

### SOUP_SOCKET_TRUSTED_CERTIFICATE

    #define SOUP_SOCKET_TRUSTED_CERTIFICATE "trusted-certificate"

Alias for the “trusted-certificate” property.

### SOUP_SOCKET_USE_THREAD_CONTEXT

    #define SOUP_SOCKET_USE_THREAD_CONTEXT  "use-thread-context"

Alias for the “use-thread-context” property. (Use `g_main_context_get_thread_default()`)

Since: 2.38

## Property Details

### The `“async-context”` property

      “async-context”            gpointer

The GMainContext to dispatch this socket's async I/O in.

Owner: SoupSocket

Flags: Read / Write / Construct Only

### The `“fd”` property

      “fd”                       int

The socket's file descriptor.

Owner: SoupSocket

Flags: Read / Write / Construct Only

Allowed values: >= -1

Default value: -1

### The `“gsocket”` property

      “gsocket”                  GSocket *

The socket's underlying GSocket.

Owner: SoupSocket

Flags: Write / Construct Only

### The `“iostream”` property

      “iostream”                 GIOStream *

The socket's underlying GIOStream.

Owner: SoupSocket

Flags: Write / Construct Only

### The `“ipv6-only”` property

      “ipv6-only”                gboolean

IPv6 only.

Owner: SoupSocket

Flags: Read / Write

Default value: FALSE

### The `“is-server”` property

      “is-server”                gboolean

Whether or not the socket is a server socket.

Note that for "ordinary" SoupSockets this will be set for both listening sockets and the sockets emitted by “new-connection”, but for sockets created by setting “fd”, it will only be set for listening sockets.

Owner: SoupSocket

Flags: Read

Default value: FALSE

### The `“local-address”` property

      “local-address”            SoupAddress *

Address of local end of socket.

Owner: SoupSocket

Flags: Read / Write / Construct Only

### The `“non-blocking”` property

      “non-blocking”             gboolean

Whether or not the socket uses non-blocking I/O.

SoupSocket's I/O methods are designed around the idea of using a single codepath for both synchronous and asynchronous I/O. If you want to read off a SoupSocket, the "correct" way to do it is to call `soup_socket_read()` or `soup_socket_read_until()` repeatedly until you have read everything you want. If it returns `SOUP_SOCKET_WOULD_BLOCK` at any point, stop reading and wait for it to emit the “readable” signal. Then go back to the reading-as-much-as-you-can loop. Likewise, for writing to a SoupSocket, you should call `soup_socket_write()` either until you have written everything, or it returns `SOUP_SOCKET_WOULD_BLOCK` (in which case you wait for “writable” and then go back into the loop).

Code written this way will work correctly with both blocking and non-blocking sockets; blocking sockets will simply never return `SOUP_SOCKET_WOULD_BLOCK`, and so the code that handles that case just won't get used for them.

Owner: SoupSocket

Flags: Read / Write

Default value: TRUE

### The `“remote-address”` property

      “remote-address”           SoupAddress *

Address of remote end of socket.

Owner: SoupSocket

Flags: Read / Write / Construct Only

### The `“socket-properties”` property

      “socket-properties”        SoupSocketProperties *

Socket properties.

Owner: SoupSocket

Flags: Write

### The `“ssl-creds”` property

      “ssl-creds”                gpointer

SSL credential information, passed from the session to the SSL implementation.

Owner: SoupSocket

Flags: Read / Write

### The `“ssl-fallback”` property

      “ssl-fallback”             gboolean

Use SSLv3 instead of TLS (client-side only).

Owner: SoupSocket

Flags: Read / Write / Construct Only

Default value: FALSE

### The `“ssl-strict”` property

      “ssl-strict”               gboolean

Whether certificate errors should be considered a connection error.

Owner: SoupSocket

Flags: Read / Write / Construct Only

Default value: TRUE

### The `“timeout”` property

      “timeout”                  guint

Value in seconds to timeout a blocking I/O.

Owner: SoupSocket

Flags: Read / Write

Default value: 0

### The `“tls-certificate”` property

      “tls-certificate”          GTlsCertificate *

The peer's TLS certificate.

Owner: SoupSocket

Flags: Read

### The `“tls-errors”` property

      “tls-errors”               GTlsCertificateFlags

Errors with the peer's TLS certificate.

Owner: SoupSocket

Flags: Read

### The `“trusted-certificate”` property

      “trusted-certificate”      gboolean

Whether the server certificate is trusted, if this is an SSL socket.

Owner: SoupSocket

Flags: Read

Default value: FALSE

### The `“use-thread-context”` property

      “use-thread-context”       gboolean

Use `g_main_context_get_thread_default()`.

Owner: SoupSocket

Flags: Read / Write / Construct Only

Default value: FALSE

Since: 2.38

## Signal Details

### The `“disconnected”` signal

    void
    user_function (SoupSocket *sock,
                   gpointer    user_data)

Emitted when the socket is disconnected, for whatever reason.

#### Parameters

sock | the socket |
user_data | user data set when the signal handler was connected. |

Flags: Run Last

### The `“event”` signal

    void
    user_function (SoupSocket        *sock,
                   GSocketClientEvent event,
                   GIOStream         *connection,
                   gpointer           user_data)

Emitted when a network-related event occurs. See “event” for more details.

#### Parameters

sock | the socket |
event | the event that occurred |
connection | the current connection state |
user_data | user data set when the signal handler was connected. |

Flags: Run Last

Since: 2.38

### The `“new-connection”` signal

    void
    user_function (SoupSocket *sock,
                   SoupSocket *new,
                   gpointer    user_data)

Emitted when a listening socket (set up with `soup_socket_listen()`) receives a new connection.

You must ref the `new` if you want to keep it; otherwise it will be destroyed after the signal is emitted.

#### Parameters

sock | the socket |
new | the new socket |
user_data | user data set when the signal handler was connected. |

Flags: Run First

### The `“readable”` signal

    void
    user_function (SoupSocket *sock,
                   gpointer    user_data)

Emitted when an async socket is readable. See `soup_socket_read()`, `soup_socket_read_until()` and “non-blocking”.

#### Parameters

sock | the socket |
user_data | user data set when the signal handler was connected. |

Flags: Run Last

### The `“writable”` signal

    void
    user_function (SoupSocket *sock,
                   gpointer    user_data)

Emitted when an async socket is writable. See `soup_socket_write()` and “non-blocking”.

#### Parameters

sock | the socket |
user_data | user data set when the signal handler was connected. |

Flags: Run Last