Class: Gio::Socket

Inherits:
Object
  • Object
show all
Defined in:
(unknown)

Instance Method Summary collapse

Instance Method Details

#accept(cancellable) ⇒ Gio::Socket

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 socket must be bound to a local address with g_socket_bind() and must be listening for incoming connections (g_socket_listen()).

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

Parameters:

Returns:

  • (Gio::Socket)

    a new #GSocket, or nil on error. Free the returned object with g_object_unref().

#available_bytesGio::gssize

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

If socket 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 g_socket_receive() with a buffer of that size, rather than calling g_socket_get_available_bytes() first and then doing a receive of exactly the right size. without blocking or truncating, or -1 on error.

Returns:

  • (Gio::gssize)

    the number of bytes that can be read from the socket

#bind(address, allow_reuse) ⇒ TrueClass

When a socket is created it is attached to an address family, but it doesn’t have an address in this family. g_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 g_socket_listen() and g_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 socket is a TCP socket, then allow_reuse controls the setting of the ‘SO_REUSEADDR` socket option; normally it should be true for server sockets (sockets that you will eventually call g_socket_accept() on), and false for client sockets. (Failing to set this flag on a server socket may cause g_socket_bind() to return %G_IO_ERROR_ADDRESS_IN_USE if the server program is stopped and then immediately restarted.)

If socket 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.)

Parameters:

  • address (Gio::SocketAddress)

    a GSocket::Address specifying the local address.

  • allow_reuse (TrueClass)

    whether to allow reusing this address

Returns:

  • (TrueClass)

    true on success, false on error.

#blockingTrueClass

Gets the blocking mode of the socket. For details on blocking I/O, see g_socket_set_blocking().

Returns:

  • (TrueClass)

    true if blocking I/O is used, false otherwise.

#blocking=(blocking) ⇒ TrueClass

Parameters:

  • blocking (TrueClass)

Returns:

  • (TrueClass)

    blocking

  • (TrueClass)

    blocking

#blocking?TrueClass

Returns blocking.

Returns:

  • (TrueClass)

    blocking

#broadcastTrueClass

Gets the broadcast setting on socket; if true, it is possible to send packets to broadcast addresses.

Returns:

  • (TrueClass)

    the broadcast setting on socket

#broadcast=(broadcast) ⇒ TrueClass

Whether the socket should allow sending to broadcast addresses.

Parameters:

  • broadcast (TrueClass)

Returns:

  • (TrueClass)

    broadcast

  • (TrueClass)

    broadcast

#broadcast?TrueClass

Whether the socket should allow sending to broadcast addresses.

Returns:

  • (TrueClass)

    broadcast

#check_connect_resultTrueClass

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

Returns:

  • (TrueClass)

    true if no error, false otherwise, setting error to the error

#closeTrueClass

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 %G_IO_ERROR_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 %G_IO_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 g_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 g_socket_close(). (This is what GTcp::Connection does if you call g_tcp_connection_set_graceful_disconnect(). But of course, this only works if the client will close its connection after the server does.)

Returns:

  • (TrueClass)

    true on success, false on error

#condition_check(condition) ⇒ GLib::IOCondition

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

Note that on Windows, it is possible for an operation to return %G_IO_ERROR_WOULD_BLOCK even immediately after g_socket_condition_check() has claimed that the socket is ready for writing. Rather than calling g_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 %G_IO_ERROR_WOULD_BLOCK.

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

This call never blocks.

Parameters:

Returns:

#condition_timed_wait(condition, timeout_us, cancellable) ⇒ TrueClass

Waits for up to timeout_us microseconds for condition to become true on socket. If the condition is met, true is returned.

If cancellable is cancelled before the condition is met, or if timeout_us (or the socket’s #GSocket:timeout) is reached before the condition is met, then false is returned and error, if non-nil, is set to the appropriate value (%G_IO_ERROR_CANCELLED or %G_IO_ERROR_TIMED_OUT).

If you don’t want a timeout, use g_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.

Parameters:

  • condition (GLib::IOCondition)

    a #GIOCondition mask to wait for

  • timeout_us (Integer)

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

  • cancellable (Gio::Cancellable)

    a #GCancellable, or nil

Returns:

  • (TrueClass)

    true if the condition was met, false otherwise

#condition_wait(condition, cancellable) ⇒ TrueClass

Waits for condition to become true on socket. When the condition is met, true 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 false is returned and error, if non-nil, is set to the appropriate value (%G_IO_ERROR_CANCELLED or %G_IO_ERROR_TIMED_OUT).

See also g_socket_condition_timed_wait().

Parameters:

Returns:

  • (TrueClass)

    true if the condition was met, false otherwise

#connect(address, cancellable) ⇒ TrueClass

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 g_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 %G_IO_ERROR_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 g_socket_check_connect_result().

Parameters:

Returns:

  • (TrueClass)

    true if connected, false on error.

#connection_factory_create_connectionGio::SocketConnection

Creates a GSocket::Connection subclass of the right type for socket.

Returns:

#create_source(condition, cancellable) ⇒ GLib::Source

Creates a #GSource that can be attached to a %GMainContext to monitor for the availability of the specified condition on the socket. The #GSource keeps a reference to the socket.

The callback on the source is of the GSocket::SourceFunc type.

It is meaningless to specify %G_IO_ERR or %G_IO_HUP in condition; these conditions will always be reported output if they are true.

cancellable if not nil can be used to cancel the source, which will cause the source to trigger, reporting the current condition (which is likely 0 unless cancellation happened at the same time as a condition change). You can check for this in the callback using g_cancellable_is_cancelled().

If socket has a timeout set, and it is reached before condition occurs, the source will then trigger anyway, reporting %G_IO_IN or %G_IO_OUT depending on condition. However, socket will have been marked as having had a timeout, and so the next #GSocket I/O method you call will then fail with a %G_IO_ERROR_TIMED_OUT.

Parameters:

Returns:

  • (GLib::Source)

    a newly allocated %GSource, free with g_source_unref().

#credentialsGio::Credentials

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

If this operation isn’t supported on the OS, the method fails with the %G_IO_ERROR_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

Other ways to obtain credentials from a foreign peer includes the GUnix::CredentialsMessage type and g_unix_connection_send_credentials() / g_unix_connection_receive_credentials() functions. that must be freed with g_object_unref().

Returns:

#familyGio::SocketFamily

Returns family.

Returns:

#family=(family) ⇒ Gio::SocketFamily

Parameters:

Returns:

#fdInteger

Returns fd.

Returns:

  • (Integer)

    fd

#fd=(fd) ⇒ Integer

Parameters:

  • fd (Integer)

Returns:

  • (Integer)

    fd

  • (Integer)

    fd

#get_option(level, optname, value) ⇒ TrueClass

Gets the value of an integer-valued option on socket, 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; g_socket_get_option() will handle the conversion internally.

Parameters:

  • level (Integer)

    the “API level” of the option (eg, ‘SOL_SOCKET`)

  • optname (Integer)

    the “name” of the option (eg, ‘SO_BROADCAST`)

  • value (Integer)

    return location for the option value

Returns:

  • (TrueClass)

    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.

#is_closedTrueClass

Checks whether a socket is closed.

Returns:

  • (TrueClass)

    true if socket is closed, false otherwise

#is_connectedTrueClass

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

If using g_socket_shutdown(), this function will return true until the socket has been shut down for reading and writing. If you do a non-blocking connect, this function will not return true until after you call g_socket_check_connect_result().

Returns:

  • (TrueClass)

    true if socket is connected, false otherwise.

#join_multicast_group(group, source_specific, iface) ⇒ TrueClass

Registers socket to receive multicast messages sent to group. socket must be a %G_SOCKET_TYPE_DATAGRAM socket, and must have been bound to an appropriate interface and port with g_socket_bind().

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

If source_specific is true, source-specific multicast as defined in RFC 4604 is used. Note that on older platforms this may fail with a %G_IO_ERROR_NOT_SUPPORTED error.

To bind to a given source-specific multicast address, use g_socket_join_multicast_group_ssm() instead.

Parameters:

  • group (Gio::InetAddress)

    a GInet::Address specifying the group address to join.

  • source_specific (TrueClass)

    true if source-specific multicast should be used

  • iface (String)

    Name of the interface to use, or nil

Returns:

  • (TrueClass)

    true on success, false on error.

#join_multicast_group_ssm(group, source_specific, iface) ⇒ TrueClass

Registers socket to receive multicast messages sent to group. socket must be a %G_SOCKET_TYPE_DATAGRAM socket, and must have been bound to an appropriate interface and port with g_socket_bind().

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

If source_specific is not nil, use source-specific multicast as defined in RFC 4604. Note that on older platforms this may fail with a %G_IO_ERROR_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. source-specific multicast address or nil to ignore.

Parameters:

  • group (Gio::InetAddress)

    a GInet::Address specifying the group address to join.

  • source_specific (Gio::InetAddress)

    a GInet::Address specifying the

  • iface (String)

    Name of the interface to use, or nil

Returns:

  • (TrueClass)

    true on success, false on error.

#keepaliveTrueClass

Gets the keepalive mode of the socket. For details on this, see g_socket_set_keepalive().

Returns:

  • (TrueClass)

    true if keepalive is active, false otherwise.

#keepalive=(keepalive) ⇒ TrueClass

Parameters:

  • keepalive (TrueClass)

Returns:

  • (TrueClass)

    keepalive

  • (TrueClass)

    keepalive

#keepalive?TrueClass

Returns keepalive.

Returns:

  • (TrueClass)

    keepalive

#leave_multicast_group(group, source_specific, iface) ⇒ TrueClass

Removes socket 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).

socket 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 g_socket_leave_multicast_group_ssm() instead.

Parameters:

  • group (Gio::InetAddress)

    a GInet::Address specifying the group address to leave.

  • source_specific (TrueClass)

    true if source-specific multicast was used

  • iface (String)

    Interface used

Returns:

  • (TrueClass)

    true on success, false on error.

#leave_multicast_group_ssm(group, source_specific, iface) ⇒ TrueClass

Removes socket 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).

socket remains bound to its address and port, and can still receive unicast messages after calling this. source-specific multicast address or nil to ignore.

Parameters:

  • group (Gio::InetAddress)

    a GInet::Address specifying the group address to leave.

  • source_specific (Gio::InetAddress)

    a GInet::Address specifying the

  • iface (String)

    Name of the interface to use, or nil

Returns:

  • (TrueClass)

    true on success, false on error.

#listenTrueClass

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

Before calling this the socket must be bound to a local address using g_socket_bind().

To set the maximum amount of outstanding clients, use g_socket_set_listen_backlog().

Returns:

  • (TrueClass)

    true on success, false on error.

#listen_backlogInteger

Returns listen-backlog.

Returns:

  • (Integer)

    listen-backlog

#listen_backlog=(listen_backlog) ⇒ Integer

Parameters:

  • listen_backlog (Integer)

Returns:

  • (Integer)

    listen-backlog

  • (Integer)

    listen-backlog

#local_addressGio::SocketAddress

Returns local-address.

Returns:

#local_address=(local_address) ⇒ Gio::SocketAddress

Parameters:

Returns:

#multicast_loopbackTrueClass

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

Returns:

  • (TrueClass)

    the multicast loopback setting on socket

#multicast_loopback=(multicast_loopback) ⇒ TrueClass

Whether outgoing multicast packets loop back to the local host.

Parameters:

  • multicast_loopback (TrueClass)

Returns:

  • (TrueClass)

    multicast-loopback

  • (TrueClass)

    multicast-loopback

#multicast_loopback?TrueClass

Whether outgoing multicast packets loop back to the local host.

Returns:

  • (TrueClass)

    multicast-loopback

#multicast_ttlInteger

Time-to-live out outgoing multicast packets

Returns:

  • (Integer)

    multicast-ttl

#multicast_ttl=(multicast_ttl) ⇒ Integer

Time-to-live out outgoing multicast packets

Parameters:

  • multicast_ttl (Integer)

Returns:

  • (Integer)

    multicast-ttl

  • (Integer)

    multicast-ttl

#new(family, type, protocol) ⇒ Gio::Socket

Creates a new #GSocket with the defined family, type and protocol. If protocol is 0 (%G_SOCKET_PROTOCOL_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. GSocket::Protocol 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 GSocket::Protocol if you know the protocol number used for it.

Parameters:

Returns:

  • (Gio::Socket)

    a #GSocket or nil on error. Free the returned object with g_object_unref().

#new_from_fd(fd) ⇒ Gio::Socket

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 %G_IO_ERROR_FAILED

Parameters:

  • fd (Integer)

    a native socket file descriptor.

Returns:

  • (Gio::Socket)

    a #GSocket or nil on error. Free the returned object with g_object_unref().

#protocolGio::SocketProtocol

Returns protocol.

Returns:

#protocol=(protocol) ⇒ Gio::SocketProtocol

Parameters:

Returns:

#receive(buffer, size, cancellable) ⇒ Gio::gssize

Receive data (up to size bytes) from a socket. This is mainly used by connection-oriented sockets; it is identical to g_socket_receive_from() with address set to nil.

For %G_SOCKET_TYPE_DATAGRAM and %G_SOCKET_TYPE_SEQPACKET sockets, g_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 %G_SOCKET_TYPE_STREAM sockets, g_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 g_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 %G_IO_ERROR_WOULD_BLOCK error will be returned. To be notified when data is available, wait for the %G_IO_IN condition.

On error -1 is returned and error is set accordingly. the peer, or -1 on error

Parameters:

  • buffer (Array<Integer>)

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

  • size (Integer)

    the number of bytes you want to read from the socket

  • cancellable (Gio::Cancellable)

    a %GCancellable or nil

Returns:

  • (Gio::gssize)

    Number of bytes read, or 0 if the connection was closed by

#receive_from(address, buffer, size, cancellable) ⇒ Gio::gssize

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

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

See g_socket_receive() for additional information. the peer, or -1 on error

Parameters:

  • address (Gio::SocketAddress)

    a pointer to a GSocket::Address pointer, or nil

  • buffer (Array<Integer>)

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

  • size (Integer)

    the number of bytes you want to read from the socket

  • cancellable (Gio::Cancellable)

    a %GCancellable or nil

Returns:

  • (Gio::gssize)

    Number of bytes read, or 0 if the connection was closed by

#receive_message(address, vectors, num_vectors, messages, num_messages, flags, cancellable) ⇒ Gio::gssize

Receive data from a socket. For receiving multiple messages, see g_socket_receive_messages(); for easier use, see g_socket_receive() and g_socket_receive_from().

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

vector must point to an array of GInput::Vector structs and num_vectors must be the length of this array. These structs describe the buffers that received data will be scattered into. If num_vectors is -1, then vectors is assumed to be terminated by a GInput::Vector with a nil buffer pointer.

As a special case, if num_vectors is 0 (in which case, vectors may of course be nil), then a single byte is received and discarded. This is to facilitate the common practice of sending a single ‘0’ byte for the purposes of transferring ancillary data.

messages, if non-nil, will be set to point to a newly-allocated array of GSocket::ControlMessage instances or nil if no such messages was received. These correspond to the control messages received from the kernel, one GSocket::ControlMessage per message from the kernel. This array is nil-terminated and must be freed by the caller using g_free() after calling g_object_unref() on each element. If messages is nil, any control messages received will be discarded.

num_messages, if non-nil, will be set to the number of control messages received.

If both messages and num_messages are non-nil, then num_messages gives the number of GSocket::ControlMessage instances in messages (ie: not including the nil terminator).

flags is an in/out parameter. The commonly available arguments for this are available in the GSocket::MsgFlags enum, but the values there are the same as the system values, and the flags are passed in as-is, so you can pass in system-specific flags too (and g_socket_receive_message() may pass system-specific flags out). Flags passed in to the parameter affect the receive operation; flags returned out of it are relevant to the specific returned message.

As with g_socket_receive(), data may be discarded if socket is %G_SOCKET_TYPE_DATAGRAM or %G_SOCKET_TYPE_SEQPACKET and you do not provide enough buffer space to read a complete message. You can pass %G_SOCKET_MSG_PEEK in flags to peek at the current message without removing it from the receive queue, but there is no portable way to find out the length of the message other than by reading it into a sufficiently-large buffer.

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 %G_IO_ERROR_WOULD_BLOCK error will be returned. To be notified when data is available, wait for the %G_IO_IN condition.

On error -1 is returned and error is set accordingly. the peer, or -1 on error

Parameters:

  • address (Gio::SocketAddress)

    a pointer to a GSocket::Address pointer, or nil

  • vectors (Array<Gio::InputVector>)

    an array of GInput::Vector structs

  • num_vectors (Integer)

    the number of elements in vectors, or -1

  • messages (Array<Gio::SocketControlMessage>)

    a pointer which may be filled with an array of GSocket::ControlMessages, or nil

  • num_messages (Integer)

    a pointer which will be filled with the number of elements in messages, or nil

  • flags (Integer)

    a pointer to an int containing GSocket::MsgFlags flags, which may additionally contain [other platform specific flags](man7.org/linux/man-pages/man2/recv.2.html)

  • cancellable (Gio::Cancellable)

    a %GCancellable or nil

Returns:

  • (Gio::gssize)

    Number of bytes read, or 0 if the connection was closed by

#receive_messages(messages, num_messages, flags, cancellable) ⇒ Integer

Receive multiple data messages from socket in one go. This is the most complicated and fully-featured version of this call. For easier use, see g_socket_receive(), g_socket_receive_from(), and g_socket_receive_message().

messages must point to an array of GInput::Message structs and num_messages must be the length of this array. Each GInput::Message contains a pointer to an array of GInput::Vector structs describing the buffers that the data received in each message will be written to. Using multiple GInput::Vectors is more memory-efficient than manually copying data out of a single buffer to multiple sources, and more system-call-efficient than making multiple calls to g_socket_receive(), such as in scenarios where a lot of data packets need to be received (e.g. high-bandwidth video streaming over RTP/UDP).

flags modify how all messages are received. The commonly available arguments for this are available in the GSocket::MsgFlags enum, but the values there are the same as the system values, and the flags are passed in as-is, so you can pass in system-specific flags too. These flags affect the overall receive operation. Flags affecting individual messages are returned in GInput::Message.flags.

The other members of GInput::Message are treated as described in its documentation.

If #GSocket:blocking is true the call will block until num_messages have been received, or the end of the stream is reached.

If #GSocket:blocking is false the call will return up to num_messages without blocking, or %G_IO_ERROR_WOULD_BLOCK if no messages are queued in the operating system to be received.

In blocking mode, if #GSocket:timeout is positive and is reached before any messages are received, %G_IO_ERROR_TIMED_OUT is returned, otherwise up to num_messages are returned. (Note: This is effectively the behaviour of ‘MSG_WAITFORONE` with recvmmsg().)

