1.80 <GSocketClient>

Class: <GSocketClient>

It derives from <GObject>.

<GSocketClient> is a lightweight high-level utility class for connecting to a network host using a connection oriented socket type.

You create a <GSocketClient> object, set any options you want, and then call a sync or async connect operation, which returns a <GSocketConnection> subclass on success.

The type of the <GSocketConnection> object returned depends on the type of the underlying socket that is in use. For instance, for a TCP/IP connection it will be a <GTcpConnection>.

As <GSocketClient> is a lightweight object, you don’t need to cache it. You can just create a new one any time you need one.

Fields

parent-instance
priv

Properties

Property: socket-client:enable-proxy self

Enable proxy support.

Its value is a boolean.

Set it with (set! (socket-client:enable-proxy self) value).

Property: socket-client:family self

The address family to use for socket construction.

Its value is a <GSocketFamily> (or, to set it, one of its symbols).

Set it with (set! (socket-client:family self) value).

Property: socket-client:local-address self

The local address constructed sockets will be bound to.

Its value is a <GSocketAddress>, or #f.

Set it with (set! (socket-client:local-address self) value).

Property: socket-client:protocol self

The protocol to use for socket construction, or 0 for default.

Its value is a <GSocketProtocol> (or, to set it, one of its symbols).

Set it with (set! (socket-client:protocol self) value).

Property: socket-client:proxy-resolver self

The proxy resolver to use

Its value is a <GProxyResolver>, or #f.

Set it with (set! (socket-client:proxy-resolver self) value).

Property: socket-client:timeout self

The I/O timeout for sockets, in seconds, or 0 for none.

Its value is an unsigned 32-bit integer.

Set it with (set! (socket-client:timeout self) value).

Property: socket-client:tls self

Whether to create TLS connections.

Its value is a boolean.

Set it with (set! (socket-client:tls self) value).

Property: socket-client:tls-validation-flags self

The TLS validation flags used when creating TLS connections. The default value is validate-all.

GLib guarantees that if certificate verification fails, at least one flag will be set, but it does not guarantee that all possible flags will be set. Accordingly, you may not safely decide to ignore any particular type of error. For example, it would be incorrect to mask expired if you want to allow expired certificates, because this could potentially be the only error flag set even if other problems exist with the certificate. Therefore, there is no safe way to use this property. This is not a horrible problem, though, because you should not be attempting to ignore validation errors anyway. If you really must ignore TLS certificate errors, connect to the socket-client:event signal, wait for it to be emitted with tls-handshaking, and use that to connect to tls-connection:accept-certificate.

Its value is a <GTlsCertificateFlags> (or, to set it, a list of its symbols, or one of them).

Set it with (set! (socket-client:tls-validation-flags self) value).

Property: socket-client:type self

The type to use for socket construction.

Its value is a <GSocketType> (or, to set it, one of its symbols).

Set it with (set! (socket-client:type self) value).

Procedures

Method: socket-client:add-application-proxy self protocol
Method: add-application-proxy self protocol

Enable proxy protocols to be handled by the application. When the indicated proxy protocol is returned by the <GProxyResolver>, <GSocketClient> will consider this protocol as supported but will not try to find a <GProxy> instance to handle handshaking. The application must check for this case by calling socket-connection:get-remote-address on the returned <GSocketConnection>, and seeing if it’s a <GProxyAddress> of the appropriate type, to determine whether or not it needs to handle the proxy handshaking itself.

This should be used for proxy protocols that are dialects of another protocol such as HTTP proxy. It also allows cohabitation of proxy protocols that are reused between protocols. A good example is HTTP. It can be used to proxy HTTP, FTP and Gopher and can also be use as generic socket proxy through the HTTP CONNECT method.

When the proxy is detected as being an application proxy, TLS handshake will be skipped. This is required to let the application do the proxy specific handshake.

self

Passed: a <GSocketClient>.

a <GSocketClient>

protocol

Passed: a string.

The proxy protocol

