1.77 <GSocket>

Class: <GSocket>

It derives from <GObject>.

A <GSocket> is a low-level networking primitive. It is a more or less direct mapping of the BSD socket API in a portable GObject based API. It supports both the UNIX socket implementations and winsock2 on Windows.

<GSocket> is the platform independent base upon which the higher level network primitives are based. Applications are not typically meant to use it directly, but rather through classes like <GSocketClient>, <GSocketService> and <GSocketConnection>. However there may be cases where direct use of <GSocket> is useful.

<GSocket> implements the <GInitable> interface, so if it is manually constructed by e.g. GObject.Object.new you must call initable:init? and check the results before using the object. This is done automatically in socket:new and socket:new-from-fd, so these functions can return NULL.

Sockets operate in two general modes, blocking or non-blocking. When in blocking mode all operations (which don’t take an explicit blocking parameter) block until the requested operation is finished or there is an error. In non-blocking mode all calls that would block return immediately with a would-block error. To know when a call would successfully run you can call socket:condition-check, or socket:condition-wait?. You can also use Gio.Socket.create_source and attach it to a <GMainContext> to get callbacks when I/O is possible. Note that all sockets are always set to non blocking mode in the system, and blocking mode is emulated in <GSocket>.

When working in non-blocking mode applications should always be able to handle getting a would-block error even when some other function said that I/O was possible. This can easily happen in case of a race condition in the application, but it can also happen for other reasons. For instance, on Windows a socket is always seen as writable until a write returns would-block.

<GSocket>s can be either connection oriented or datagram based. For connection oriented types you must first establish a connection by either connecting to an address or accepting a connection from another address. For connectionless socket types the target/source address is specified or received in each I/O operation.

All socket file descriptors are set to be close-on-exec.

Note that creating a <GSocket> causes the signal SIGPIPE to be ignored for the remainder of the program. If you are writing a command-line utility that uses <GSocket>, you may need to take into account the fact that your program will not automatically be killed if it tries to write to stdout after it has been closed.

Like most other APIs in GLib, <GSocket> is not inherently thread safe. To use a <GSocket> concurrently from multiple threads, you must implement your own locking.

Nagle’s algorithm

Since GLib 2.80, <GSocket> will automatically set the TCP_NODELAY option on all stream sockets. This disables Nagle’s algorithm as it typically does more harm than good on modern networks.

If your application needs Nagle’s algorithm enabled, call socket:set-option? after constructing a <GSocket> to enable it:

socket = g_socket_new (…, G_SOCKET_TYPE_STREAM, …);
if (socket != NULL)
  {
    g_socket_set_option (socket, IPPROTO_TCP, TCP_NODELAY, FALSE, &local_error);
    // handle error if needed
  }

Fields

parent-instance
priv

Properties

Property: socket:blocking self

Whether I/O on this socket is blocking.

Its value is a boolean.

Set it with (set! (socket:blocking self) value).

Property: socket:broadcast self

Whether the socket should allow sending to broadcast addresses.

Its value is a boolean.

Set it with (set! (socket:broadcast self) value).

Property: socket:family self

The socket’s address family.

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

It can only be set when the object is made, with #:family.

Property: socket:fd self

The socket’s file descriptor.

Its value is a 32-bit integer.

It can only be set when the object is made, with #:fd.

Property: socket:keepalive self

Whether to keep the connection alive by sending periodic pings.

Its value is a boolean.

Set it with (set! (socket:keepalive self) value).

Property: socket:listen-backlog self

The number of outstanding connections in the listen queue.

Its value is a 32-bit integer.

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

Property: socket:local-address self

The local address the socket is bound to.

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

It is read only.

Property: socket:multicast-loopback self

Whether outgoing multicast packets loop back to the local host.

Its value is a boolean.

Set it with (set! (socket:multicast-loopback self) value).

Property: socket:multicast-ttl self

Time-to-live out outgoing multicast packets

Its value is an unsigned 32-bit integer.

Set it with (set! (socket:multicast-ttl self) value).

Property: socket:protocol self

The ID of the protocol to use, or -1 for unknown.

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

It can only be set when the object is made, with #:protocol.

Property: socket:remote-address self

The remote address the socket is connected to.

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

It is read only.

Property: socket:timeout self

The timeout in seconds on socket I/O

Its value is an unsigned 32-bit integer.

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

Property: socket:ttl self

Time-to-live for outgoing unicast packets

Its value is an unsigned 32-bit integer.

Set it with (set! (socket:ttl self) value).

Property: socket:type self

The socket’s type.

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

It can only be set when the object is made, with #:type.

Procedures