To be notified when messages are available, wait for the %G_IO_IN condition. Note though that you may still receive %G_IO_ERROR_WOULD_BLOCK from g_socket_receive_messages() even if you were previously notified of a %G_IO_IN condition.

If the remote peer closes the connection, any messages queued in the operating system will be returned, and subsequent calls to g_socket_receive_messages() will return 0 (with no error set).

On error -1 is returned and error is set accordingly. An error will only be returned if zero messages could be received; otherwise the number of messages successfully received before the error will be returned.

Parameters:

  • messages (Array<Gio::InputMessage>)

    an array of GInput::Message structs

  • num_messages (Integer)

    the number of elements in messages

  • flags (Integer)

    an int containing GSocket::MsgFlags flags for the overall operation, which may additionally contain [other platform specific flags](man7.org/linux/man-pages/man2/recv.2.html)

  • cancellable (Gio::Cancellable)

    a %GCancellable or nil

Returns:

  • (Integer)

    number of messages received, or -1 on error. Note that the number of messages received may be smaller than num_messages if in non-blocking mode, if the peer closed the connection, or if num_messages was larger than ‘UIO_MAXIOV` (1024), in which case the caller may re-try to receive the remaining messages.

#receive_with_blocking(buffer, size, blocking, cancellable) ⇒ Gio::gssize

This behaves exactly the same as g_socket_receive(), except that the choice of blocking or non-blocking behavior is determined by the blocking argument rather than by socket’s properties. the peer, or -1 on error

Parameters:

  • buffer (Array<Integer>)

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

  • size (Integer)

    the number of bytes you want to read from the socket

  • blocking (TrueClass)

    whether to do blocking or non-blocking I/O

  • cancellable (Gio::Cancellable)

    a %GCancellable or nil

Returns:

  • (Gio::gssize)

    Number of bytes read, or 0 if the connection was closed by

#remote_addressGio::SocketAddress

Returns remote-address.

Returns:

#remote_address=(remote_address) ⇒ Gio::SocketAddress

Parameters:

Returns:

#send(buffer, size, cancellable) ⇒ Gio::gssize

Tries to send size bytes from buffer on the socket. This is mainly used by connection-oriented sockets; it is identical to g_socket_send_to() with address set to nil.

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 %G_IO_ERROR_WOULD_BLOCK error will be returned. To be notified when space is available, wait for the %G_IO_OUT condition. Note though that you may still receive %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously notified of a %G_IO_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. on error

Parameters:

  • buffer (Array<Integer>)

    the buffer containing the data to send.

  • size (Integer)

    the number of bytes to send

  • cancellable (Gio::Cancellable)

    a %GCancellable or nil

Returns:

  • (Gio::gssize)

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

#send_message(address, vectors, num_vectors, messages, num_messages, flags, cancellable) ⇒ Gio::gssize

Send data to address on socket. For sending multiple messages see g_socket_send_messages(); for easier use, see g_socket_send() and g_socket_send_to().

If address is nil then the message is sent to the default receiver (set by g_socket_connect()).

vectors must point to an array of GOutput::Vector structs and num_vectors must be the length of this array. (If num_vectors is -1, then vectors is assumed to be terminated by a GOutput::Vector with a nil buffer pointer.) The GOutput::Vector structs describe the buffers that the sent data will be gathered from. Using multiple GOutput::Vectors is more memory-efficient than manually copying data from multiple sources into a single buffer, and more network-efficient than making multiple calls to g_socket_send().

messages, if non-nil, is taken to point to an array of num_messages GSocket::ControlMessage instances. These correspond to the control messages to be sent on the socket. If num_messages is -1 then messages is treated as a nil-terminated array.

flags modify how the message is sent. The commonly available arguments for this are available in the GSocket::MsgFlags enum, but the values there are the same as the system values, and the flags are passed in as-is, so you can pass in system-specific flags too.

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 %G_IO_ERROR_WOULD_BLOCK error will be returned. To be notified when space is available, wait for the %G_IO_OUT condition. Note though that you may still receive %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously notified of a %G_IO_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. on error

Parameters:

  • address (Gio::SocketAddress)

    a GSocket::Address, or nil

  • vectors (Array<Gio::OutputVector>)

    an array of GOutput::Vector structs

  • num_vectors (Integer)

    the number of elements in vectors, or -1

  • messages (Array<Gio::SocketControlMessage>)

    a pointer to an array of GSocket::ControlMessages, or nil.

  • num_messages (Integer)

    number of elements in messages, or -1.

  • flags (Integer)

    an int containing GSocket::MsgFlags flags, which may additionally contain [other platform specific flags](man7.org/linux/man-pages/man2/recv.2.html)

  • cancellable (Gio::Cancellable)

    a %GCancellable or nil

Returns:

  • (Gio::gssize)

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

#send_message_with_timeout(address, vectors, num_vectors, messages, num_messages, flags, timeout_us, bytes_written, cancellable) ⇒ Gio::PollableReturn

This behaves exactly the same as g_socket_send_message(), except that the choice of timeout behavior is determined by the timeout_us argument rather than by socket’s properties.

On error %G_POLLABLE_RETURN_FAILED is returned and error is set accordingly, or if the socket is currently not writable %G_POLLABLE_RETURN_WOULD_BLOCK is returned. bytes_written will contain 0 in both cases. %G_POLLABLE_RETURN_WOULD_BLOCK if the socket is currently not writable, or %G_POLLABLE_RETURN_FAILED if an error happened and error is set.

Parameters:

  • address (Gio::SocketAddress)

    a GSocket::Address, or nil

  • vectors (Array<Gio::OutputVector>)

    an array of GOutput::Vector structs

  • num_vectors (Integer)

    the number of elements in vectors, or -1

  • messages (Array<Gio::SocketControlMessage>)

    a pointer to an array of GSocket::ControlMessages, or nil.

  • num_messages (Integer)

    number of elements in messages, or -1.

  • flags (Integer)

    an int containing GSocket::MsgFlags flags, which may additionally contain [other platform specific flags](man7.org/linux/man-pages/man2/recv.2.html)

  • timeout_us (Integer)

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

  • bytes_written (Integer)

    location to store the number of bytes that were written to the socket

  • cancellable (Gio::Cancellable)

    a %GCancellable or nil

Returns:

#send_messages(messages, num_messages, flags, cancellable) ⇒ Integer

Send multiple data messages from socket in one go. This is the most complicated and fully-featured version of this call. For easier use, see g_socket_send(), g_socket_send_to(), and g_socket_send_message().

messages must point to an array of GOutput::Message structs and num_messages must be the length of this array. Each GOutput::Message contains an address to send the data to, and a pointer to an array of GOutput::Vector structs to describe the buffers that the data to be sent for each message will be gathered from. Using multiple GOutput::Vectors is more memory-efficient than manually copying data from multiple sources into a single buffer, and more network-efficient than making multiple calls to g_socket_send(). Sending multiple messages in one go avoids the overhead of making a lot of syscalls in scenarios where a lot of data packets need to be sent (e.g. high-bandwidth video streaming over RTP/UDP), or where the same data needs to be sent to multiple recipients.

flags modify how the message is sent. The commonly available arguments for this are available in the GSocket::MsgFlags enum, but the values there are the same as the system values, and the flags are passed in as-is, so you can pass in system-specific flags too.

If the socket is in blocking mode the call will block until there is space for all the data in the socket queue. If there is no space available and the socket is in non-blocking mode a %G_IO_ERROR_WOULD_BLOCK error will be returned if no data was written at all, otherwise the number of messages sent will be returned. To be notified when space is available, wait for the %G_IO_OUT condition. Note though that you may still receive %G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously notified of a %G_IO_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. An error will only be returned if zero messages could be sent; otherwise the number of messages successfully sent before the error will be returned.

Parameters:

  • messages (Array<Gio::OutputMessage>)

    an array of GOutput::Message structs

  • num_messages (Integer)

    the number of elements in messages

  • flags (Integer)

    an int containing GSocket::MsgFlags flags, which may additionally contain [other platform specific flags](man7.org/linux/man-pages/man2/recv.2.html)

  • cancellable (Gio::Cancellable)

    a %GCancellable or nil

Returns:

  • (Integer)

    number of messages sent, or -1 on error. Note that the number of messages sent may be smaller than num_messages if the socket is non-blocking or if num_messages was larger than UIO_MAXIOV (1024), in which case the caller may re-try to send the remaining messages.

#send_to(address, buffer, size, cancellable) ⇒ Gio::gssize

Tries to send size bytes from buffer to address. If address is nil then the message is sent to the default receiver (set by g_socket_connect()).

See g_socket_send() for additional information. on error

Parameters:

  • address (Gio::SocketAddress)

    a GSocket::Address, or nil

  • buffer (Array<Integer>)

    the buffer containing the data to send.

  • size (Integer)

    the number of bytes to send

  • cancellable (Gio::Cancellable)

    a %GCancellable or nil

Returns:

  • (Gio::gssize)

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

#send_with_blocking(buffer, size, blocking, cancellable) ⇒ Gio::gssize

This behaves exactly the same as g_socket_send(), except that the choice of blocking or non-blocking behavior is determined by the blocking argument rather than by socket’s properties. on error

Parameters:

  • buffer (Array<Integer>)

    the buffer containing the data to send.

  • size (Integer)

    the number of bytes to send

  • blocking (TrueClass)

    whether to do blocking or non-blocking I/O

  • cancellable (Gio::Cancellable)

    a %GCancellable or nil

Returns:

  • (Gio::gssize)

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

#set_option(level, optname, value) ⇒ TrueClass

Sets the value of an integer-valued option on socket, 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.

Parameters:

  • level (Integer)

    the “API level” of the option (eg, ‘SOL_SOCKET`)

  • optname (Integer)

    the “name” of the option (eg, ‘SO_BROADCAST`)

  • value (Integer)

    the value to set the option to

Returns:

  • (TrueClass)

    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.

#shutdown(shutdown_read, shutdown_write) ⇒ TrueClass

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

If shutdown_read is true then the receiving side of the connection is shut down, and further reading is disallowed.

If shutdown_write is true 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 true.

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.

Parameters:

  • shutdown_read (TrueClass)

    whether to shut down the read side

  • shutdown_write (TrueClass)

    whether to shut down the write side

Returns:

  • (TrueClass)

    true on success, false on error

#socket_typeGio::SocketType

Gets the socket type of the socket.

Returns:

#speaks_ipv4TrueClass

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.

Returns:

  • (TrueClass)

    true if this socket can be used with IPv4.

#timeoutInteger

The timeout in seconds on socket I/O

Returns:

  • (Integer)

    timeout

#timeout=(timeout) ⇒ Integer

The timeout in seconds on socket I/O

Parameters:

  • timeout (Integer)

Returns:

  • (Integer)

    timeout

  • (Integer)

    timeout

#ttlInteger

Time-to-live for outgoing unicast packets

Returns:

  • (Integer)

    ttl

#ttl=(ttl) ⇒ Integer

Time-to-live for outgoing unicast packets

Parameters:

  • ttl (Integer)

Returns:

  • (Integer)

    ttl

  • (Integer)

    ttl

#typeGio::SocketType

Returns type.

Returns:

#type=(type) ⇒ Gio::SocketType

Parameters:

Returns: