GSocketListener: GIO Reference Manual

## GSocketListener GSocketListener — Helper for accepting network client connections |

## Functions

GSocketListener * | g_socket_listener_new ()
gboolean | g_socket_listener_add_socket ()
gboolean | g_socket_listener_add_address ()
gboolean | g_socket_listener_add_inet_port ()
guint16 | g_socket_listener_add_any_inet_port ()
GSocketConnection * | g_socket_listener_accept ()
void | g_socket_listener_accept_async ()
GSocketConnection * | g_socket_listener_accept_finish ()
GSocket * | g_socket_listener_accept_socket ()
void | g_socket_listener_accept_socket_async ()
GSocket * | g_socket_listener_accept_socket_finish ()
void | g_socket_listener_close ()
void | g_socket_listener_set_backlog ()

## Properties

gint | listen-backlog | Read / Write / Construct

## Signals

void | event | Run Last

## Types and Values

| GSocketListener
enum | GSocketListenerEvent

## Object Hierarchy

        GObject
        ╰── GSocketListener
            ╰── GSocketService

## Includes

    #include <gio/gio.h>

## Description

A GSocketListener is an object that keeps track of a set of server sockets and helps you accept sockets from any of the socket, either sync or async.

Add addresses and ports to listen on using `g_socket_listener_add_address()` and `g_socket_listener_add_inet_port()`. These will be listened on until `g_socket_listener_close()` is called. Dropping your final reference to the GSocketListener will not cause `g_socket_listener_close()` to be called implicitly, as some references to the GSocketListener may be held internally.

If you want to implement a network server, also look at GSocketService and GThreadedSocketService which are subclasses of GSocketListener that make this even easier.

## Functions

### g_socket_listener_new ()

    GSocketListener *
    g_socket_listener_new (void);

Creates a new GSocketListener with no sockets to listen for. New listeners can be added with e.g. `g_socket_listener_add_address()` or `g_socket_listener_add_inet_port()`.

#### Returns

a new GSocketListener.

Since: 2.22

### g_socket_listener_add_socket ()

    gboolean
    g_socket_listener_add_socket (GSocketListener *listener,
                                  GSocket *socket,
                                  GObject *source_object,
                                  GError **error);

Adds `socket` to the set of sockets that we try to accept new clients from. The socket must be bound to a local address and listened to.

`source_object` will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.

The `socket` will not be automatically closed when the `listener` is finalized unless the listener held the final reference to the socket. Before GLib 2.42, the `socket` was automatically closed on finalization of the `listener` , even if references to it were held elsewhere.

#### Parameters

listener | a GSocketListener |
socket | a listening GSocket |
source_object | Optional GObject identifying this source. | [nullable]
error | GError for error reporting, or `NULL` to ignore. |

#### Returns

`TRUE` on success, `FALSE` on error.

Since: 2.22

### g_socket_listener_add_address ()

    gboolean
    g_socket_listener_add_address (GSocketListener *listener,
                                   GSocketAddress *address,
                                   GSocketType type,
                                   GSocketProtocol protocol,
                                   GObject *source_object,
                                   GSocketAddress **effective_address,
                                   GError **error);

Creates a socket of type `type` and protocol `protocol` , binds it to `address` and adds it to the set of sockets we're accepting sockets from.

Note that adding an IPv6 address, depending on the platform, may or may not result in a listener that also accepts IPv4 connections. For more deterministic behavior, see `g_socket_listener_add_inet_port()`.

`source_object` will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.

If successful and `effective_address` is non-`NULL` then it will be set to the address that the binding actually occurred at. This is helpful for determining the port number that was used for when requesting a binding to port 0 (ie: "any port"). This address, if requested, belongs to the caller and must be freed.

Call `g_socket_listener_close()` to stop listening on `address` ; this will not be done automatically when you drop your final reference to `listener` , as references may be held internally.

#### Parameters

listener | a GSocketListener |
address | a GSocketAddress |
type | a GSocketType |
protocol | a GSocketProtocol |
source_object | Optional GObject identifying this source. | [nullable]
effective_address | location to store the address that was bound to, or `NULL`. | [out][optional]
error | GError for error reporting, or `NULL` to ignore. |