Method: socket:accept self [cancellable] ⇒ %return
Method: accept self [cancellable] ⇒ %return

Accept incoming connections on a connection-based socket. This removes the first outstanding connection request from the listening socket and creates a <GSocket> object for it.

The self must be bound to a local address with socket:bind? and must be listening for incoming connections (socket:listen?).

If there are no outstanding connections then the operation will block or return would-block if non-blocking I/O is enabled. To be notified of an incoming connection, wait for the in condition.

self

Passed: a <GSocket>.

a <GSocket>.

cancellable

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

a <GCancellable> or #f

%return

Returned: a <GSocket>.

a new <GSocket>, or #f on error. Free the returned object with object:unref.

Method: socket:bind? self address allow-reuse ⇒ %return
Method: bind? self address allow-reuse ⇒ %return

When a socket is created it is attached to an address family, but it doesn’t have an address in this family. socket:bind? assigns the address (sometimes called name) of the socket.

It is generally required to bind to a local address before you can receive connections. (See socket:listen? and socket:accept ). In certain situations, you may also want to bind a socket that will be used to initiate connections, though this is not normally required.

If self is a TCP socket, then allow-reuse controls the setting of the SO_REUSEADDR socket option; normally it should be #t for server sockets (sockets that you will eventually call socket:accept on), and #f for client sockets. (Failing to set this flag on a server socket may cause socket:bind? to return address-in-use if the server program is stopped and then immediately restarted.)

If self is a UDP socket, then allow-reuse determines whether or not other UDP sockets can be bound to the same address at the same time. In particular, you can have several UDP sockets bound to the same address, and they will all receive all of the multicast and broadcast packets sent to that address. (The behavior of unicast UDP packets to an address with multiple listeners is not defined.)

self

Passed: a <GSocket>.

a <GSocket>.

address

Passed: a <GSocketAddress>.

a <GSocketAddress> specifying the local address.

allow-reuse

Passed: a boolean.

whether to allow reusing this address

%return

Returned: a boolean.

#t on success, #f on error.

Method: socket:check-connect-result? self ⇒ %return
Method: check-connect-result? self ⇒ %return

Checks and resets the pending connect error for the socket. This is used to check for errors when socket:connect? is used in non-blocking mode.

self

Passed: a <GSocket>.

a <GSocket>

%return

Returned: a boolean.

#t if no error, #f otherwise, setting error to the error

Method: socket:close? self ⇒ %return
Method: close? self ⇒ %return

Closes the socket, shutting down any active connection.

Closing a socket does not wait for all outstanding I/O operations to finish, so the caller should not rely on them to be guaranteed to complete even if the close returns with no error.

Once the socket is closed, all other operations will return closed. Closing a socket multiple times will not return an error.

Sockets will be automatically closed when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible.

Beware that due to the way that TCP works, it is possible for recently-sent data to be lost if either you close a socket while the in condition is set, or else if the remote connection tries to send something to you after you close the socket but before it has finished reading all of the data you sent. There is no easy generic way to avoid this problem; the easiest fix is to design the network protocol such that the client will never send data "out of turn". Another solution is for the server to half-close the connection by calling socket:shutdown? with only the shutdown-write flag set, and then wait for the client to notice this and close its side of the connection, after which the server can safely call socket:close?. (This is what <GTcpConnection> does if you call tcp-connection:set-graceful-disconnect. But of course, this only works if the client will close its connection after the server does.)

self

Passed: a <GSocket>.

a <GSocket>

%return

Returned: a boolean.

#t on success, #f on error

Method: socket:condition-check self condition ⇒ %return
Method: condition-check self condition ⇒ %return

Checks on the readiness of self to perform operations. The operations specified in condition are checked for and masked against the currently-satisfied conditions on self. The result is returned.

Note that on Windows, it is possible for an operation to return would-block even immediately after socket:condition-check has claimed that the socket is ready for writing. Rather than calling socket:condition-check and then writing to the socket if it succeeds, it is generally better to simply try writing to the socket right away, and try again later if the initial attempt returns would-block.

It is meaningless to specify err or hup in condition; these conditions will always be set in the output if they are true.

This call never blocks.

self

Passed: a <GSocket>.

a <GSocket>

condition

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

a <GIOCondition> mask to check

%return

Returned: a <GIOCondition>.

the GIOCondition mask of the current state

Method: socket:condition-timed-wait? self condition timeout-us [cancellable] ⇒ %return
Method: condition-timed-wait? self condition timeout-us [cancellable] ⇒ %return

Waits for up to timeout-us microseconds for condition to become true on self. If the condition is met, #t is returned.