Method: socket-client:connect self connectable [cancellable] ⇒ %return
Method: connect self connectable [cancellable] ⇒ %return

Tries to resolve the connectable and make a network connection to it.

Upon a successful connection, a new <GSocketConnection> is constructed and returned. The caller owns this new object and must drop their reference to it when finished with it.

The type of the <GSocketConnection> object returned depends on the type of the underlying socket that is used. For instance, for a TCP/IP connection it will be a <GTcpConnection>.

The socket created will be the same family as the address that the connectable resolves to, unless family is set with socket-client:set-family or indirectly via socket-client:set-local-address. The socket type defaults to stream but can be set with socket-client:set-socket-type.

If a local address is specified with socket-client:set-local-address the socket will be bound to this address before connecting.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

connectable

Passed: a <GSocketConnectable>.

a <GSocketConnectable> specifying the remote address.

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.

Method: socket-client:connect-async self connectable [cancellable] [callback]
Method: connect-async self connectable [cancellable] [callback]

This is the asynchronous version of socket-client:connect.

You may wish to prefer the asynchronous version even in synchronous command line programs because, since 2.60, it implements RFC 8305 "Happy Eyeballs" recommendations to work around long connection timeouts in networks where IPv6 is broken by performing an IPv4 connection simultaneously without waiting for IPv6 to time out, which is not supported by the synchronous call. (This is not an API guarantee, and may change in the future.)

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

self

Passed: a <GSocketClient>.

a <GSocketClient>

connectable

Passed: a <GSocketConnectable>.

a <GSocketConnectable> specifying the remote address.

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-client:connect-finish self result ⇒ %return
Method: connect-finish self result ⇒ %return

Finishes an async connect operation. See socket-client:connect-async

self

Passed: a <GSocketClient>.

a <GSocketClient>.

result

Passed: a <GAsyncResult>.

a <GAsyncResult>.

%return

Returned: a <GSocketConnection>.

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

Method: socket-client:connect-to-host self host-and-port default-port [cancellable] ⇒ %return
Method: connect-to-host self host-and-port default-port [cancellable] ⇒ %return

This is a helper function for socket-client:connect.

Attempts to create a TCP connection to the named host.

host-and-port may be in any of a number of recognized formats; an IPv6 address, an IPv4 address, or a domain name (in which case a DNS lookup is performed). Quoting with [] is supported for all address types. A port override may be specified in the usual way with a colon. Ports may be given as decimal numbers or symbolic names (in which case an /etc/services lookup is performed).

If no port override is given in host-and-port then default-port will be used as the port number to connect to.

In general, host-and-port is expected to be provided by the user (allowing them to give the hostname, and a port override if necessary) and default-port is expected to be provided by the application.

In the case that an IP address is given, a single connection attempt is made. In the case that a name is given, multiple connection attempts may be made, in turn and according to the number of address records in DNS, until a connection succeeds.

Upon a successful connection, a new <GSocketConnection> is constructed and returned. The caller owns this new object and must drop their reference to it when finished with it.

In the event of any failure (DNS error, service not found, no hosts connectable) #f is returned and error (if non-#f) is set accordingly.

self

Passed: a <GSocketClient>.

a <GSocketClient>

host-and-port

Passed: a string.

the name and optionally port of the host to connect to

default-port

Passed: an unsigned 16-bit integer.

the default port to connect to

cancellable

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

a <GCancellable>, or #f

%return

Returned: a <GSocketConnection>.

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

Method: socket-client:connect-to-host-async self host-and-port default-port [cancellable] [callback]
Method: connect-to-host-async self host-and-port default-port [cancellable] [callback]

This is the asynchronous version of socket-client:connect-to-host.

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

self

Passed: a <GSocketClient>.

a <GSocketClient>

host-and-port

Passed: a string.

the name and optionally the port of the host to connect to

default-port

Passed: an unsigned 16-bit integer.

the default port to connect to

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-client:connect-to-host-finish self result ⇒ %return
Method: connect-to-host-finish self result ⇒ %return

Finishes an async connect operation. See socket-client:connect-to-host-async