#### Returns

`TRUE` on success, `FALSE` on error.

Since: 2.22

### g_socket_listener_add_inet_port ()

    gboolean
    g_socket_listener_add_inet_port (GSocketListener *listener,
                                     guint16 port,
                                     GObject *source_object,
                                     GError **error);

Helper function for `g_socket_listener_add_address()` that creates a TCP/IP socket listening on IPv4 and IPv6 (if supported) on the specified port on all interfaces.

`source_object` will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.

Call `g_socket_listener_close()` to stop listening on `port` ; this will not be done automatically when you drop your final reference to `listener` , as references may be held internally.

#### Parameters

listener | a GSocketListener |
port | an IP port number (non-zero) |
source_object | Optional GObject identifying this source. | [nullable]
error | GError for error reporting, or `NULL` to ignore. |

#### Returns

`TRUE` on success, `FALSE` on error.

Since: 2.22

### g_socket_listener_add_any_inet_port ()

    guint16
    g_socket_listener_add_any_inet_port (GSocketListener *listener,
                                         GObject *source_object,
                                         GError **error);

Listens for TCP connections on any available port number for both IPv6 and IPv4 (if each is available).

This is useful if you need to have a socket for incoming connections but don't care about the specific port number.

`source_object` will be passed out in the various calls to accept to identify this particular source, which is useful if you're listening on multiple addresses and do different things depending on what address is connected to.

#### Parameters

listener | a GSocketListener |
source_object | Optional GObject identifying this source. | [nullable]
error | a GError location to store the error occurring, or `NULL` to ignore. |

#### Returns

the port number, or 0 in case of failure.

Since: 2.24

### g_socket_listener_accept ()

    GSocketConnection *
    g_socket_listener_accept (GSocketListener *listener,
                              GObject **source_object,
                              GCancellable *cancellable,
                              GError **error);

Blocks waiting for a client to connect to any of the sockets added to the listener. Returns a GSocketConnection for the socket that was accepted.

If `source_object` is not `NULL` it will be filled out with the source object specified when the corresponding socket or address was added to the listener.

If `cancellable` is not `NULL`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error `G_IO_ERROR_CANCELLED` will be returned.

#### Parameters

listener | a GSocketListener |
source_object | location where GObject pointer will be stored, or `NULL`. | [out][transfer none][optional][nullable]
cancellable | optional GCancellable object, `NULL` to ignore. | [nullable]
error | GError for error reporting, or `NULL` to ignore. |

#### Returns

a GSocketConnection on success, `NULL` on error.

[transfer full]

Since: 2.22

### g_socket_listener_accept_async ()

    void
    g_socket_listener_accept_async (GSocketListener *listener,
                                    GCancellable *cancellable,
                                    GAsyncReadyCallback callback,
                                    gpointer user_data);

This is the asynchronous version of `g_socket_listener_accept()`.

When the operation is finished `callback` will be called. You can then call `g_socket_listener_accept_socket()` to get the result of the operation.

#### Parameters

listener | a GSocketListener |
cancellable | a GCancellable, or `NULL`. | [nullable]
callback | a GAsyncReadyCallback. | [scope async]
user_data | user data for the callback. | [closure]

Since: 2.22

### g_socket_listener_accept_finish ()

    GSocketConnection *
    g_socket_listener_accept_finish (GSocketListener *listener,
                                     GAsyncResult *result,
                                     GObject **source_object,
                                     GError **error);

Finishes an async accept operation. See `g_socket_listener_accept_async()`

#### Parameters

listener | a GSocketListener |
result | a GAsyncResult. |
source_object | Optional GObject identifying this source. | [out][transfer none][optional][nullable]
error | a GError location to store the error occurring, or `NULL` to ignore. |

#### Returns

a GSocketConnection on success, `NULL` on error.

[transfer full]

Since: 2.22

### g_socket_listener_accept_socket ()

    GSocket *
    g_socket_listener_accept_socket (GSocketListener *listener,
                                     GObject **source_object,
                                     GCancellable *cancellable,
                                     GError **error);

Blocks waiting for a client to connect to any of the sockets added to the listener. Returns the GSocket that was accepted.

