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.
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
}
parent-instanceprivWhether I/O on this socket is blocking.
Its value is a boolean.
Set it with (set! (socket:blocking self) value).
Whether the socket should allow sending to broadcast addresses.
Its value is a boolean.
Set it with (set! (socket:broadcast self) value).
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.
The socket’s file descriptor.
Its value is a 32-bit integer.
It can only be set when the object is made, with #:fd.
Whether to keep the connection alive by sending periodic pings.
Its value is a boolean.
Set it with (set! (socket:keepalive self) value).
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).
The local address the socket is bound to.
Its value is a <GSocketAddress>, or
#f.
It is read only.
Whether outgoing multicast packets loop back to the local host.
Its value is a boolean.
Set it with (set! (socket:multicast-loopback self) value).
Time-to-live out outgoing multicast packets
Its value is an unsigned 32-bit integer.
Set it with (set! (socket:multicast-ttl self) value).
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.
The remote address the socket is connected to.
Its value is a <GSocketAddress>, or
#f.
It is read only.
The timeout in seconds on socket I/O
Its value is an unsigned 32-bit integer.
Set it with (set! (socket:timeout self) value).
Time-to-live for outgoing unicast packets
Its value is an unsigned 32-bit integer.
Set it with (set! (socket:ttl self) value).
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.
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.
Passed: a <GSocket>.
a <GSocket>.
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable> or #f
Returned: a <GSocket>.
a new <GSocket>, or #f on error. Free the
returned object with object:unref.
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.)
Passed: a <GSocket>.
a <GSocket>.
Passed: a <GSocketAddress>.
a <GSocketAddress> specifying the local
address.
Passed: a boolean.
whether to allow reusing this address
Returned: a boolean.
#t on success, #f on error.
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.
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.)
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.
Passed: a <GSocket>.
Passed: a <GIOCondition>, or a list of
its symbols, or one of them.
a <GIOCondition> mask to check
Returned: a <GIOCondition>.
the GIOCondition mask of the current state
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.
Passed: a <GSocket>.
Passed: a <GIOCondition>, or a list of
its symbols, or one of them.
a <GIOCondition> mask to wait for
Passed: a 64-bit integer.
the maximum time (in microseconds) to wait, or -1
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable>, or #f
Returned: a boolean.
#t if the condition was met, #f otherwise
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?.
Passed: a <GSocket>.
Passed: a <GIOCondition>, or a list of
its symbols, or one of them.
a <GIOCondition> mask to wait for
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable>, or #f
Returned: a boolean.
#t if the condition was met, #f otherwise
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?.
Passed: a <GSocket>.
a <GSocket>.
Passed: a <GSocketAddress>.
a <GSocketAddress> specifying the remote
address.
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable> or #f
Returned: a boolean.
#t if connected, #f on error.
Creates a <GSocketConnection> subclass of
the right type for self.
Passed: a <GSocket>.
Returned: a <GSocketConnection>.
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.
Gets the blocking mode of the socket. For details on blocking I/O, see
socket:set-blocking.
Gets the broadcast setting on self; if #t, it is possible
to send packets to broadcast addresses.
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:
Other ways to obtain credentials from a foreign peer includes the
<GUnixCredentialsMessage> type and
unix-connection:send-credentials? /
unix-connection:receive-credentials functions.
Passed: a <GSocket>.
a <GSocket>.
Returned: a <GCredentials>.
#f if error is set, otherwise a
<GCredentials> object that must be freed with
object:unref.
Gets the socket family of the socket.
Passed: a <GSocket>.
a <GSocket>.
Returned: a <GSocketFamily>.
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.
Gets the keepalive mode of the socket. For details on this, see
socket:set-keepalive.
Gets the listen backlog setting of the socket. For details on this, see
socket:set-listen-backlog.
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.
Passed: a <GSocket>.
a <GSocket>.
Returned: a <GSocketAddress>.
a <GSocketAddress> or #f on error.
Free the returned object with object:unref.
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.
Gets the multicast time-to-live setting on self; see
socket:set-multicast-ttl for more
details.
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.
Passed: a <GSocket>.
Passed: a 32-bit integer.
the "API level" of the option (eg, SOL_SOCKET)
Passed: a 32-bit integer.
the "name" of the option (eg, SO_BROADCAST)
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.
Returned: a 32-bit integer.
return location for the option value
Gets the socket protocol id the socket was created with. In case the protocol is unknown, -1 is returned.
Passed: a <GSocket>.
a <GSocket>.
Returned: a <GSocketProtocol>.
a protocol id, or -1 if unknown
Try to get the remote address of a connected socket. This is only useful for connection oriented sockets that have been connected.
Passed: a <GSocket>.
a <GSocket>.
Returned: a <GSocketAddress>.
a <GSocketAddress> or #f on error.
Free the returned object with object:unref.
Gets the socket type of the socket.
Passed: a <GSocket>.
a <GSocket>.
Returned: a <GSocketType>.
Gets the timeout setting of the socket. For details on this, see
socket:set-timeout.
Gets the unicast time-to-live setting on self; see
socket:set-ttl for more details.
Checks whether a socket is closed.
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?.
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.
Passed: a <GSocket>.
a <GSocket>.
Passed: a <GInetAddress>.
a <GInetAddress> specifying the group address
to join.
Passed, or left out: a <GInetAddress>, or
#f.
a <GInetAddress> specifying the
source-specific multicast address or #f to ignore.
Passed, or left out: a string, or #f.
Name of the interface to use, or #f
Returned: a boolean.
#t on success, #f on error.
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.
Passed: a <GSocket>.
a <GSocket>.
Passed: a <GInetAddress>.
a <GInetAddress> specifying the group address
to join.
Passed: a boolean.
#t if source-specific multicast should be used
Passed, or left out: a string, or #f.
Name of the interface to use, or #f
Returned: a boolean.
#t on success, #f on error.
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.
Passed: a <GSocket>.
a <GSocket>.
Passed: a <GInetAddress>.
a <GInetAddress> specifying the group address
to leave.
Passed, or left out: a <GInetAddress>, or
#f.
a <GInetAddress> specifying the
source-specific multicast address or #f to ignore.
Passed, or left out: a string, or #f.
Name of the interface to use, or #f
Returned: a boolean.
#t on success, #f on error.
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.
Passed: a <GSocket>.
a <GSocket>.
Passed: a <GInetAddress>.
a <GInetAddress> specifying the group address
to leave.
Passed: a boolean.
#t if source-specific multicast was used
Passed, or left out: a string, or #f.
Interface used
Returned: a boolean.
#t on success, #f on error.
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.
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.
Passed: a <GSocketFamily>, or one of its
symbols.
the socket family to use, e.g. ipv4.
Passed: a <GSocketType>, or one of its symbols.
the socket type to use.
Passed: a <GSocketProtocol>, or one of its
symbols.
the id of the protocol to use, or 0 for default.
Returned: a <GSocket>.
a <GSocket> or #f on error. Free the
returned object with object:unref.
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
Passed: a 32-bit integer.
a native socket file descriptor.
Returned: a <GSocket>.
a <GSocket> or #f on error. Free the
returned object with object:unref.
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.
Passed: a <GSocket>.
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).
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable> or #f
Returned: a 64-bit integer.
Number of bytes read, or 0 if the connection was closed by the peer, or -1 on error
Not returned: it is the length of buffer.
the number of bytes you want to read from the socket
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.
Passed: a <GSocket>.
Passed: an unsigned 64-bit integer.
the number of bytes you want to read from the socket
Passed: a 64-bit integer.
the timeout to wait for, in microseconds, or -1 to block
indefinitely
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable>, or NULL
Returned: a <GBytes>.
a bytes buffer containing the received bytes, or NULL on error
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.
Passed: a <GSocket>.
Passed: an unsigned 64-bit integer.
the number of bytes you want to read from the socket
Passed: a 64-bit integer.
the timeout to wait for, in microseconds, or -1 to block
indefinitely
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable>, or NULL
Returned: a <GBytes>.
a bytes buffer containing the received bytes, or NULL on error
Returned: a <GSocketAddress>.
return location for a <GSocketAddress>
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.
Passed: a <GSocket>.
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).
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable> or #f
Returned: a 64-bit integer.
Number of bytes read, or 0 if the connection was closed by the peer, or -1 on error
Returned: a <GSocketAddress>.
a pointer to a <GSocketAddress> pointer, or
#f
Not returned: it is the length of buffer.
the number of bytes you want to read from the socket
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.
Passed: a <GSocket>.
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).
Passed: a boolean.
whether to do blocking or non-blocking I/O
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable> or #f
Returned: a 64-bit integer.
Number of bytes read, or 0 if the connection was closed by the peer, or -1 on error
Not returned: it is the length of buffer.
the number of bytes you want to read from the socket
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.
Passed: a <GSocket>.
Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer.
the buffer containing the data to send.
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable> or #f
Returned: a 64-bit integer.
Number of bytes written (which may be less than size), or -1 on error
Not passed: guile-gi gives the length of buffer.
the number of bytes to send
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.
Passed: a <GSocket>.
Passed: a <GSocketAddress>, or #f.
a <GSocketAddress>, or #f
Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer.
the buffer containing the data to send.
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable> or #f
Returned: a 64-bit integer.
Number of bytes written (which may be less than size), or -1 on error
Not passed: guile-gi gives the length of buffer.
the number of bytes to send
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.
Passed: a <GSocket>.
Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer.
the buffer containing the data to send.
Passed: a boolean.
whether to do blocking or non-blocking I/O
Passed, or left out: a <GCancellable>, or
#f.
a <GCancellable> or #f
Returned: a 64-bit integer.
Number of bytes written (which may be less than size), or -1 on error
Not passed: guile-gi gives the length of buffer.
the number of bytes to send
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.
Sets whether self should allow sending to broadcast addresses.
This is #f by default.
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.
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.
Sets whether outgoing multicast packets will be received by sockets
listening on that multicast address on the same host. This is #t
by default.
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.
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.
Passed: a <GSocket>.
Passed: a 32-bit integer.
the "API level" of the option (eg, SOL_SOCKET)
Passed: a 32-bit integer.
the "name" of the option (eg, SO_BROADCAST)
Passed: a 32-bit integer.
the value to set the option to
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.
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.
Sets the time-to-live for outgoing unicast packets on self. By default the platform-specific default value is used.
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.
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.