If cancellable is cancelled before the condition is met, or if timeout-us (or the socket’s socket:timeout) is reached before the condition is met, then #f is returned and error, if non-#f, is set to the appropriate value (cancelled or timed-out).

If you don’t want a timeout, use socket:condition-wait?. (Alternatively, you can pass -1 for timeout-us.)

Note that although timeout-us is in microseconds for consistency with other GLib APIs, this function actually only has millisecond resolution, and the behavior is undefined if timeout-us is not an exact number of milliseconds.

self

Passed: a <GSocket>.

a <GSocket>

condition

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

a <GIOCondition> mask to wait for

timeout-us

Passed: a 64-bit integer.

the maximum time (in microseconds) to wait, or -1

cancellable

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

a <GCancellable>, or #f

%return

Returned: a boolean.

#t if the condition was met, #f otherwise

Method: socket:condition-wait? self condition [cancellable] ⇒ %return
Method: condition-wait? self condition [cancellable] ⇒ %return

Waits for condition to become true on self. When the condition is met, #t is returned.

If cancellable is cancelled before the condition is met, or if the socket has a timeout set and it is reached before the condition is met, then #f is returned and error, if non-#f, is set to the appropriate value (cancelled or timed-out).

See also socket:condition-timed-wait?.

self

Passed: a <GSocket>.

a <GSocket>

condition

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

a <GIOCondition> mask to wait for

cancellable

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

a <GCancellable>, or #f

%return

Returned: a boolean.

#t if the condition was met, #f otherwise

Method: socket:connect? self address [cancellable] ⇒ %return
Method: connect? self address [cancellable] ⇒ %return

Connect the socket to the specified remote address.

For connection oriented socket this generally means we attempt to make a connection to the address. For a connection-less socket it sets the default address for socket:send and discards all incoming datagrams from other sources.

Generally connection oriented sockets can only connect once, but connection-less sockets can connect multiple times to change the default address.

If the connect call needs to do network I/O it will block, unless non-blocking I/O is enabled. Then pending is returned and the user can be notified of the connection finishing by waiting for the G_IO_OUT condition. The result of the connection must then be checked with socket:check-connect-result?.

self

Passed: a <GSocket>.

a <GSocket>.

address

Passed: a <GSocketAddress>.

a <GSocketAddress> specifying the remote address.

cancellable

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

a <GCancellable> or #f

%return

Returned: a boolean.

#t if connected, #f on error.

Method: socket:connection-factory-create-connection self ⇒ %return
Method: connection-factory-create-connection self ⇒ %return

Creates a <GSocketConnection> subclass of the right type for self.

self

Passed: a <GSocket>.

a <GSocket>

%return

Returned: a <GSocketConnection>.

a <GSocketConnection>

Method: socket:get-available-bytes self ⇒ %return
Method: get-available-bytes self ⇒ %return

Get the amount of data pending in the OS input buffer, without blocking.

If self is a UDP or SCTP socket, this will return the size of just the next packet, even if additional packets are buffered after that one.

Note that on Windows, this function is rather inefficient in the UDP case, and so if you know any plausible upper bound on the size of the incoming packet, it is better to just do a socket:receive! with a buffer of that size, rather than calling socket:get-available-bytes first and then doing a receive of exactly the right size.

self

Passed: a <GSocket>.

a <GSocket>

%return

Returned: a 64-bit integer.

the number of bytes that can be read from the socket without blocking or truncating, or -1 on error.

Method: socket:get-blocking? self ⇒ %return
Method: get-blocking? self ⇒ %return

Gets the blocking mode of the socket. For details on blocking I/O, see socket:set-blocking.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a boolean.

#t if blocking I/O is used, #f otherwise.

Method: socket:get-broadcast? self ⇒ %return
Method: get-broadcast? self ⇒ %return

Gets the broadcast setting on self; if #t, it is possible to send packets to broadcast addresses.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a boolean.

the broadcast setting on self

Method: socket:get-credentials self ⇒ %return
Method: get-credentials self ⇒ %return

Returns the credentials of the foreign process connected to this socket, if any (e.g. it is only supported for unix sockets).

If this operation isn’t supported on the OS, the method fails with the not-supported error. On Linux this is implemented by reading the SO_PEERCRED option on the underlying socket.

This method can be expected to be available on the following platforms:

  • Linux since GLib 2.26
  • OpenBSD since GLib 2.30
  • Solaris, Illumos and OpenSolaris since GLib 2.40
  • NetBSD since GLib 2.42
  • macOS, tvOS, iOS since GLib 2.66

Other ways to obtain credentials from a foreign peer includes the <GUnixCredentialsMessage> type and unix-connection:send-credentials? / unix-connection:receive-credentials functions.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a <GCredentials>.

