glibmm: Gio::SocketListener Class Reference

glibmm 2.28.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Examples

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Static Public Member Functions | Protected Member Functions | Related Functions
Gio::SocketListener Class Reference
Portable Network I/O Functionality

Helper for accepting network client connections. More...

`#include <giomm/socketlistener.h>`

Inheritance diagram for Gio::SocketListener:
[legend]

List of all members.

## Public Member Functions
virtual | ~SocketListener ()
GSocketListener* | gobj ()
| Provides access to the underlying C GObject.
const GSocketListener* | gobj () const
| Provides access to the underlying C GObject.
GSocketListener* | gobj_copy ()
| Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.
void | set_backlog (int listen_backlog)
| Sets the listen backlog on the sockets in the listener.
bool | add_socket (const Glib::RefPtr< Socket >& socket, const Glib::RefPtr< Glib::Object >& source_object)
| Adds socket to the set of sockets that we try to accept new clients from.
bool | add_socket (const Glib::RefPtr< Socket >& socket)
| Adds socket to the set of sockets that we try to accept new clients from.
bool | add_address (const Glib::RefPtr< SocketAddress >& address, SocketType type, SocketProtocol protocol, const Glib::RefPtr< Glib::Object >& source_object, Glib::RefPtr< SocketAddress >& effective_address)
| 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.
bool | add_address (const Glib::RefPtr< SocketAddress >& address, SocketType type, SocketProtocol protocol, Glib::RefPtr< SocketAddress >& effective_address)
| 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.
bool | add_inet_port (guint16 port, const Glib::RefPtr< Glib::Object >& source_object)
| 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.
bool | add_inet_port (guint16 port)
| 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.
guint16 | add_any_inet_port (const Glib::RefPtr< Glib::Object >& source_object)
| Listens for TCP connections on any available port number for both IPv6 and IPv4 (if each are available).
guint16 | add_any_inet_port ()
| Listens for TCP connections on any available port number for both IPv6 and IPv4 (if each are available).
Glib::RefPtr< Socket > | accept_socket (Glib::RefPtr< Glib::Object >& source_object, const Glib::RefPtr< Cancellable >& cancellable)
| Blocks waiting for a client to connect to any of the sockets added to the listener.
Glib::RefPtr< Socket > | accept_socket (Glib::RefPtr< Glib::Object >& source_object)
| Blocks waiting for a client to connect to any of the sockets added to the listener.
Glib::RefPtr< Socket > | accept_socket (const Glib::RefPtr< Cancellable >& cancellable)
| Blocks waiting for a client to connect to any of the sockets added to the listener.
Glib::RefPtr< Socket > | accept_socket ()
| Blocks waiting for a client to connect to any of the sockets added to the listener.
void | accept_socket_async (const Glib::RefPtr< Cancellable >& cancellable, const SlotAsyncReady& slot)
| This is the asynchronous version of g_socket_listener_accept_socket().
void | accept_socket_async (const SlotAsyncReady& slot)
| This is the asynchronous version of g_socket_listener_accept_socket().
Glib::RefPtr< Socket > | accept_socket_finish (const Glib::RefPtr< AsyncResult >& result, Glib::RefPtr< Glib::Object >& source_object)
| Finishes an async accept operation.
Glib::RefPtr< Socket > | accept_socket_finish (const Glib::RefPtr< AsyncResult >& result)
| Finishes an async accept operation.
Glib::RefPtr< SocketConnection > | accept (Glib::RefPtr< Object >& source_object, const Glib::RefPtr< Cancellable >& cancellable)
| Blocks waiting for a client to connect to any of the sockets added to the listener.
Glib::RefPtr< SocketConnection > | accept (Glib::RefPtr< Object >& source_object)
| Blocks waiting for a client to connect to any of the sockets added to the listener.
Glib::RefPtr< SocketConnection > | accept (const Glib::RefPtr< Cancellable >& cancellable)
| Blocks waiting for a client to connect to any of the sockets added to the listener.
Glib::RefPtr< SocketConnection > | accept ()
| Blocks waiting for a client to connect to any of the sockets added to the listener.
void | accept_async (const SlotAsyncReady& slot)
| This is the asynchronous version of g_socket_listener_accept().
void | accept_async (const Glib::RefPtr< Cancellable >& cancellable, const SlotAsyncReady& slot)
| This is the asynchronous version of g_socket_listener_accept().
Glib::RefPtr< SocketConnection > | accept_finish (const Glib::RefPtr< AsyncResult >& result, Glib::RefPtr< Glib::Object >& source_object)
| Finishes an async accept operation.
Glib::RefPtr< SocketConnection > | accept_finish (const Glib::RefPtr< AsyncResult >& result)
| Finishes an async accept operation.
void | close ()
| Closes all the sockets in the listener.
Glib::PropertyProxy< int > | property_listen_backlog ()
| outstanding connections in the listen queue.
Glib::PropertyProxy_ReadOnly< int > | property_listen_backlog () const
| outstanding connections in the listen queue.
## Static Public Member Functions
static Glib::RefPtr < SocketListener > | create ()
## Protected Member Functions
| SocketListener ()
## Related Functions
(Note that these are not member functions.)
Glib::RefPtr< Gio::SocketListener > | wrap (GSocketListener* object, bool take_copy=false)
| A Glib::wrap() method for this object.

