1.83 <GSocketListener>

Class: <GSocketListener>

It derives from <GObject>.

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 socket-listener:add-address and socket-listener:add-inet-port?. These will be listened on until socket-listener:close is called. Dropping your final reference to the <GSocketListener> will not cause 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.

Fields

parent-instance
priv

Properties

Property: socket-listener:listen-backlog self

The number of outstanding connections in the listen queue.

Its value is a 32-bit integer.

Set it with (set! (socket-listener:listen-backlog self) value).

Procedures

Method: socket-listener:accept self [cancellable] ⇒ %return source-object
Method: accept self [cancellable] ⇒ %return source-object

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 #f 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 #f, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error cancelled will be returned.

self

Passed: a <GSocketListener>.

a <GSocketListener>

cancellable

Passed, or left out: a <GCancellable>, or #f.

optional <GCancellable> object, #f to ignore.

%return

Returned: a <GSocketConnection>.

a <GSocketConnection> on success, #f on error.

source-object

Returned: a <GObject>, or #f.

location where <GObject> pointer will be stored, or #f

Method: socket-listener:accept-async self [cancellable] [callback]
Method: accept-async self [cancellable] [callback]

This is the asynchronous version of socket-listener:accept.

When the operation is finished callback will be called. You can then call socket-listener:accept-finish to get the result of the operation.

self

Passed: a <GSocketListener>.

a <GSocketListener>

cancellable

Passed, or left out: a <GCancellable>, or #f.

a <GCancellable>, or #f

callback

Passed, or left out: a procedure, or #f.

a GAsyncReadyCallback

See GAsyncReadyCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

user data for the callback

Method: socket-listener:accept-finish self result ⇒ %return source-object
Method: accept-finish self result ⇒ %return source-object

Finishes an async accept operation. See socket-listener:accept-async

self

Passed: a <GSocketListener>.

a <GSocketListener>

result

Passed: a <GAsyncResult>.

a <GAsyncResult>.

%return

Returned: a <GSocketConnection>.

a <GSocketConnection> on success, #f on error.

source-object

Returned: a <GObject>, or #f.

Optional <GObject> identifying this source

Method: socket-listener:accept-socket self [cancellable] ⇒ %return source-object
Method: accept-socket self [cancellable] ⇒ %return source-object

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 socket-listener:accept instead.

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

self

Passed: a <GSocketListener>.

a <GSocketListener>

cancellable

Passed, or left out: a <GCancellable>, or #f.

optional <GCancellable> object, #f to ignore.

%return

Returned: a <GSocket>.

a <GSocket> on success, #f on error.

source-object

Returned: a <GObject>, or #f.

location where <GObject> pointer will be stored, or #f.

Method: socket-listener:accept-socket-async self [cancellable] [callback]
Method: accept-socket-async self [cancellable] [callback]

This is the asynchronous version of socket-listener:accept-socket.

When the operation is finished callback will be called. You can then call socket-listener:accept-socket-finish to get the result of the operation.

self

Passed: a <GSocketListener>.

a <GSocketListener>

cancellable

Passed, or left out: a <GCancellable>, or #f.

a <GCancellable>, or #f

callback

Passed, or left out: a procedure, or #f.

a GAsyncReadyCallback

See GAsyncReadyCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

user data for the callback

Method: socket-listener:accept-socket-finish self result ⇒ %return source-object
Method: accept-socket-finish self result ⇒ %return source-object

Finishes an async accept operation. See socket-listener:accept-socket-async

self

Passed: a <GSocketListener>.

a <GSocketListener>

result

Passed: a <GAsyncResult>.

a <GAsyncResult>.

%return

Returned: a <GSocket>.

a <GSocket> on success, #f on error.

source-object

Returned: a <GObject>, or #f.

Optional <GObject> identifying this source

Method: socket-listener:add-address self address type protocol [source-object] ⇒ %return effective-address
Method: add-address self address type protocol [source-object] ⇒ %return 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 deterministic behavior, see 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-#f 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 socket-listener:close to stop listening on address; this will not be done automatically when you drop your final reference to self, as references may be held internally.

self

Passed: a <GSocketListener>.

a <GSocketListener>

address

Passed: a <GSocketAddress>.

a <GSocketAddress>

type

Passed: a <GSocketType>, or one of its symbols.

a <GSocketType>

protocol

Passed: a <GSocketProtocol>, or one of its symbols.

a <GSocketProtocol>

source-object

Passed, or left out: a <GObject>, or #f.

Optional <GObject> identifying this source

%return