#f if error is set, otherwise a <GCredentials> object that must be freed with object:unref.

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

Gets the socket family of the socket.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a <GSocketFamily>.

a <GSocketFamily>

Method: socket:get-fd self ⇒ %return
Method: get-fd self ⇒ %return

Returns the underlying OS socket object. On unix this is a socket file descriptor, and on Windows this is a Winsock2 SOCKET handle. This may be useful for doing platform specific or otherwise unusual operations on the socket.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a 32-bit integer.

the file descriptor of the socket.

Method: socket:get-keepalive? self ⇒ %return
Method: get-keepalive? self ⇒ %return

Gets the keepalive mode of the socket. For details on this, see socket:set-keepalive.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a boolean.

#t if keepalive is active, #f otherwise.

Method: socket:get-listen-backlog self ⇒ %return
Method: get-listen-backlog self ⇒ %return

Gets the listen backlog setting of the socket. For details on this, see socket:set-listen-backlog.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a 32-bit integer.

the maximum number of pending connections.

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

Try to get the local address of a bound socket. This is only useful if the socket has been bound to a local address, either explicitly or implicitly when connecting.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a <GSocketAddress>.

a <GSocketAddress> or #f on error. Free the returned object with object:unref.

Method: socket:get-multicast-loopback? self ⇒ %return
Method: get-multicast-loopback? self ⇒ %return

Gets the multicast loopback setting on self; if #t (the default), outgoing multicast packets will be looped back to multicast listeners on the same host.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a boolean.

the multicast loopback setting on self

Method: socket:get-multicast-ttl self ⇒ %return
Method: get-multicast-ttl self ⇒ %return

Gets the multicast time-to-live setting on self; see socket:set-multicast-ttl for more details.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: an unsigned 32-bit integer.

the multicast time-to-live setting on self

Method: socket:get-option self level optname ⇒ %return value
Method: get-option self level optname ⇒ %return value

Gets the value of an integer-valued option on self, as with getsockopt(). (If you need to fetch a non-integer-valued option, you will need to call getsockopt() directly.)

The ‘<gio/gnetworking.h>‘ header pulls in system headers that will define most of the standard/portable socket options. For unusual socket protocols or platform-dependent options, you may need to include additional headers.

Note that even for socket options that are a single byte in size, value is still a pointer to a gint variable, not a guchar; socket:get-option will handle the conversion internally.

self

Passed: a <GSocket>.

a <GSocket>

level

Passed: a 32-bit integer.

the "API level" of the option (eg, SOL_SOCKET)

optname

Passed: a 32-bit integer.

the "name" of the option (eg, SO_BROADCAST)

%return

Returned: a boolean.

success or failure. On failure, error will be set, and the system error value (errno or WSAGetLastError()) will still be set to the result of the getsockopt() call.

value

Returned: a 32-bit integer.

return location for the option value

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

Gets the socket protocol id the socket was created with. In case the protocol is unknown, -1 is returned.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a <GSocketProtocol>.

a protocol id, or -1 if unknown

Method: socket:get-remote-address self ⇒ %return
Method: get-remote-address self ⇒ %return

Try to get the remote address of a connected socket. This is only useful for connection oriented sockets that have been connected.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a <GSocketAddress>.

a <GSocketAddress> or #f on error. Free the returned object with object:unref.

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

Gets the socket type of the socket.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a <GSocketType>.

a <GSocketType>

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

Gets the timeout setting of the socket. For details on this, see socket:set-timeout.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: an unsigned 32-bit integer.

the timeout in seconds

Method: socket:get-ttl self ⇒ %return
Method: get-ttl self ⇒ %return

Gets the unicast time-to-live setting on self; see socket:set-ttl for more details.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: an unsigned 32-bit integer.

the time-to-live setting on self

Method: socket:is-closed? self ⇒ %return
Method: is-closed? self ⇒ %return

Checks whether a socket is closed.

self

Passed: a <GSocket>.

a <GSocket>

%return

Returned: a boolean.

#t if socket is closed, #f otherwise

Method: socket:is-connected? self ⇒ %return
Method: is-connected? self ⇒ %return

Check whether the socket is connected. This is only useful for connection-oriented sockets.

If using socket:shutdown?, this function will return #t until the socket has been shut down for reading and writing. If you do a non-blocking connect, this function will not return #t until after you call socket:check-connect-result?.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a boolean.

#t if socket is connected, #f otherwise.

Method: socket:join-multicast-group-ssm? self group [source-specific] [iface] ⇒ %return
Method: join-multicast-group-ssm? self group [source-specific] [iface] ⇒ %return

Registers self to receive multicast messages sent to group. self must be a datagram socket, and must have been bound to an appropriate interface and port with socket:bind?.

If iface is #f, the system will automatically pick an interface to bind to based on group.

If source-specific is not #f, use source-specific multicast as defined in RFC 4604. Note that on older platforms this may fail with a not-supported error.

Note that this function can be called multiple times for the same group with different source-specific in order to receive multicast packets from more than one source.

self

Passed: a <GSocket>.

a <GSocket>.

group

Passed: a <GInetAddress>.

a <GInetAddress> specifying the group address to join.

source-specific

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

a <GInetAddress> specifying the source-specific multicast address or #f to ignore.

iface

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

Name of the interface to use, or #f

%return

Returned: a boolean.

#t on success, #f on error.

Method: socket:join-multicast-group? self group source-specific [iface] ⇒ %return
Method: join-multicast-group? self group source-specific [iface] ⇒ %return

Registers self to receive multicast messages sent to group. self must be a datagram socket, and must have been bound to an appropriate interface and port with socket:bind?.

If iface is #f, the system will automatically pick an interface to bind to based on group.

If source-specific is #t, source-specific multicast as defined in RFC 4604 is used. Note that on older platforms this may fail with a not-supported error.

To bind to a given source-specific multicast address, use socket:join-multicast-group-ssm? instead.

self

Passed: a <GSocket>.

a <GSocket>.

group

Passed: a <GInetAddress>.

a <GInetAddress> specifying the group address to join.

source-specific

Passed: a boolean.

#t if source-specific multicast should be used

iface

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

Name of the interface to use, or #f

%return

Returned: a boolean.

#t on success, #f on error.

Method: socket:leave-multicast-group-ssm? self group [source-specific] [iface] ⇒ %return
Method: leave-multicast-group-ssm? self group [source-specific] [iface] ⇒ %return

Removes self from the multicast group defined by group, iface, and source-specific (which must all have the same values they had when you joined the group).

self remains bound to its address and port, and can still receive unicast messages after calling this.

self

Passed: a <GSocket>.

a <GSocket>.

group

Passed: a <GInetAddress>.

a <GInetAddress> specifying the group address to leave.

source-specific

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

a <GInetAddress> specifying the source-specific multicast address or #f to ignore.

iface

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

Name of the interface to use, or #f

%return

Returned: a boolean.

#t on success, #f on error.

Method: socket:leave-multicast-group? self group source-specific [iface] ⇒ %return
Method: leave-multicast-group? self group source-specific [iface] ⇒ %return

Removes self from the multicast group defined by group, iface, and source-specific (which must all have the same values they had when you joined the group).

self remains bound to its address and port, and can still receive unicast messages after calling this.

To unbind to a given source-specific multicast address, use socket:leave-multicast-group-ssm? instead.

self

Passed: a <GSocket>.

a <GSocket>.

group

Passed: a <GInetAddress>.

a <GInetAddress> specifying the group address to leave.

source-specific

Passed: a boolean.

#t if source-specific multicast was used

iface

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

Interface used

%return

Returned: a boolean.

#t on success, #f on error.

Method: socket:listen? self ⇒ %return
Method: listen? self ⇒ %return

Marks the socket as a server socket, i.e. a socket that is used to accept incoming requests using socket:accept.

Before calling this the socket must be bound to a local address using socket:bind?.

To set the maximum amount of outstanding clients, use socket:set-listen-backlog.

self

Passed: a <GSocket>.

a <GSocket>.

%return

Returned: a boolean.

#t on success, #f on error.

Procedure: socket:new family type protocol ⇒ %return

Creates a new <GSocket> with the defined family, type and protocol. If protocol is 0 (default) the default protocol type for the family and type is used.

The protocol is a family and type specific int that specifies what kind of protocol to use. <GSocketProtocol> lists several common ones. Many families only support one protocol, and use 0 for this, others support several and using 0 means to use the default protocol for the family and type.

The protocol id is passed directly to the operating system, so you can use protocols not listed in <GSocketProtocol> if you know the protocol number used for it.

family

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

the socket family to use, e.g. ipv4.

type

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

the socket type to use.

protocol

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

the id of the protocol to use, or 0 for default.

%return

Returned: a <GSocket>.

a <GSocket> or #f on error. Free the returned object with object:unref.

Procedure: socket:new-from-fd fd ⇒ %return

Creates a new <GSocket> from a native file descriptor or winsock SOCKET handle.

This reads all the settings from the file descriptor so that all properties should work. Note that the file descriptor will be set to non-blocking mode, independent on the blocking mode of the <GSocket>.

On success, the returned <GSocket> takes ownership of fd. On failure, the caller must close fd themselves.

Since GLib 2.46, it is no longer a fatal error to call this on a non-socket descriptor. Instead, a GError will be set with code failed

fd

Passed: a 32-bit integer.

a native socket file descriptor.

%return

Returned: a <GSocket>.

a <GSocket> or #f on error. Free the returned object with object:unref.

Method: socket:receive! self buffer [cancellable] ⇒ %return buffer
Method: receive! self buffer [cancellable] ⇒ %return buffer

Receive data (up to size bytes) from a socket. This is mainly used by connection-oriented sockets; it is identical to socket:receive-from! with address set to #f.

For datagram and seqpacket sockets, socket:receive! will always read either 0 or 1 complete messages from the socket. If the received message is too large to fit in buffer, then the data beyond size bytes will be discarded, without any explicit indication that this has occurred.

For stream sockets, socket:receive! can return any number of bytes, up to size. If more than size bytes have been received, the additional data will be returned in future calls to socket:receive!.

If the socket is in blocking mode the call will block until there is some data to receive, the connection is closed, or there is an error. If there is no data available and the socket is in non-blocking mode, a would-block error will be returned. To be notified when data is available, wait for the in condition.

On error -1 is returned and error is set accordingly.

self

Passed: a <GSocket>.

a <GSocket>

buffer

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer. It is filled in, and returned: a bytevector.

a buffer to read data into (which should be at least size bytes long).

cancellable

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

a <GCancellable> or #f

%return

Returned: a 64-bit integer.

Number of bytes read, or 0 if the connection was closed by the peer, or -1 on error

size

Not returned: it is the length of buffer.

the number of bytes you want to read from the socket

Method: socket:receive-bytes self size timeout-us [cancellable] ⇒ %return
Method: receive-bytes self size timeout-us [cancellable] ⇒ %return

Receives data (up to size bytes) from a socket.

This function is a variant of socket:receive! which returns a <GBytes> rather than a plain buffer.

Pass -1 to timeout-us to block indefinitely until data is received (or the connection is closed, or there is an error). Pass 0 to use the default timeout from socket:timeout, or pass a positive number to wait for that many microseconds for data before returning timed-out.

self

Passed: a <GSocket>.

a <GSocket>

size

Passed: an unsigned 64-bit integer.

the number of bytes you want to read from the socket

timeout-us

Passed: a 64-bit integer.

the timeout to wait for, in microseconds, or -1 to block indefinitely

cancellable

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

a <GCancellable>, or NULL

%return

Returned: a <GBytes>.

a bytes buffer containing the received bytes, or NULL on error

Method: socket:receive-bytes-from self size timeout-us [cancellable] ⇒ %return address
Method: receive-bytes-from self size timeout-us [cancellable] ⇒ %return address

Receive data (up to size bytes) from a socket.

This function is a variant of socket:receive-from! which returns a <GBytes> rather than a plain buffer.

If address is non-#f then address will be set equal to the source address of the received packet.

The address is owned by the caller.

Pass -1 to timeout-us to block indefinitely until data is received (or the connection is closed, or there is an error). Pass 0 to use the default timeout from socket:timeout, or pass a positive number to wait for that many microseconds for data before returning timed-out.

self

Passed: a <GSocket>.

a <GSocket>

size

Passed: an unsigned 64-bit integer.

the number of bytes you want to read from the socket

timeout-us

Passed: a 64-bit integer.

the timeout to wait for, in microseconds, or -1 to block indefinitely

cancellable

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

a <GCancellable>, or NULL

%return

Returned: a <GBytes>.

a bytes buffer containing the received bytes, or NULL on error

address

Returned: a <GSocketAddress>.

return location for a <GSocketAddress>

Method: socket:receive-from! self buffer [cancellable] ⇒ %return address buffer
Method: receive-from! self buffer [cancellable] ⇒ %return address buffer

Receive data (up to size bytes) from a socket.

If address is non-#f then address will be set equal to the source address of the received packet. address is owned by the caller.

See socket:receive! for additional information.

self

Passed: a <GSocket>.

a <GSocket>

buffer

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer. It is filled in, and returned: a bytevector.

a buffer to read data into (which should be at least size bytes long).

cancellable

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

a <GCancellable> or #f

%return

Returned: a 64-bit integer.

Number of bytes read, or 0 if the connection was closed by the peer, or -1 on error

address

Returned: a <GSocketAddress>.

a pointer to a <GSocketAddress> pointer, or #f

size

Not returned: it is the length of buffer.

the number of bytes you want to read from the socket

Method: socket:receive-with-blocking! self buffer blocking [cancellable] ⇒ %return buffer
Method: receive-with-blocking! self buffer blocking [cancellable] ⇒ %return buffer

This behaves exactly the same as socket:receive!, except that the choice of blocking or non-blocking behavior is determined by the blocking argument rather than by self’s properties.

self

Passed: a <GSocket>.

a <GSocket>

buffer

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer. It is filled in, and returned: a bytevector.

a buffer to read data into (which should be at least size bytes long).

blocking

Passed: a boolean.

whether to do blocking or non-blocking I/O

cancellable

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

a <GCancellable> or #f

%return

Returned: a 64-bit integer.

Number of bytes read, or 0 if the connection was closed by the peer, or -1 on error

size

Not returned: it is the length of buffer.

the number of bytes you want to read from the socket

Method: socket:send self buffer [cancellable] ⇒ %return
Method: send self buffer [cancellable] ⇒ %return

Tries to send size bytes from buffer on the socket. This is mainly used by connection-oriented sockets; it is identical to socket:send-to with address set to #f.

If the socket is in blocking mode the call will block until there is space for the data in the socket queue. If there is no space available and the socket is in non-blocking mode a would-block error will be returned. To be notified when space is available, wait for the out condition. Note though that you may still receive would-block from socket:send even if you were previously notified of a out condition. (On Windows in particular, this is very common due to the way the underlying APIs work.)

On error -1 is returned and error is set accordingly.

self

Passed: a <GSocket>.

a <GSocket>

buffer

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer.

the buffer containing the data to send.

cancellable

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

a <GCancellable> or #f

%return

Returned: a 64-bit integer.

Number of bytes written (which may be less than size), or -1 on error

size

Not passed: guile-gi gives the length of buffer.

the number of bytes to send

Method: socket:send-to self address buffer [cancellable] ⇒ %return
Method: send-to self address buffer [cancellable] ⇒ %return

Tries to send size bytes from buffer to address. If address is #f then the message is sent to the default receiver (set by socket:connect?).

See socket:send for additional information.

self

Passed: a <GSocket>.

a <GSocket>

address

Passed: a <GSocketAddress>, or #f.

a <GSocketAddress>, or #f

buffer

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer.

the buffer containing the data to send.

cancellable

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

a <GCancellable> or #f

%return

Returned: a 64-bit integer.

Number of bytes written (which may be less than size), or -1 on error

size

Not passed: guile-gi gives the length of buffer.

the number of bytes to send

Method: socket:send-with-blocking self buffer blocking [cancellable] ⇒ %return
Method: send-with-blocking self buffer blocking [cancellable] ⇒ %return

This behaves exactly the same as socket:send, except that the choice of blocking or non-blocking behavior is determined by the blocking argument rather than by self’s properties.

self

Passed: a <GSocket>.

a <GSocket>

buffer

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer.

the buffer containing the data to send.

blocking

Passed: a boolean.

whether to do blocking or non-blocking I/O

cancellable

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

a <GCancellable> or #f

%return

Returned: a 64-bit integer.

Number of bytes written (which may be less than size), or -1 on error

size

Not passed: guile-gi gives the length of buffer.

the number of bytes to send

Method: socket:set-blocking self blocking
Method: set-blocking self blocking

Sets the blocking mode of the socket. In blocking mode all operations (which don’t take an explicit blocking parameter) block until they succeed or there is an error. In non-blocking mode all functions return results immediately or with a would-block error.

All sockets are created in blocking mode. However, note that the platform level socket is always non-blocking, and blocking mode is a GSocket level feature.

self

Passed: a <GSocket>.

a <GSocket>.

blocking

Passed: a boolean.

Whether to use blocking I/O or not.

Method: socket:set-broadcast self broadcast
Method: set-broadcast self broadcast

Sets whether self should allow sending to broadcast addresses. This is #f by default.

self

Passed: a <GSocket>.

a <GSocket>.

broadcast

Passed: a boolean.

whether self should allow sending to broadcast addresses

Method: socket:set-keepalive self keepalive
Method: set-keepalive self keepalive

Sets or unsets the SO_KEEPALIVE flag on the underlying socket. When this flag is set on a socket, the system will attempt to verify that the remote socket endpoint is still present if a sufficiently long period of time passes with no data being exchanged. If the system is unable to verify the presence of the remote endpoint, it will automatically close the connection.

This option is only functional on certain kinds of sockets. (Notably, tcp sockets.)

The exact time between pings is system- and protocol-dependent, but will normally be at least two hours. Most commonly, you would set this flag on a server socket if you want to allow clients to remain idle for long periods of time, but also want to ensure that connections are eventually garbage-collected if clients crash or become unreachable.

self

Passed: a <GSocket>.