## Detailed Description

Helper for accepting network client connections.

See also:
  ThreadedSocketService, SocketService.

A SocketListener 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.

If you want to implement a network server, also look at SocketService and ThreadedSocketService which are subclass of SocketListener that makes this even easier.

Since glibmm 2.24:

## Constructor & Destructor Documentation

virtual Gio::SocketListener::~SocketListener | ( |  | ) | `[virtual]`

Gio::SocketListener::SocketListener | ( |  | ) | `[protected]`

## Member Function Documentation

Glib::RefPtr<SocketConnection> Gio::SocketListener::accept | ( | Glib::RefPtr< Object >& | source_object,
|  | const Glib::RefPtr< Cancellable >& | cancellable
| ) |  |

Blocks waiting for a client to connect to any of the sockets added to the listener.

Returns a SocketConnection for the socket that was accepted.

If source_object is not `0` 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 `0`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IO_ERROR_CANCELLED will be returned.

Since glibmm 2.22:

Parameters:

  source_object | Location where Object pointer will be stored, or `0`.
  cancellable | Optional Cancellable object, `0` to ignore.

Returns:
  A SocketConnection on success, `0` on error.

Glib::RefPtr<SocketConnection> Gio::SocketListener::accept | ( | Glib::RefPtr< Object >& | source_object | ) |

Blocks waiting for a client to connect to any of the sockets added to the listener.

Returns a SocketConnection for the socket that was accepted.

If source_object is not `0` 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 `0`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IO_ERROR_CANCELLED will be returned.

Since glibmm 2.22:

Parameters:

  source_object | Location where Object pointer will be stored, or `0`.
  cancellable | Optional Cancellable object, `0` to ignore.

Returns:
  A SocketConnection on success, `0` on error.

Glib::RefPtr<SocketConnection> Gio::SocketListener::accept | ( | const Glib::RefPtr< Cancellable >& | cancellable | ) |

Blocks waiting for a client to connect to any of the sockets added to the listener.

Returns a SocketConnection for the socket that was accepted.

If source_object is not `0` 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 `0`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IO_ERROR_CANCELLED will be returned.

Since glibmm 2.22:

Parameters:

  source_object | Location where Object pointer will be stored, or `0`.
  cancellable | Optional Cancellable object, `0` to ignore.

Returns:
  A SocketConnection on success, `0` on error.

Glib::RefPtr<SocketConnection> Gio::SocketListener::accept | ( |  | ) |

Blocks waiting for a client to connect to any of the sockets added to the listener.

Returns a SocketConnection for the socket that was accepted.

If source_object is not `0` 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 `0`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IO_ERROR_CANCELLED will be returned.

Since glibmm 2.22:

Parameters:

  source_object | Location where Object pointer will be stored, or `0`.
  cancellable | Optional Cancellable object, `0` to ignore.

Returns:
  A SocketConnection on success, `0` on error.

void Gio::SocketListener::accept_async | ( | const SlotAsyncReady& | slot | ) |

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.

Since glibmm 2.22:

Parameters:

  cancellable | A Cancellable, or `0`.
  callback | A AsyncReadyCallback.
  user_data | User data for the callback.

void Gio::SocketListener::accept_async | ( | const Glib::RefPtr< Cancellable >& | cancellable,
|  | const SlotAsyncReady& | slot
| ) |  |

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.

Since glibmm 2.22:

Parameters:

  cancellable | A Cancellable, or `0`.
  callback | A AsyncReadyCallback.
  user_data | User data for the callback.

Glib::RefPtr<SocketConnection> Gio::SocketListener::accept_finish | ( | const Glib::RefPtr< AsyncResult >& | result,
|  | Glib::RefPtr< Glib::Object >& | source_object
| ) |  |

Finishes an async accept operation.

See g_socket_listener_accept_async()

Since glibmm 2.22:

Parameters:

  result | A AsyncResult.
  source_object | Optional Object identifying this source.

Returns:
  A SocketConnection on success, `0` on error.

Glib::RefPtr<SocketConnection> Gio::SocketListener::accept_finish | ( | const Glib::RefPtr< AsyncResult >& | result | ) |

Finishes an async accept operation.