If you want to accept the high-level GSocketConnection, not a GSocket, which is often the case, then you should use `g_socket_listener_accept()` instead.

If `source_object` is not `NULL` it will be filled out with the source object specified when the corresponding socket or address was added to the listener.

If `cancellable` is not `NULL`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error `G_IO_ERROR_CANCELLED` will be returned.

#### Parameters

listener | a GSocketListener |
source_object | location where GObject pointer will be stored, or `NULL`. | [out][transfer none][optional][nullable]
cancellable | optional GCancellable object, `NULL` to ignore. | [nullable]
error | GError for error reporting, or `NULL` to ignore. |

#### Returns

a GSocket on success, `NULL` on error.

[transfer full]

Since: 2.22

### g_socket_listener_accept_socket_async ()

    void
    g_socket_listener_accept_socket_async (GSocketListener *listener,
                                           GCancellable *cancellable,
                                           GAsyncReadyCallback callback,
                                           gpointer user_data);

This is the asynchronous version of `g_socket_listener_accept_socket()`.

When the operation is finished `callback` will be called. You can then call `g_socket_listener_accept_socket_finish()` to get the result of the operation.

#### Parameters

listener | a GSocketListener |
cancellable | a GCancellable, or `NULL`. | [nullable]
callback | a GAsyncReadyCallback. | [scope async]
user_data | user data for the callback. | [closure]

Since: 2.22

### g_socket_listener_accept_socket_finish ()

    GSocket *
    g_socket_listener_accept_socket_finish
                                   (GSocketListener *listener,
                                    GAsyncResult *result,
                                    GObject **source_object,
                                    GError **error);

Finishes an async accept operation. See `g_socket_listener_accept_socket_async()`

#### Parameters

listener | a GSocketListener |
result | a GAsyncResult. |
source_object | Optional GObject identifying this source. | [out][transfer none][optional][nullable]
error | a GError location to store the error occurring, or `NULL` to ignore. |

#### Returns

a GSocket on success, `NULL` on error.

[transfer full]

Since: 2.22

### g_socket_listener_close ()

    void
    g_socket_listener_close (GSocketListener *listener);

Closes all the sockets in the listener.

#### Parameters

listener | a GSocketListener |

Since: 2.22

### g_socket_listener_set_backlog ()

    void
    g_socket_listener_set_backlog (GSocketListener *listener,
                                   int listen_backlog);

Sets the listen backlog on the sockets in the listener.

See `g_socket_set_listen_backlog()` for details

#### Parameters

listener | a GSocketListener |
listen_backlog | an integer |

Since: 2.22

## Types and Values

### GSocketListener

    typedef struct _GSocketListener GSocketListener;

A helper class for network servers to listen for and accept connections.

Since: 2.22

### enum GSocketListenerEvent

Describes an event occurring on a GSocketListener. See the “event” signal for more details.

Additional values may be added to this type in the future.

#### Members

G_SOCKET_LISTENER_BINDING | The listener is about to bind a socket. |
G_SOCKET_LISTENER_BOUND | The listener has bound a socket. |
G_SOCKET_LISTENER_LISTENING | The listener is about to start listening on this socket. |
G_SOCKET_LISTENER_LISTENED | The listener is now listening on this socket. |

Since: 2.46

## Property Details

### The `“listen-backlog”` property

      “listen-backlog”           gint

outstanding connections in the listen queue.

Flags: Read / Write / Construct

Allowed values: [0,2000]

Default value: 10

## Signal Details

### The `“event”` signal

    void
    user_function (GSocketListener     *listener,
                   GSocketListenerEvent event,
                   GSocket             *socket,
                   gpointer             user_data)

Emitted when `listener` 's activity on `socket` changes state. Note that when `listener` is used to listen on both IPv4 and IPv6, a separate set of signals will be emitted for each, and the order they happen in is undefined.

#### Parameters

listener | the GSocketListener |
event | the event that is occurring |
socket | the GSocket the event is occurring on |
user_data | user data set when the signal handler was connected. |

Flags: Run Last

Since: 2.46

## See Also

GThreadedSocketService, GSocketService.