a <GSocket>.

keepalive

Passed: a boolean.

Value for the keepalive flag

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

Sets the maximum number of outstanding connections allowed when listening on this socket. If more clients than this are connecting to the socket and the application is not handling them on time then the new connections will be refused.

Note that this must be called before socket:listen? and has no effect if called after that.

self

Passed: a <GSocket>.

a <GSocket>.

backlog

Passed: a 32-bit integer.

the maximum number of pending connections.

Method: socket:set-multicast-loopback self loopback
Method: set-multicast-loopback self loopback

Sets whether outgoing multicast packets will be received by sockets listening on that multicast address on the same host. This is #t by default.

self

Passed: a <GSocket>.

a <GSocket>.

loopback

Passed: a boolean.

whether self should receive messages sent to its multicast groups from the local host

Method: socket:set-multicast-ttl self ttl
Method: set-multicast-ttl self ttl

Sets the time-to-live for outgoing multicast datagrams on self. By default, this is 1, meaning that multicast packets will not leave the local network.

self

Passed: a <GSocket>.

a <GSocket>.

ttl

Passed: an unsigned 32-bit integer.

the time-to-live value for all multicast datagrams on self

Method: socket:set-option? self level optname value ⇒ %return
Method: set-option? self level optname value ⇒ %return

Sets the value of an integer-valued option on self, as with setsockopt(). (If you need to set a non-integer-valued option, you will need to call setsockopt() directly.)

The ‘<gio/gnetworking.h>‘ header pulls in system headers that will define most of the standard/portable socket options. For unusual socket protocols or platform-dependent options, you may need to include additional headers.

self

Passed: a <GSocket>.

a <GSocket>

level

Passed: a 32-bit integer.

the "API level" of the option (eg, SOL_SOCKET)

optname

Passed: a 32-bit integer.

the "name" of the option (eg, SO_BROADCAST)

value

Passed: a 32-bit integer.

the value to set the option to

%return

Returned: a boolean.

success or failure. On failure, error will be set, and the system error value (errno or WSAGetLastError()) will still be set to the result of the setsockopt() call.

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

Sets the time in seconds after which I/O operations on self will time out if they have not yet completed.

On a blocking socket, this means that any blocking <GSocket> operation will time out after timeout seconds of inactivity, returning timed-out.

On a non-blocking socket, calls to socket:condition-wait? will also fail with timed-out after the given time. Sources created with g_socket_create_source() will trigger after timeout seconds of inactivity, with the requested condition set, at which point calling socket:receive!, socket:send, socket:check-connect-result?, etc, will fail with timed-out.

If timeout is 0 (the default), operations will never time out on their own.

Note that if an I/O operation is interrupted by a signal, this may cause the timeout to be reset.

self

Passed: a <GSocket>.

a <GSocket>.

timeout

Passed: an unsigned 32-bit integer.

the timeout for self, in seconds, or 0 for none

Method: socket:set-ttl self ttl
Method: set-ttl self ttl

Sets the time-to-live for outgoing unicast packets on self. By default the platform-specific default value is used.

self

Passed: a <GSocket>.

a <GSocket>.

ttl

Passed: an unsigned 32-bit integer.

the time-to-live value for all unicast packets on self

Method: socket:shutdown? self shutdown-read shutdown-write ⇒ %return
Method: shutdown? self shutdown-read shutdown-write ⇒ %return

Shut down part or all of a full-duplex connection.

If shutdown-read is #t then the receiving side of the connection is shut down, and further reading is disallowed.

If shutdown-write is #t then the sending side of the connection is shut down, and further writing is disallowed.

It is allowed for both shutdown-read and shutdown-write to be #t.

One example where it is useful to shut down only one side of a connection is graceful disconnect for TCP connections where you close the sending side, then wait for the other side to close the connection, thus ensuring that the other side saw all sent data.

self

Passed: a <GSocket>.

a <GSocket>

shutdown-read

Passed: a boolean.

whether to shut down the read side

shutdown-write

Passed: a boolean.

whether to shut down the write side

%return

Returned: a boolean.

#t on success, #f on error

Method: socket:speaks-ipv4? self ⇒ %return
Method: speaks-ipv4? self ⇒ %return

Checks if a socket is capable of speaking IPv4.

IPv4 sockets are capable of speaking IPv4. On some operating systems and under some combinations of circumstances IPv6 sockets are also capable of speaking IPv4. See RFC 3493 section 3.7 for more information.

No other types of sockets are currently considered as being capable of speaking IPv4.

self

Passed: a <GSocket>.

a <GSocket>

%return

Returned: a boolean.

#t if this socket can be used with IPv4.