See g_socket_listener_accept_async()

Since glibmm 2.22:

Parameters:

  result | A AsyncResult.
  source_object | Optional Object identifying this source.

Returns:
  A SocketConnection on success, `0` on error.

Glib::RefPtr<Socket> Gio::SocketListener::accept_socket | ( |  | ) |

Blocks waiting for a client to connect to any of the sockets added to the listener.

Returns the Socket that was accepted.

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

If source_object is not `0` 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 `0`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IO_ERROR_CANCELLED will be returned.

Since glibmm 2.22:

Parameters:

  source_object | Location where Object pointer will be stored, or `0`.
  cancellable | Optional Cancellable object, `0` to ignore.

Returns:
  A Socket on success, `0` on error.

Glib::RefPtr<Socket> Gio::SocketListener::accept_socket | ( | Glib::RefPtr< Glib::Object >& | source_object,
|  | const Glib::RefPtr< Cancellable >& | cancellable
| ) |  |

Blocks waiting for a client to connect to any of the sockets added to the listener.

Returns the Socket that was accepted.

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

If source_object is not `0` 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 `0`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IO_ERROR_CANCELLED will be returned.

Since glibmm 2.22:

Parameters:

  source_object | Location where Object pointer will be stored, or `0`.
  cancellable | Optional Cancellable object, `0` to ignore.

Returns:
  A Socket on success, `0` on error.

Glib::RefPtr<Socket> Gio::SocketListener::accept_socket | ( | const Glib::RefPtr< Cancellable >& | cancellable | ) |

Blocks waiting for a client to connect to any of the sockets added to the listener.

Returns the Socket that was accepted.

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

If source_object is not `0` 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 `0`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IO_ERROR_CANCELLED will be returned.

Since glibmm 2.22:

Parameters:

  source_object | Location where Object pointer will be stored, or `0`.
  cancellable | Optional Cancellable object, `0` to ignore.

Returns:
  A Socket on success, `0` on error.

Glib::RefPtr<Socket> Gio::SocketListener::accept_socket | ( | Glib::RefPtr< Glib::Object >& | source_object | ) |

Blocks waiting for a client to connect to any of the sockets added to the listener.

Returns the Socket that was accepted.

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

If source_object is not `0` 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 `0`, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error IO_ERROR_CANCELLED will be returned.

Since glibmm 2.22:

Parameters:

  source_object | Location where Object pointer will be stored, or `0`.
  cancellable | Optional Cancellable object, `0` to ignore.

Returns:
  A Socket on success, `0` on error.

void Gio::SocketListener::accept_socket_async | ( | const Glib::RefPtr< Cancellable >& | cancellable,
|  | const SlotAsyncReady& | slot
| ) |  |

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.

Since glibmm 2.22:

Parameters:

  cancellable | A Cancellable, or `0`.
  callback | A AsyncReadyCallback.
  user_data | User data for the callback.

void Gio::SocketListener::accept_socket_async | ( | const SlotAsyncReady& | slot | ) |

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.

Since glibmm 2.22:

Parameters:

  cancellable | A Cancellable, or `0`.
  callback | A AsyncReadyCallback.
  user_data | User data for the callback.

Glib::RefPtr<Socket> Gio::SocketListener::accept_socket_finish | ( | const Glib::RefPtr< AsyncResult >& | result | ) |

Finishes an async accept operation.

See g_socket_listener_accept_socket_async()

Since glibmm 2.22:

Parameters:

  result | A AsyncResult.
  source_object | Optional Object identifying this source.

Returns:
  A Socket on success, `0` on error.

Glib::RefPtr<Socket> Gio::SocketListener::accept_socket_finish | ( | const Glib::RefPtr< AsyncResult >& | result,
|  | Glib::RefPtr< Glib::Object >& | source_object
| ) |  |

Finishes an async accept operation.

See g_socket_listener_accept_socket_async()

Since glibmm 2.22:

Parameters:

  result | A AsyncResult.
  source_object | Optional Object identifying this source.

Returns:
  A Socket on success, `0` on error.

bool Gio::SocketListener::add_address | ( | const Glib::RefPtr< SocketAddress >& | address,
|  | SocketType | type,
|  | SocketProtocol | protocol,
|  | Glib::RefPtr< SocketAddress >& | effective_address
| ) |  |

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 determinstic behaviour, 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-`0` then it will be set to the address that the binding actually occured 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.

Since glibmm 2.22:

Parameters:

  address | A SocketAddress.
  type | A SocketType.
  protocol | A SocketProtocol.
  source_object | Optional Object identifying this source.
  effective_address | Location to store the address that was bound to, or `0`.

Returns:
  `true` on success, `false` on error.