self

Passed: a <GSocketClient>.

a <GSocketClient>.

result

Passed: a <GAsyncResult>.

a <GAsyncResult>.

%return

Returned: a <GSocketConnection>.

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

Method: socket-client:connect-to-service self domain service [cancellable] ⇒ %return
Method: connect-to-service self domain service [cancellable] ⇒ %return

Attempts to create a TCP connection to a service.

This call looks up the SRV record for service at domain for the "tcp" protocol. It then attempts to connect, in turn, to each of the hosts providing the service until either a connection succeeds or there are no hosts remaining.

Upon a successful connection, a new <GSocketConnection> is constructed and returned. The caller owns this new object and must drop their reference to it when finished with it.

In the event of any failure (DNS error, service not found, no hosts connectable) #f is returned and error (if non-#f) is set accordingly.

self

Passed: a <GSocketClient>.

a <GSocketConnection>

domain

Passed: a string.

a domain name

service

Passed: a string.

the name of the service to connect to

cancellable

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

a <GCancellable>, or #f

%return

Returned: a <GSocketConnection>.

a <GSocketConnection> if successful, or #f on error

Method: socket-client:connect-to-service-async self domain service [cancellable] [callback]
Method: connect-to-service-async self domain service [cancellable] [callback]

This is the asynchronous version of socket-client:connect-to-service.

self

Passed: a <GSocketClient>.

a <GSocketClient>

domain

Passed: a string.

a domain name

service

Passed: a string.

the name of the service to connect to

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-client:connect-to-service-finish self result ⇒ %return
Method: connect-to-service-finish self result ⇒ %return

Finishes an async connect operation. See socket-client:connect-to-service-async

self

Passed: a <GSocketClient>.

a <GSocketClient>.

result

Passed: a <GAsyncResult>.

a <GAsyncResult>.

%return

Returned: a <GSocketConnection>.

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

Method: socket-client:connect-to-uri self uri default-port [cancellable] ⇒ %return
Method: connect-to-uri self uri default-port [cancellable] ⇒ %return

This is a helper function for socket-client:connect.

Attempts to create a TCP connection with a network URI.

uri may be any valid URI containing an "authority" (hostname/port) component. If a port is not specified in the URI, default-port will be used. TLS will be negotiated if socket-client:tls is #t. (<GSocketClient> does not know to automatically assume TLS for certain URI schemes.)

Using this rather than socket-client:connect or socket-client:connect-to-host allows <GSocketClient> to determine when to use application-specific proxy protocols.

Upon a successful connection, a new <GSocketConnection> is constructed and returned. The caller owns this new object and must drop their reference to it when finished with it.

In the event of any failure (DNS error, service not found, no hosts connectable) #f is returned and error (if non-#f) is set accordingly.

self

Passed: a <GSocketClient>.

a <GSocketClient>

uri

Passed: a string.

A network URI

default-port

Passed: an unsigned 16-bit integer.

the default port to connect to

cancellable

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

a <GCancellable>, or #f

%return

Returned: a <GSocketConnection>.

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

Method: socket-client:connect-to-uri-async self uri default-port [cancellable] [callback]
Method: connect-to-uri-async self uri default-port [cancellable] [callback]

This is the asynchronous version of socket-client:connect-to-uri.

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

self

Passed: a <GSocketClient>.

a <GSocketClient>

uri

Passed: a string.

a network uri

default-port

Passed: an unsigned 16-bit integer.

the default port to connect to

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-client:connect-to-uri-finish self result ⇒ %return
Method: connect-to-uri-finish self result ⇒ %return

Finishes an async connect operation. See socket-client:connect-to-uri-async

self

Passed: a <GSocketClient>.

a <GSocketClient>.

result

Passed: a <GAsyncResult>.

a <GAsyncResult>.

%return

Returned: a <GSocketConnection>.

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

Method: socket-client:get-enable-proxy? self ⇒ %return
Method: get-enable-proxy? self ⇒ %return

Gets the proxy enable state; see socket-client:set-enable-proxy