Returned: a boolean.

#t on success, #f on error.

effective-address

Returned: a <GSocketAddress>.

location to store the address that was bound to, or #f.

Method: socket-listener:add-any-inet-port self [source-object] ⇒ %return
Method: add-any-inet-port self [source-object] ⇒ %return

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.

If possible, the <GSocketListener> will listen on both IPv4 and IPv6 (listening on the same port on both). If listening on one of the socket families fails, the <GSocketListener> will only listen on the other. If listening on both fails, an error will be returned.

If you need to distinguish whether listening on IPv4 or IPv6 or both was successful, connect to socket-listener:event.

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.

self

Passed: a <GSocketListener>.

a <GSocketListener>

source-object

Passed, or left out: a <GObject>, or #f.

Optional <GObject> identifying this source

%return

Returned: an unsigned 16-bit integer.

the port number, or 0 in case of failure.

Method: socket-listener:add-inet-port? self port [source-object] ⇒ %return
Method: add-inet-port? self port [source-object] ⇒ %return

Helper function for socket-listener:add-address that creates a TCP/IP socket listening on IPv4 and IPv6 (if supported) on the specified port on all interfaces.

If possible, the <GSocketListener> will listen on both IPv4 and IPv6 (listening on the same port on both). If listening on one of the socket families fails, the <GSocketListener> will only listen on the other. If listening on both fails, an error will be returned.

If you need to distinguish whether listening on IPv4 or IPv6 or both was successful, connect to socket-listener:event.

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 socket-listener:close to stop listening on port; this will not be done automatically when you drop your final reference to self, as references may be held internally.

self

Passed: a <GSocketListener>.

a <GSocketListener>

port

Passed: an unsigned 16-bit integer.

an IP port number (non-zero)

source-object

Passed, or left out: a <GObject>, or #f.

Optional <GObject> identifying this source

%return

Returned: a boolean.

#t on success, #f on error.

Method: socket-listener:add-socket? self socket [source-object] ⇒ %return
Method: add-socket? self socket [source-object] ⇒ %return

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.

For parallel calls to <GSocketListener> methods to work, the socket must be in non-blocking mode. (See socket:blocking.)

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 self 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 self, even if references to it were held elsewhere.

self

Passed: a <GSocketListener>.

a <GSocketListener>

socket

Passed: a <GSocket>.

a listening <GSocket>

source-object

Passed, or left out: a <GObject>, or #f.

Optional <GObject> identifying this source

%return

Returned: a boolean.

#t on success, #f on error.

Method: socket-listener:close self
Method: close self

Closes all the sockets in the listener.

self

Passed: a <GSocketListener>.

a <GSocketListener>

Procedure: socket-listener:new ⇒ %return

Creates a new <GSocketListener> with no sockets to listen for. New listeners can be added with e.g. socket-listener:add-address or socket-listener:add-inet-port?.

%return

Returned: a <GSocketListener>.

a new <GSocketListener>.

Method: socket-listener:set-backlog self listen-backlog
Method: set-backlog self listen-backlog

Sets the listen backlog on the sockets in the listener. This must be called before adding any sockets, addresses or ports to the <GSocketListener> (for example, by calling socket-listener:add-inet-port?) to be effective.

See socket:set-listen-backlog for details

self

Passed: a <GSocketListener>.

a <GSocketListener>

listen-backlog

Passed: a 32-bit integer.

an integer

Signals

Signal: socket-listener:event

Connect a handler with (connect object socket-listener:event handler). It is called as (handler self event socket).

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.

self

Passed: a <GSocketListener>.

event

Passed: a <GSocketListenerEvent>.

the event that is occurring

socket

Passed: a <GSocket>.

the <GSocket> the event is occurring on

Virtual methods

Virtual method: socket-listener:do-changed implementor self

virtual method called when the set of socket listened to changes

To override it in a new class, give register-type #:vfuncs `((changed . ,procedure)). The procedure is called as (procedure self).

implementor

Passed: the class whose implementation to call: <GSocketListener>, or a class that derives from it, or its GType number.

self

Passed: a <GSocketListener>.

Virtual method: socket-listener:do-event implementor self event socket

To override it in a new class, give register-type #:vfuncs `((event . ,procedure)). The procedure is called as (procedure self event socket).

implementor

Passed: the class whose implementation to call: <GSocketListener>, or a class that derives from it, or its GType number.

self

Passed: a <GSocketListener>.

event

Passed: a <GSocketListenerEvent>, or one of its symbols.

socket

Passed: a <GSocket>.