bool Gio::SocketListener::add_address | ( | const Glib::RefPtr< SocketAddress >& | address,
|  | SocketType | type,
|  | SocketProtocol | protocol,
|  | const Glib::RefPtr< Glib::Object >& | source_object,
|  | Glib::RefPtr< SocketAddress >& | effective_address
| ) |  |

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 determinstic behaviour, 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-`0` then it will be set to the address that the binding actually occured 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.

Since glibmm 2.22:

Parameters:

  address | A SocketAddress.
  type | A SocketType.
  protocol | A SocketProtocol.
  source_object | Optional Object identifying this source.
  effective_address | Location to store the address that was bound to, or `0`.

Returns:
  `true` on success, `false` on error.

guint16 Gio::SocketListener::add_any_inet_port | ( |  | ) |

Listens for TCP connections on any available port number for both IPv6 and IPv4 (if each are 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.

Since glibmm 2.24:

Parameters:

  source_object | Optional Object identifying this source.

Returns:
  The port number, or 0 in case of failure.

guint16 Gio::SocketListener::add_any_inet_port | ( | const Glib::RefPtr< Glib::Object >& | source_object | ) |

Listens for TCP connections on any available port number for both IPv6 and IPv4 (if each are 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.

Since glibmm 2.24:

Parameters:

  source_object | Optional Object identifying this source.

Returns:
  The port number, or 0 in case of failure.

bool Gio::SocketListener::add_inet_port | ( | guint16 | port,
|  | const Glib::RefPtr< Glib::Object >& | source_object
| ) |  |

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.

Since glibmm 2.22:

Parameters:

  port | An IP port number (non-zero).
  source_object | Optional Object identifying this source.

Returns:
  `true` on success, `false` on error.

bool Gio::SocketListener::add_inet_port | ( | guint16 | port | ) |

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.

Since glibmm 2.22:

Parameters:

  port | An IP port number (non-zero).
  source_object | Optional Object identifying this source.

Returns:
  `true` on success, `false` on error.

bool Gio::SocketListener::add_socket | ( | const Glib::RefPtr< Socket >& | socket,
|  | const Glib::RefPtr< Glib::Object >& | source_object
| ) |  |

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.

Since glibmm 2.22:

Parameters:

  socket | A listening Socket.
  source_object | Optional Object identifying this source.

Returns:
  `true` on success, `false` on error.

bool Gio::SocketListener::add_socket | ( | const Glib::RefPtr< Socket >& | socket | ) |

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.

Since glibmm 2.22:

Parameters:

  socket | A listening Socket.
  source_object | Optional Object identifying this source.

Returns:
  `true` on success, `false` on error.

void Gio::SocketListener::close | ( |  | ) |

Closes all the sockets in the listener.

Since glibmm 2.22:

static Glib::RefPtr<SocketListener> Gio::SocketListener::create | ( |  | ) | `[static]`

Reimplemented in Gio::SocketService.

GSocketListener* Gio::SocketListener::gobj | ( |  | ) | `[inline]`

Provides access to the underlying C GObject.

Reimplemented from Glib::ObjectBase.

Reimplemented in Gio::SocketService, and Gio::ThreadedSocketService.

const GSocketListener* Gio::SocketListener::gobj | ( |  | ) | const`[inline]`

Provides access to the underlying C GObject.

Reimplemented from Glib::ObjectBase.

Reimplemented in Gio::SocketService, and Gio::ThreadedSocketService.

GSocketListener* Gio::SocketListener::gobj_copy | ( |  | ) |

Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.

Reimplemented in Gio::SocketService, and Gio::ThreadedSocketService.

Glib::PropertyProxy<int> Gio::SocketListener::property_listen_backlog | ( |  | ) |

outstanding connections in the listen queue.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

Returns:
  A PropertyProxy that allows you to get or set the property of the value, or receive notification when the value of the property changes.

Glib::PropertyProxy_ReadOnly<int> Gio::SocketListener::property_listen_backlog | ( |  | ) | const

outstanding connections in the listen queue.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

Returns:
  A PropertyProxy that allows you to get or set the property of the value, or receive notification when the value of the property changes.

void Gio::SocketListener::set_backlog | ( | int | listen_backlog | ) |

Sets the listen backlog on the sockets in the listener.

See g_socket_set_listen_backlog() for details

Since glibmm 2.22:

Parameters:

  listen_backlog | An integer.

## Friends And Related Function Documentation

Glib::RefPtr< Gio::SocketListener > wrap | ( | GSocketListener * | object,
|  | bool | take_copy = `false`
| ) |  | `[related]`

A Glib::wrap() method for this object.

Parameters:

  object | The C instance.
  take_copy | False if the result should take ownership of the C instance. True if it should take a new copy or ref.

Returns:
  A C++ instance that wraps this C instance.