self

Passed: a <GSocketClient>.

a <GSocketClient>.

%return

Returned: a boolean.

whether proxying is enabled

Method: socket-client:get-family self ⇒ %return
Method: get-family self ⇒ %return

Gets the socket family of the socket client.

See socket-client:set-family for details.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

%return

Returned: a <GSocketFamily>.

a <GSocketFamily>

Method: socket-client:get-local-address self ⇒ %return
Method: get-local-address self ⇒ %return

Gets the local address of the socket client.

See socket-client:set-local-address for details.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

%return

Returned: a <GSocketAddress>, or #f.

a <GSocketAddress> or #f. Do not free.

Method: socket-client:get-protocol self ⇒ %return
Method: get-protocol self ⇒ %return

Gets the protocol name type of the socket client.

See socket-client:set-protocol for details.

self

Passed: a <GSocketClient>.

a <GSocketClient>

%return

Returned: a <GSocketProtocol>.

a <GSocketProtocol>

Method: socket-client:get-proxy-resolver self ⇒ %return
Method: get-proxy-resolver self ⇒ %return

Gets the <GProxyResolver> being used by self. Normally, this will be the resolver returned by proxy-resolver-get-default, but you can override it with socket-client:set-proxy-resolver.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

%return

Returned: a <GProxyResolver>.

The <GProxyResolver> being used by self.

Method: socket-client:get-socket-type self ⇒ %return
Method: get-socket-type self ⇒ %return

Gets the socket type of the socket client.

See socket-client:set-socket-type for details.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

%return

Returned: a <GSocketType>.

a <GSocketFamily>

Method: socket-client:get-timeout self ⇒ %return
Method: get-timeout self ⇒ %return

Gets the I/O timeout time for sockets created by self.

See socket-client:set-timeout for details.

self

Passed: a <GSocketClient>.

a <GSocketClient>

%return

Returned: an unsigned 32-bit integer.

the timeout in seconds

Method: socket-client:get-tls-validation-flags self ⇒ %return
Method: get-tls-validation-flags self ⇒ %return

Gets the TLS validation flags used creating TLS connections via self.

This function does not work as originally designed and is impossible to use correctly. See socket-client:tls-validation-flags for more information.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

%return

Returned: a <GTlsCertificateFlags>.

the TLS validation flags

Method: socket-client:get-tls? self ⇒ %return
Method: get-tls? self ⇒ %return

Gets whether self creates TLS connections. See socket-client:set-tls for details.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

%return

Returned: a boolean.

whether self uses TLS

Procedure: socket-client:new ⇒ %return

Creates a new <GSocketClient> with the default options.

%return

Returned: a <GSocketClient>.

a <GSocketClient>. Free the returned object with object:unref.

Method: socket-client:set-enable-proxy self enable
Method: set-enable-proxy self enable

Sets whether or not self attempts to make connections via a proxy server. When enabled (the default), <GSocketClient> will use a <GProxyResolver> to determine if a proxy protocol such as SOCKS is needed, and automatically do the necessary proxy negotiation.

See also socket-client:set-proxy-resolver.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

enable

Passed: a boolean.

whether to enable proxies

Method: socket-client:set-family self family
Method: set-family self family

Sets the socket family of the socket client. If this is set to something other than invalid then the sockets created by this object will be of the specified family.

This might be useful for instance if you want to force the local connection to be an ipv4 socket, even though the address might be an ipv6 mapped to ipv4 address.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

family

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

a <GSocketFamily>

Method: socket-client:set-local-address self [address]
Method: set-local-address self [address]

Sets the local address of the socket client. The sockets created by this object will bound to the specified address (if not #f) before connecting.

This is useful if you want to ensure that the local side of the connection is on a specific port, or on a specific interface.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

address

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

a <GSocketAddress>, or #f

Method: socket-client:set-protocol self protocol
Method: set-protocol self protocol

Sets the protocol of the socket client. The sockets created by this object will use of the specified protocol.

If protocol is default that means to use the default protocol for the socket family and type.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

protocol

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

a <GSocketProtocol>

Method: socket-client:set-proxy-resolver self [proxy-resolver]
Method: set-proxy-resolver self [proxy-resolver]

Overrides the <GProxyResolver> used by self. You can call this if you want to use specific proxies, rather than using the system default proxy settings.

Note that whether or not the proxy resolver is actually used depends on the setting of socket-client:enable-proxy, which is not changed by this function (but which is #t by default)

self

Passed: a <GSocketClient>.

a <GSocketClient>.

proxy-resolver

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

a <GProxyResolver>, or #f for the default.

Method: socket-client:set-socket-type self type
Method: set-socket-type self type

Sets the socket type of the socket client. The sockets created by this object will be of the specified type.

It doesn’t make sense to specify a type of datagram, as GSocketClient is used for connection oriented services.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

type

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

a <GSocketType>

Method: socket-client:set-timeout self timeout
Method: set-timeout self timeout

Sets the I/O timeout for sockets created by self. timeout is a time in seconds, or 0 for no timeout (the default).

The timeout value affects the initial connection attempt as well, so setting this may cause calls to socket-client:connect, etc, to fail with timed-out.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

timeout

Passed: an unsigned 32-bit integer.

the timeout

Method: socket-client:set-tls self tls
Method: set-tls self tls

Sets whether self creates TLS (aka SSL) connections. If tls is #t, self will wrap its connections in a <GTlsClientConnection> and perform a TLS handshake when connecting.

Note that since <GSocketClient> must return a <GSocketConnection>, but <GTlsClientConnection> is not a <GSocketConnection>, this actually wraps the resulting <GTlsClientConnection> in a <GTcpWrapperConnection> when returning it. You can use tcp-wrapper-connection:get-base-io-stream on the return value to extract the <GTlsClientConnection>.

If you need to modify the behavior of the TLS handshake (eg, by setting a client-side certificate to use, or connecting to the tls-connection:accept-certificate signal), you can connect to self’s socket-client:event signal and wait for it to be emitted with tls-handshaking, which will give you a chance to see the <GTlsClientConnection> before the handshake starts.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

tls

Passed: a boolean.

whether to use TLS

Method: socket-client:set-tls-validation-flags self flags
Method: set-tls-validation-flags self flags

Sets the TLS validation flags used when creating TLS connections via self. The default value is validate-all.

This function does not work as originally designed and is impossible to use correctly. See socket-client:tls-validation-flags for more information.

self

Passed: a <GSocketClient>.

a <GSocketClient>.

flags

Passed: a <GTlsCertificateFlags>, or a list of its symbols, or one of them.

the validation flags

Signals

Signal: socket-client:event

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

Emitted when client’s activity on connectable changes state. Among other things, this can be used to provide progress information about a network connection in the UI. The meanings of the different event values are as follows:

  • resolving: client is about to look up connectable in DNS. connection will be #f.
  • resolved: client has successfully resolved connectable in DNS. connection will be #f.
  • complete: client has either successfully connected to connectable (in which case connection is the <GSocketConnection> that it will be returning to the caller) or has failed (in which case connection is #f and the client is about to return an error).

Each event except complete may be emitted multiple times (or not at all) for a given connectable (in particular, if client ends up attempting to connect to more than one address). However, if client emits the socket-client:event signal at all for a given connectable, then it will always emit it with complete when it is done.

Note that there may be additional <GSocketClientEvent> values in the future; unrecognized event values should be ignored.

self

Passed: a <GSocketClient>.

event

Passed: a <GSocketClientEvent>.

the event that is occurring

connectable

Passed: a <GSocketConnectable>.

the <GSocketConnectable> that event is occurring on

connection

Passed: a <GIOStream>, or #f.

the current representation of the connection

Virtual methods

Virtual method: socket-client:do-event implementor self event connectable connection

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

implementor

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

self

Passed: a <GSocketClient>.

event

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

connectable

Passed: a <GSocketConnectable>.

connection

Passed: a <GIOStream>.