glibmm: Gio::Socket Class Reference

glibmm 2.77.0

Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Related Functions | List of all members
Gio::Socket Class Reference
Portable Network I/O Functionality

Low-level socket object. More...

`#include <giomm/socket.h>`

Inheritance diagram for Gio::Socket:
[legend]

## Public Types
enum class | Type { Type::INVALID , Type::STREAM , Type::DATAGRAM , Type::SEQPACKET }
| Flags used when creating a Socket. More...
enum class | Protocol { Protocol::UNKNOWN = -1 , Protocol::DEFAULT = 0 , Protocol::TCP = 6 , Protocol::UDP = 17 , Protocol::SCTP = 132 }
| A protocol identifier is specified when creating a Socket, which is a family/type specific identifier, where 0 means the default protocol for the particular family/type. More...
enum class | MsgFlags { MsgFlags::NONE = 0x0 , MsgFlags::OOB = GLIB_SYSDEF_MSG_OOB , MsgFlags::PEEK = GLIB_SYSDEF_MSG_PEEK , MsgFlags::DONTROUTE = GLIB_SYSDEF_MSG_DONTROUTE }
| Flags used in g_socket_receive_message() and g_socket_send_message(). More...
Public Types inherited from Glib::Object
using | DestroyNotify = void(*)(gpointer data)
Public Types inherited from sigc::trackable
typedef internal::func_destroy_notify | func_destroy_notify
Public Types inherited from sigc::notifiable
typedef internal::func_destroy_notify | func_destroy_notify

## Public Member Functions
| Socket (Socket && src) noexcept
Socket & | operator= (Socket && src) noexcept
| ~Socket () noexcept override
GSocket * | gobj ()
| Provides access to the underlying C GObject. More...
const GSocket * | gobj () const
| Provides access to the underlying C GObject. More...
GSocket * | gobj_copy ()
| Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs. More...
void | bind (const Glib::RefPtr< SocketAddress > & address, bool allow_reuse)
| When a socket is created it is attached to an address family, but it doesn't have an address in this family. More...
void | listen ()
| Marks the socket as a server socket - a socket that is used to accept incoming requests using Socket::accept(). More...
Glib::RefPtr< Socket > | accept (const Glib::RefPtr< Cancellable > & cancellable)
| Accept incoming connections on a connection-based socket. More...
Glib::RefPtr< Socket > | accept ()
| A accept() convenience overload. More...
void | connect (const Glib::RefPtr< SocketAddress > & address, const Glib::RefPtr< Cancellable > & cancellable)
| Connect the socket to the specified remote address. More...
void | connect (const Glib::RefPtr< SocketAddress > & address)
| A connect() convenience overload. More...
void | check_connect_result ()
| Checks and resets the pending connect error for the socket. More...
gssize | receive (char * buffer, gsize size, const Glib::RefPtr< Cancellable > & cancellable)
| Receive data (up to size bytes) from a socket. More...
gssize | receive (char * buffer, gsize size)
| A receive() convenience overload. More...
gssize | receive_from (Glib::RefPtr< SocketAddress > & address, char * buffer, gsize size, const Glib::RefPtr< Cancellable > & cancellable)
gssize | receive_from (Glib::RefPtr< SocketAddress > & address, char * buffer, gsize size)
gssize | send (const gchar * buffer, gsize size, const Glib::RefPtr< Cancellable > & cancellable)
| Tries to send size bytes from buffer on the socket. More...
gssize | send (const gchar * buffer, gsize size)
| A send() convenience overload. More...
gssize | send_to (const Glib::RefPtr< SocketAddress > & address, const char * buffer, gsize size, const Glib::RefPtr< Cancellable > & cancellable)
| Tries to send size bytes from buffer to address. More...
gssize | send_to (const Glib::RefPtr< SocketAddress > & address, const char * buffer, gsize size)
| A send_to() convenience overload. More...
void | close ()
| Closes the socket, shutting down any active connection. More...
bool | is_closed ()
| Checks whether a socket is closed. More...
Glib::RefPtr< SocketSource > | create_source (Glib::IOCondition condition, const Glib::RefPtr< Cancellable > & cancellable={})
| Creates a SocketSource that can be attached to a Glib::MainContext to monitor for the availability of the specified condition on the socket. More...
void | shutdown (bool shutdown_read, bool shutdown_write)
| Shut down part or all of a full-duplex connection. More...
bool | is_connected ()
| Check whether the socket is connected. More...
gssize | get_available_bytes () const
| Get the amount of data pending in the OS input buffer, without blocking. More...
Glib::IOCondition | condition_check (Glib::IOCondition condition)
| Checks on the readiness of socket to perform operations. More...
void | condition_wait (Glib::IOCondition condition, const Glib::RefPtr< Cancellable > & cancellable)
| Waits for condition to become true on socket. More...
void | condition_wait (Glib::IOCondition condition)
| A condition_wait() convenience overload. More...
void | condition_timed_wait (Glib::IOCondition condition, gint64 timeout, const Glib::RefPtr< Cancellable > & cancellable)
| Waits for up to timeout microseconds for condition to become true on socket. More...
void | condition_timed_wait (Glib::IOCondition condition, gint64 timeout)
| A condition_timed_wait() convenience overload. More...
void | set_listen_backlog (int backlog)
| Sets the maximum number of outstanding connections allowed when listening on this socket. More...
int | get_listen_backlog () const
| Gets the listen backlog setting of the socket. More...
void | set_blocking (bool blocking)
| Sets the blocking mode of the socket. More...
bool | get_blocking () const
| Gets the blocking mode of the socket. More...
void | set_keepalive (bool keepalive)
| Sets or unsets the SO_KEEPALIVE flag on the underlying socket. More...
bool | get_keepalive () const
| Gets the keepalive mode of the socket. More...
SocketFamily | get_family () const
| Gets the socket family of the socket. More...
int | get_fd () const
| Returns the underlying OS socket object. More...
Glib::RefPtr< SocketAddress > | get_local_address () const
| Try to get the local address of a bound socket. More...
Glib::RefPtr< SocketAddress > | get_remote_address () const
| Try to get the remote address of a connected socket. More...
Protocol | get_protocol () const
| Gets the socket protocol id the socket was created with. More...
Type | get_socket_type () const
| Gets the socket type of the socket. More...
bool | speaks_ipv4 () const
| Checks if a socket is capable of speaking IPv4. More...
Glib::RefPtr< Credentials > | get_credentials ()
| Returns the credentials of the foreign process connected to this socket, if any (e.g. it is only supported for Gio::SocketFamily::UNIX sockets). More...
Glib::RefPtr< const Credentials > | get_credentials () const
| Returns the credentials of the foreign process connected to this socket, if any (e.g. it is only supported for Gio::SocketFamily::UNIX sockets). More...
guint | get_timeout () const
| Gets the timeout setting of the socket. More...
void | set_timeout (guint timeout)
| Sets the time in seconds after which I/O operations on socket will time out if they have not yet completed. More...
gssize | receive_with_blocking (gchar * buffer, gsize size, bool blocking, const Glib::RefPtr< Cancellable > & cancellable={})
| 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. More...
gssize | send_with_blocking (gchar * buffer, gsize size, bool blocking, const Glib::RefPtr< Cancellable > & cancellable={})
| 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. More...
bool | get_option (int level, int optname, int & value) const
| Gets the value of an integer-valued option on socket, as with getsockopt(). More...
bool | set_option (int level, int optname, int value)
| Sets the value of an integer-valued option on socket, as with setsockopt(). More...
guint | get_ttl () const
| Gets the unicast time-to-live setting on socket; see g_socket_set_ttl() for more details. More...
void | set_ttl (guint ttl)
| Sets the time-to-live for outgoing unicast packets on socket. More...
bool | get_broadcast () const
| Gets the broadcast setting on socket; if `true`, it is possible to send packets to broadcast addresses. More...
void | set_broadcast (bool broadcast)
| Sets whether socket should allow sending to broadcast addresses. More...
bool | get_multicast_loopback () const
| 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. More...
void | set_multicast_loopback (bool loopback)
| Sets whether outgoing multicast packets will be received by sockets listening on that multicast address on the same host. More...
guint | get_multicast_ttl () const
| Gets the multicast time-to-live setting on socket; see g_socket_set_multicast_ttl() for more details. More...
void | set_multicast_ttl (guint ttl)
| Sets the time-to-live for outgoing multicast datagrams on socket. More...
bool | join_multicast_group (const Glib::RefPtr< InetAddress > & group, bool source_specific, const std::string & iface)
| Registers socket to receive multicast messages sent to group. More...
bool | join_multicast_group (const Glib::RefPtr< InetAddress > & group, bool source_specific)
| A join_multicast_group() convenience overload. More...
bool | leave_multicast_group (const Glib::RefPtr< InetAddress > & group, bool source_specific, const std::string & iface)
| 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). More...
bool | leave_multicast_group (const Glib::RefPtr< InetAddress > & group, bool source_specific)
| A leave_multicast_group() convenience overload. More...
Glib::PropertyProxy< bool > | property_blocking ()
| Whether or not I/O on this socket is blocking. More...
Glib::PropertyProxy_ReadOnly< bool > | property_blocking () const
| Whether or not I/O on this socket is blocking. More...
Glib::PropertyProxy_ReadOnly< SocketFamily > | property_family () const
| The sockets address family. More...
Glib::PropertyProxy_ReadOnly< int > | property_fd () const
| The sockets file descriptor. More...
Glib::PropertyProxy< bool > | property_keepalive ()
| Keep connection alive by sending periodic pings. More...
Glib::PropertyProxy_ReadOnly< bool > | property_keepalive () const
| Keep connection alive by sending periodic pings. More...
Glib::PropertyProxy< int > | property_listen_backlog ()
| Outstanding connections in the listen queue. More...
Glib::PropertyProxy_ReadOnly< int > | property_listen_backlog () const
| Outstanding connections in the listen queue. More...
Glib::PropertyProxy_ReadOnly< Glib::RefPtr< SocketAddress > > | property_local_address () const
| The local address the socket is bound to. More...
Glib::PropertyProxy_ReadOnly< Glib::RefPtr< SocketAddress > > | property_remote_address () const
| The remote address the socket is connected to. More...
Glib::PropertyProxy< guint > | property_timeout ()
| The timeout in seconds on socket I/O. More...
Glib::PropertyProxy_ReadOnly< guint > | property_timeout () const
| The timeout in seconds on socket I/O. More...
Glib::PropertyProxy_ReadOnly< Protocol > | property_protocol () const
| The id of the protocol to use, or -1 for unknown. More...
Glib::PropertyProxy< bool > | property_broadcast ()
| Whether the socket should allow sending to broadcast addresses. More...
Glib::PropertyProxy_ReadOnly< bool > | property_broadcast () const
| Whether the socket should allow sending to broadcast addresses. More...
Glib::PropertyProxy_ReadOnly< Type > | property_type () const
| The sockets type. More...
Glib::PropertyProxy< guint > | property_ttl ()
| Time-to-live for outgoing unicast packets. More...
Glib::PropertyProxy_ReadOnly< guint > | property_ttl () const
| Time-to-live for outgoing unicast packets. More...
Glib::PropertyProxy< bool > | property_multicast_loopback ()
| Whether outgoing multicast packets loop back to the local host. More...
Glib::PropertyProxy_ReadOnly< bool > | property_multicast_loopback () const
| Whether outgoing multicast packets loop back to the local host. More...
Glib::PropertyProxy< guint > | property_multicast_ttl ()
| Time-to-live out outgoing multicast packets. More...
Glib::PropertyProxy_ReadOnly< guint > | property_multicast_ttl () const
| Time-to-live out outgoing multicast packets. More...
Public Member Functions inherited from Glib::Object
| Object (const Object &)=delete
Object & | operator= (const Object &)=delete
| Object (Object && src) noexcept
Object & | operator= (Object && src) noexcept
void * | get_data (const QueryQuark & key)
void | set_data (const Quark & key, void *data)
void | set_data_with_c_callback (const Quark & key, void *data, GDestroyNotify notify)
void | set_data (const Quark & key, void *data, DestroyNotify notify)
| Prefer set_data_with_c_callback() with a callback with C linkage. More...
void | remove_data (const QueryQuark & quark)
void * | steal_data (const QueryQuark & quark)
Public Member Functions inherited from Glib::ObjectBase
| ObjectBase (const ObjectBase &)=delete
ObjectBase & | operator= (const ObjectBase &)=delete
void | set_property_value (const Glib::ustring & property_name, const Glib::ValueBase & value)
| You probably want to use a specific property_*() accessor method instead. More...
void | get_property_value (const Glib::ustring & property_name, Glib::ValueBase & value) const
| You probably want to use a specific property_*() accessor method instead. More...
template<class PropertyType >
void | set_property (const Glib::ustring & property_name, const PropertyType & value)
| You probably want to use a specific property_*() accessor method instead. More...
template<class PropertyType >
void | get_property (const Glib::ustring & property_name, PropertyType & value) const
| You probably want to use a specific property_*() accessor method instead. More...
template<class PropertyType >
PropertyType | get_property (const Glib::ustring & property_name) const
| You probably want to use a specific property_*() accessor method instead. More...
sigc::connection | connect_property_changed (const Glib::ustring & property_name, const sigc::slot< void()> & slot)
| You can use the signal_changed() signal of the property proxy instead. More...
sigc::connection | connect_property_changed (const Glib::ustring & property_name, sigc::slot< void()> && slot)
| You can use the signal_changed() signal of the property proxy instead. More...
void | freeze_notify ()
| Increases the freeze count on object. More...
void | thaw_notify ()
| Reverts the effect of a previous call to freeze_notify(). More...
virtual void | reference () const
| Increment the reference count for this object. More...
virtual void | unreference () const
| Decrement the reference count for this object. More...
GObject * | gobj ()
| Provides access to the underlying C GObject. More...
const GObject * | gobj () const
| Provides access to the underlying C GObject. More...
GObject * | gobj_copy () const
| Give a ref-ed copy to someone. Use for direct struct access. More...
Public Member Functions inherited from sigc::trackable
| trackable () noexcept
| trackable (const trackable &src) noexcept
| trackable (trackable &&src) noexcept
| ~trackable ()
void | add_destroy_notify_callback (notifiable *data, func_destroy_notify func) const
void | notify_callbacks ()
trackable & | operator= (const trackable &src)
trackable & | operator= (trackable &&src) noexcept
void | remove_destroy_notify_callback (notifiable *data) const
Public Member Functions inherited from Gio::Initable
| Initable (Initable && src) noexcept
Initable & | operator= (Initable && src) noexcept
| ~Initable () noexcept override
GInitable * | gobj ()
| Provides access to the underlying C GObject. More...
const GInitable * | gobj () const
| Provides access to the underlying C GObject. More...
Public Member Functions inherited from Glib::Interface
| Interface ()
| A Default constructor. More...
| Interface (Interface && src) noexcept
Interface & | operator= (Interface && src) noexcept
| Interface (const Glib::Interface_Class & interface_class)
| Called by constructors of derived classes. More...
| Interface (GObject * castitem)
| Called by constructors of derived classes. More...
| ~Interface () noexcept override
| Interface (const Interface &)=delete
Interface & | operator= (const Interface &)=delete
GObject * | gobj ()
const GObject * | gobj () const

## Static Public Member Functions
static GType | get_type ()
| Get the GType for this class, for use with the underlying GObject type system. More...
static Glib::RefPtr< Socket > | create (SocketFamily family, Type type, Protocol protocol, const Glib::RefPtr< Cancellable > & cancellable={})
| Creates a new Socket with the defined family, type and protocol. More...
static Glib::RefPtr< Socket > | create_from_fd (int fd, const Glib::RefPtr< Cancellable > & cancellable={})
| Creates a new Socket from a native file descriptor or winsock SOCKET handle. More...
Static Public Member Functions inherited from Gio::Initable
static void | add_interface (GType gtype_implementer)
static GType | get_type ()
| Get the GType for this class, for use with the underlying GObject type system. More...

## Protected Member Functions
| Socket (SocketFamily family, Type type, Protocol protocol, const Glib::RefPtr< Cancellable > & cancellable)
| Socket (int fd, const Glib::RefPtr< Cancellable > & cancellable)
Protected Member Functions inherited from Glib::Object
| Object ()
| Object (const Glib::ConstructParams & construct_params)
| Object (GObject * castitem)
| ~Object () noexcept override
Protected Member Functions inherited from Glib::ObjectBase
| ObjectBase ()
| This default constructor is called implicitly from the constructor of user-derived classes, even if, for instance, Gtk::Button calls a different ObjectBase constructor. More...
| ObjectBase (const char * custom_type_name)
| A derived constructor always overrides this choice. More...
| ObjectBase (const std::type_info & custom_type_info)
| This constructor is a special feature to allow creation of derived types on the fly, without having to use g_object_new() manually. More...
| ObjectBase (ObjectBase && src) noexcept
ObjectBase & | operator= (ObjectBase && src) noexcept
virtual | ~ObjectBase () noexcept=0
void | initialize (GObject * castitem)
void | initialize_move (GObject * castitem, Glib::ObjectBase * previous_wrapper)
Protected Member Functions inherited from Gio::Initable
| Initable ()
| You should derive from this class to use it. More...
void | init (const Glib::RefPtr< Cancellable > & cancellable)
| Initializes the object implementing the interface. More...
void | init ()
| A init() convenience overload. More...
virtual bool | init_vfunc (const Glib::RefPtr< Cancellable > & cancellable, GError ** error)

## Related Functions
(Note that these are not member functions.)
Glib::RefPtr< Gio::Socket > | wrap (GSocket * object, bool take_copy=false)
| A Glib::wrap() method for this object. More...
Related Functions inherited from Glib::Object
Glib::RefPtr< Glib::Object > | wrap (GObject * object, bool take_copy=false)
Related Functions inherited from Gio::Initable
Glib::RefPtr< Gio::Initable > | wrap (GInitable * object, bool take_copy=false)
| A Glib::wrap() method for this object. More...

## Detailed Description

Low-level socket object.

A Socket 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.

Socket 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 SocketClient, SocketService and SocketConnection. However there may be cases where direct use of Socket is useful.

Socket implements the Initable interface, and since initialization can fail, the constructor may throw an exception.

Sockets operate in two general modes, blocking or non-blocking. When in blocking mode all operations 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 Gio::Error::WOULD_BLOCK error. To know when a call would successfully run you can call condition_check(), or condition_wait(). You can also use create_source() and attach it to a Glib::MainContext 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 Socket.

When working in non-blocking mode applications should always be able to handle getting a Gio::Error::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 Gio::Error::WOULD_BLOCK.

Sockets 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 Socket causes the signal SIGPIPE to be ignored for the remainder of the program. If you are writing a command-line utility that uses Socket, 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.

Since glibmm 2.24:

## Constructor & Destructor Documentation

## ◆ Socket() [1/3]

Gio::Socket::Socket | ( | Socket && | src | ) |
| noexcept

## ◆ ~Socket()

Gio::Socket::~Socket | ( |  | ) |
| overridenoexcept

## ◆ Socket() [2/3]

Gio::Socket::Socket | ( | SocketFamily | family,
|  | Type | type,
|  | Protocol | protocol,
|  | const Glib::RefPtr< Cancellable > & | cancellable
| ) |  |
| protected

## ◆ Socket() [3/3]

Gio::Socket::Socket | ( | int | fd,
|  | const Glib::RefPtr< Cancellable > & | cancellable
| ) |  |
| protected

## Member Function Documentation

## ◆ accept() [1/2]

Glib::RefPtr< Socket > Gio::Socket::accept | ( |  | ) |

A accept() convenience overload.

## ◆ accept() [2/2]

Glib::RefPtr< Socket > Gio::Socket::accept | ( | const Glib::RefPtr< Cancellable > & | cancellable | ) |

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 (Socket::listen()).

If there are no outstanding connections then the operation will block or throw Gio::Error with ERROR_WOULD_BLOCK if non-blocking I/O is enabled. To be notified of an incoming connection, wait for the Glib::IO_IN condition.

Parameters

  cancellable | A Cancellable object which can be used to cancel the operation.

Returns
  a Gio::Socket

Exceptions

  Gio::Error |

## ◆ bind()

void Gio::Socket::bind | ( | const Glib::RefPtr< SocketAddress > & | address,
|  | bool | allow_reuse
| ) |  |

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 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 Socket::accept() on), and `false` for client sockets. (Failing to set this flag on a server socket may cause Socket::bind() to throw Gio::Error with 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 | a SocketAddress specifying the local address.
  allow_reuse | whether to allow reusing this address

Exceptions

  Gio::Error |

## ◆ check_connect_result()

void Gio::Socket::check_connect_result | ( |  | ) |

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.

Since glibmm 2.22:

Exceptions

  Glib::Error |

## ◆ close()

void Gio::Socket::close | ( |  | ) |

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 Gio::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 Glib::IOCondition::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 TcpConnection 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.)

Since glibmm 2.22:

Exceptions

  Glib::Error |

## ◆ condition_check()

Glib::IOCondition Gio::Socket::condition_check | ( | Glib::IOCondition | condition | ) |

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 Gio::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 Gio::Error::WOULD_BLOCK.

It is meaningless to specify Glib::IOCondition::ERR or Glib::IOCondition::HUP in condition; these conditions will always be set in the output if they are true.

This call never blocks.

Since glibmm 2.22:

Parameters

  condition | A IOCondition mask to check.

Returns
  The GIOCondition mask of the current state.

## ◆ condition_timed_wait() [1/2]

void Gio::Socket::condition_timed_wait | ( | Glib::IOCondition | condition,
|  | gint64 | timeout
| ) |  |

A condition_timed_wait() convenience overload.

## ◆ condition_timed_wait() [2/2]

void Gio::Socket::condition_timed_wait | ( | Glib::IOCondition | condition,
|  | gint64 | timeout,
|  | const Glib::RefPtr< Cancellable > & | cancellable
| ) |  |

Waits for up to timeout 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 (or the socket's Socket::property_timeout()) is reached before the condition is met, then `false` is returned and error, if non-`nullptr`, is set to the appropriate value (Gio::Error::CANCELLED or Gio::Error::TIMED_OUT).

If you don't want a timeout, use g_socket_condition_wait(). (Alternatively, you can pass -1 for timeout.)

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

Since glibmm 2.32:

Parameters

  condition | A IOCondition mask to wait for.
  timeout | The maximum time (in microseconds) to wait, or -1.
  cancellable | A Cancellable, or `nullptr`.

Exceptions

  Glib::Error |

## ◆ condition_wait() [1/2]

void Gio::Socket::condition_wait | ( | Glib::IOCondition | condition | ) |

A condition_wait() convenience overload.

## ◆ condition_wait() [2/2]

void Gio::Socket::condition_wait | ( | Glib::IOCondition | condition,
|  | const Glib::RefPtr< Cancellable > & | cancellable
| ) |  |

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-`nullptr`, is set to the appropriate value (Gio::Error::CANCELLED or Gio::Error::TIMED_OUT).

See also g_socket_condition_timed_wait().

Since glibmm 2.22:

Parameters

  condition | A IOCondition mask to wait for.
  cancellable | A Cancellable, or `nullptr`.

Exceptions

  Glib::Error |

## ◆ connect() [1/2]

void Gio::Socket::connect | ( | const Glib::RefPtr< SocketAddress > & | address | ) |

A connect() convenience overload.

## ◆ connect() [2/2]

void Gio::Socket::connect | ( | const Glib::RefPtr< SocketAddress > & | address,
|  | const Glib::RefPtr< Cancellable > & | cancellable
| ) |  |

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 Gio::Error with ERROR_PENDING is thrown and the user can be notified of the connection finishing by waiting for the Glib::IO_OUT condition. The result of the connection must then be checked with Socket::check_connect_result().

Parameters

  address | a SocketAddress specifying the remote address.
  cancellable | A Cancellable object which can be used to cancel the operation.

Exceptions

  Gio::Error |

## ◆ create()

static Glib::RefPtr< Socket > Gio::Socket::create | ( | SocketFamily | family,
|  | Type | type,
|  | Protocol | protocol,
|  | const Glib::RefPtr< Cancellable > & | cancellable = `{}`
| ) |  |
| static

Creates a new Socket with the defined family, type and protocol.

If protocol is 0 (Gio::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. Gio::Socket::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 Gio::Socket::Protocol if you know the protocol number used for it.

Since glibmm 2.22:

Parameters

  family | The socket family to use, e.g. Gio::SocketFamily::IPV4.
  type | The socket type to use.
  protocol | The id of the protocol to use, or 0 for default.

Returns
  A Socket or `nullptr` on error. Free the returned object with Glib::object_unref().

Parameters

  cancellable | A Cancellable object which can be used to cancel the operation.

Exceptions

  Glib::Error |

## ◆ create_from_fd()

static Glib::RefPtr< Socket > Gio::Socket::create_from_fd | ( | int | fd,
|  | const Glib::RefPtr< Cancellable > & | cancellable = `{}`
| ) |  |
| static

Creates a new Socket 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 Socket.

On success, the returned Socket 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 Gio::Error::FAILED

Since glibmm 2.22:

Parameters

  fd | A native socket file descriptor.

Returns
  A Socket or `nullptr` on error. Free the returned object with Glib::object_unref().

Parameters

  cancellable | A Cancellable object which can be used to cancel the operation.

Exceptions

  Glib::Error |

## ◆ create_source()

Glib::RefPtr< SocketSource > Gio::Socket::create_source | ( | Glib::IOCondition | condition,
|  | const Glib::RefPtr< Cancellable > & | cancellable = `{}`
| ) |  |

Creates a SocketSource that can be attached to a Glib::MainContext to monitor for the availability of the specified condition on the socket.

Create a slot from a function to be called when condition is met for the socket with sigc::ptr_fun() or sigc::mem_fun() and pass it into the connect() function of the returned SocketSource object. Polling of the socket will start when you attach a Glib::MainContext object to the returned SocketSource object using its attach() function.

It is meaningless to specify Glib::IO_ERR or Glib::IO_HUP in condition; these conditions will always be reported output if they are true.

cancellable 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 Cancellable::is_cancelled().

If the socket has a timeout set, and it is reached before condition occurs, the source will then trigger anyway, reporting Glib::IO_IN or Glib::IO_OUT depending on condition. However, the socket will have been marked as having had a timeout, and so the next Socket I/O method you call will then fail with a Gio::IO_ERROR_TIMED_OUT.

Gio::signal_socket().connect() is a simpler interface to the same functionality.

Since glibmm 2.42:

Parameters

  condition | A Glib::IOCondition mask to monitor.
  cancellable | A Cancellable. The default value means the source is not cancellable.

Returns
  A newly allocated SocketSource.

## ◆ get_available_bytes()

gssize Gio::Socket::get_available_bytes | ( |  | ) | const

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.

Since glibmm 2.32:

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

## ◆ get_blocking()

bool Gio::Socket::get_blocking | ( |  | ) | const

Gets the blocking mode of the socket.

For details on blocking I/O, see g_socket_set_blocking().

Since glibmm 2.22:

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

## ◆ get_broadcast()

bool Gio::Socket::get_broadcast | ( |  | ) | const

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

Since glibmm 2.32:

Returns
  The broadcast setting on socket.

## ◆ get_credentials() [1/2]

Glib::RefPtr< Credentials > Gio::Socket::get_credentials | ( |  | ) |

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

If this operation isn't supported on the OS, the method fails with the Gio::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
- macOS, tvOS, iOS since GLib 2.66

Other ways to obtain credentials from a foreign peer includes the UnixCredentialsMessage type and g_unix_connection_send_credentials() / g_unix_connection_receive_credentials() functions.

Since glibmm 2.26:

Returns
  `nullptr` if error is set, otherwise a Credentials object that must be freed with Glib::object_unref().

Exceptions

  Glib::Error |

## ◆ get_credentials() [2/2]

Glib::RefPtr< const Credentials > Gio::Socket::get_credentials | ( |  | ) | const

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

If this operation isn't supported on the OS, the method fails with the Gio::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
- macOS, tvOS, iOS since GLib 2.66

Other ways to obtain credentials from a foreign peer includes the UnixCredentialsMessage type and g_unix_connection_send_credentials() / g_unix_connection_receive_credentials() functions.

Since glibmm 2.26:

Returns
  `nullptr` if error is set, otherwise a Credentials object that must be freed with Glib::object_unref().

Exceptions

  Glib::Error |

## ◆ get_family()

SocketFamily Gio::Socket::get_family | ( |  | ) | const

Gets the socket family of the socket.

Since glibmm 2.22:

Returns
  A SocketFamily.

## ◆ get_fd()

int Gio::Socket::get_fd | ( |  | ) | const

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.

Since glibmm 2.22:

Returns
  The file descriptor of the socket.

## ◆ get_keepalive()

bool Gio::Socket::get_keepalive | ( |  | ) | const

Gets the keepalive mode of the socket.

For details on this, see g_socket_set_keepalive().

Since glibmm 2.22:

Returns
  `true` if keepalive is active, `false` otherwise.

## ◆ get_listen_backlog()

int Gio::Socket::get_listen_backlog | ( |  | ) | const

Gets the listen backlog setting of the socket.

For details on this, see g_socket_set_listen_backlog().

Since glibmm 2.22:

Returns
  The maximum number of pending connections.

## ◆ get_local_address()

Glib::RefPtr< SocketAddress > Gio::Socket::get_local_address | ( |  | ) | const

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.

Since glibmm 2.22:

Returns
  A SocketAddress or `nullptr` on error. Free the returned object with Glib::object_unref().

Exceptions

  Glib::Error |

## ◆ get_multicast_loopback()

bool Gio::Socket::get_multicast_loopback | ( |  | ) | const

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.

Since glibmm 2.32:

Returns
  The multicast loopback setting on socket.

## ◆ get_multicast_ttl()

guint Gio::Socket::get_multicast_ttl | ( |  | ) | const

Gets the multicast time-to-live setting on socket; see g_socket_set_multicast_ttl() for more details.

Since glibmm 2.32:

Returns
  The multicast time-to-live setting on socket.

## ◆ get_option()

bool Gio::Socket::get_option | ( | int | level,
|  | int | optname,
|  | int & | value
| ) |  | const

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>][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 `int` variable, not a #guchar; g_socket_get_option() will handle the conversion internally.

Since glibmm 2.36:

Parameters

  level | The "API level" of the option (eg, `SOL_SOCKET`).
  optname | The "name" of the option (eg, `SO_BROADCAST`).
  value | Return location for the option value.

Returns
  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.

Exceptions

  Glib::Error |

## ◆ get_protocol()

Protocol Gio::Socket::get_protocol | ( |  | ) | const

Gets the socket protocol id the socket was created with.

In case the protocol is unknown, -1 is returned.

Since glibmm 2.22:

Returns
  A protocol id, or -1 if unknown.

## ◆ get_remote_address()

Glib::RefPtr< SocketAddress > Gio::Socket::get_remote_address | ( |  | ) | const

Try to get the remote address of a connected socket.

This is only useful for connection oriented sockets that have been connected.

Since glibmm 2.22:

Returns
  A SocketAddress or `nullptr` on error. Free the returned object with Glib::object_unref().

Exceptions

  Glib::Error |

## ◆ get_socket_type()

Type Gio::Socket::get_socket_type | ( |  | ) | const

Gets the socket type of the socket.

Since glibmm 2.22:

Returns
  A Gio::Socket::Type.

## ◆ get_timeout()

guint Gio::Socket::get_timeout | ( |  | ) | const

Gets the timeout setting of the socket.

For details on this, see g_socket_set_timeout().

Since glibmm 2.26:

Returns
  The timeout in seconds.

## ◆ get_ttl()

guint Gio::Socket::get_ttl | ( |  | ) | const

Gets the unicast time-to-live setting on socket; see g_socket_set_ttl() for more details.

Since glibmm 2.32:

Returns
  The time-to-live setting on socket.

## ◆ get_type()

static GType Gio::Socket::get_type | ( |  | ) |
| static

Get the GType for this class, for use with the underlying GObject type system.

## ◆ gobj() [1/2]

GSocket * Gio::Socket::gobj | ( |  | ) |
| inline

Provides access to the underlying C GObject.

## ◆ gobj() [2/2]

const GSocket * Gio::Socket::gobj | ( |  | ) | const
| inline

Provides access to the underlying C GObject.

## ◆ gobj_copy()

GSocket * Gio::Socket::gobj_copy | ( |  | ) |

Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.

## ◆ is_closed()

bool Gio::Socket::is_closed | ( |  | ) |

Checks whether a socket is closed.

Since glibmm 2.22:

Returns
  `true` if socket is closed, `false` otherwise.

## ◆ is_connected()

bool Gio::Socket::is_connected | ( |  | ) |

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().

Since glibmm 2.22:

Returns
  `true` if socket is connected, `false` otherwise.

## ◆ join_multicast_group() [1/2]

bool Gio::Socket::join_multicast_group | ( | const Glib::RefPtr< InetAddress > & | group,
|  | bool | source_specific
| ) |  |

A join_multicast_group() convenience overload.

## ◆ join_multicast_group() [2/2]

bool Gio::Socket::join_multicast_group | ( | const Glib::RefPtr< InetAddress > & | group,
|  | bool | source_specific,
|  | const std::string & | iface
| ) |  |

Registers socket to receive multicast messages sent to group.

socket must be a Gio::Socket::Type::DATAGRAM socket, and must have been bound to an appropriate interface and port with g_socket_bind().

If iface is `nullptr`, 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 Gio::Error::NOT_SUPPORTED error.

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

Since glibmm 2.32:

Parameters

  group | A InetAddress specifying the group address to join.
  iface | Name of the interface to use, or `nullptr`.
  source_specific | `true` if source-specific multicast should be used.

Returns
  `true` on success, `false` on error.

Exceptions

  Glib::Error |

## ◆ leave_multicast_group() [1/2]

bool Gio::Socket::leave_multicast_group | ( | const Glib::RefPtr< InetAddress > & | group,
|  | bool | source_specific
| ) |  |

A leave_multicast_group() convenience overload.

## ◆ leave_multicast_group() [2/2]

bool Gio::Socket::leave_multicast_group | ( | const Glib::RefPtr< InetAddress > & | group,
|  | bool | source_specific,
|  | const std::string & | iface
| ) |  |

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.

Since glibmm 2.32:

Parameters

  group | A InetAddress specifying the group address to leave.
  iface | Interface used.
  source_specific | `true` if source-specific multicast was used.

Returns
  `true` on success, `false` on error.

Exceptions

  Glib::Error |

## ◆ listen()

void Gio::Socket::listen | ( |  | ) |

Marks the socket as a server socket - 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().

Exceptions

  Gio::Error |

## ◆ operator=()

Socket & Gio::Socket::operator= | ( | Socket && | src | ) |
| noexcept

## ◆ property_blocking() [1/2]

Glib::PropertyProxy< bool > Gio::Socket::property_blocking | ( |  | ) |

Whether or not I/O on this socket is blocking.

Default value: `true`

Returns
  A PropertyProxy that allows you to get or set the value of the property, or receive notification when the value of the property changes.

## ◆ property_blocking() [2/2]

Glib::PropertyProxy_ReadOnly< bool > Gio::Socket::property_blocking | ( |  | ) | const

Whether or not I/O on this socket is blocking.

Default value: `true`

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_broadcast() [1/2]

Glib::PropertyProxy< bool > Gio::Socket::property_broadcast | ( |  | ) |

Whether the socket should allow sending to broadcast addresses.

Since glibmm 2.32:

Default value: `false`

Returns
  A PropertyProxy that allows you to get or set the value of the property, or receive notification when the value of the property changes.

## ◆ property_broadcast() [2/2]

Glib::PropertyProxy_ReadOnly< bool > Gio::Socket::property_broadcast | ( |  | ) | const

Whether the socket should allow sending to broadcast addresses.

Since glibmm 2.32:

Default value: `false`

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_family()

Glib::PropertyProxy_ReadOnly< SocketFamily > Gio::Socket::property_family | ( |  | ) | const

The sockets address family.

Default value: Gio::SocketFamily::INVALID

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_fd()

Glib::PropertyProxy_ReadOnly< int > Gio::Socket::property_fd | ( |  | ) | const

The sockets file descriptor.

Default value: -1

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_keepalive() [1/2]

Glib::PropertyProxy< bool > Gio::Socket::property_keepalive | ( |  | ) |

Keep connection alive by sending periodic pings.

Default value: `false`

Returns
  A PropertyProxy that allows you to get or set the value of the property, or receive notification when the value of the property changes.

## ◆ property_keepalive() [2/2]

Glib::PropertyProxy_ReadOnly< bool > Gio::Socket::property_keepalive | ( |  | ) | const

Keep connection alive by sending periodic pings.

Default value: `false`

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_listen_backlog() [1/2]

Glib::PropertyProxy< int > Gio::Socket::property_listen_backlog | ( |  | ) |

Outstanding connections in the listen queue.

Default value: 10

Returns
  A PropertyProxy that allows you to get or set the value of the property, or receive notification when the value of the property changes.

## ◆ property_listen_backlog() [2/2]

Glib::PropertyProxy_ReadOnly< int > Gio::Socket::property_listen_backlog | ( |  | ) | const

Outstanding connections in the listen queue.

Default value: 10

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_local_address()

Glib::PropertyProxy_ReadOnly< Glib::RefPtr< SocketAddress > > Gio::Socket::property_local_address | ( |  | ) | const

The local address the socket is bound to.

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_multicast_loopback() [1/2]

Glib::PropertyProxy< bool > Gio::Socket::property_multicast_loopback | ( |  | ) |

Whether outgoing multicast packets loop back to the local host.

Since glibmm 2.32:

Default value: `true`

Returns
  A PropertyProxy that allows you to get or set the value of the property, or receive notification when the value of the property changes.

## ◆ property_multicast_loopback() [2/2]

Glib::PropertyProxy_ReadOnly< bool > Gio::Socket::property_multicast_loopback | ( |  | ) | const

Whether outgoing multicast packets loop back to the local host.

Since glibmm 2.32:

Default value: `true`

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_multicast_ttl() [1/2]

Glib::PropertyProxy< guint > Gio::Socket::property_multicast_ttl | ( |  | ) |

Time-to-live out outgoing multicast packets.

Since glibmm 2.32:

Default value: 1

Returns
  A PropertyProxy that allows you to get or set the value of the property, or receive notification when the value of the property changes.

## ◆ property_multicast_ttl() [2/2]

Glib::PropertyProxy_ReadOnly< guint > Gio::Socket::property_multicast_ttl | ( |  | ) | const

Time-to-live out outgoing multicast packets.

Since glibmm 2.32:

Default value: 1

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_protocol()

Glib::PropertyProxy_ReadOnly< Protocol > Gio::Socket::property_protocol | ( |  | ) | const

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

Default value: Gio::Socket::Protocol::UNKNOWN

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_remote_address()

Glib::PropertyProxy_ReadOnly< Glib::RefPtr< SocketAddress > > Gio::Socket::property_remote_address | ( |  | ) | const

The remote address the socket is connected to.

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_timeout() [1/2]

Glib::PropertyProxy< guint > Gio::Socket::property_timeout | ( |  | ) |

The timeout in seconds on socket I/O.

Since glibmm 2.26:

Default value: 0

Returns
  A PropertyProxy that allows you to get or set the value of the property, or receive notification when the value of the property changes.

## ◆ property_timeout() [2/2]

Glib::PropertyProxy_ReadOnly< guint > Gio::Socket::property_timeout | ( |  | ) | const

The timeout in seconds on socket I/O.

Since glibmm 2.26:

Default value: 0

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_ttl() [1/2]

Glib::PropertyProxy< guint > Gio::Socket::property_ttl | ( |  | ) |

Time-to-live for outgoing unicast packets.

Since glibmm 2.32:

Default value: 0

Returns
  A PropertyProxy that allows you to get or set the value of the property, or receive notification when the value of the property changes.

## ◆ property_ttl() [2/2]

Glib::PropertyProxy_ReadOnly< guint > Gio::Socket::property_ttl | ( |  | ) | const

Time-to-live for outgoing unicast packets.

Since glibmm 2.32:

Default value: 0

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ property_type()

Glib::PropertyProxy_ReadOnly< Type > Gio::Socket::property_type | ( |  | ) | const

The sockets type.

Default value: Gio::Socket::Type::STREAM

Returns
  A PropertyProxy_ReadOnly that allows you to get the value of the property, or receive notification when the value of the property changes.

## ◆ receive() [1/2]

gssize Gio::Socket::receive | ( | char * | buffer,
|  | gsize | size
| ) |  |

A receive() convenience overload.

## ◆ receive() [2/2]

gssize Gio::Socket::receive | ( | char * | buffer,
|  | gsize | size,
|  | const Glib::RefPtr< Cancellable > & | cancellable
| ) |  |

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 `nullptr`.

For Gio::Socket::Type::DATAGRAM and Gio::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 Gio::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 Gio::Error::WOULD_BLOCK error will be returned. To be notified when data is available, wait for the Glib::IOCondition::IN condition.

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

Since glibmm 2.22:

Parameters

  buffer | A buffer to read data into (which should be at least size bytes long).
  size | The number of bytes you want to read from the socket.
  cancellable | A Cancellable or `nullptr`.

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

Exceptions

  Glib::Error |

## ◆ receive_from() [1/2]

gssize Gio::Socket::receive_from | ( | Glib::RefPtr< SocketAddress > & | address,
|  | char * | buffer,
|  | gsize | size
| ) |  |

## ◆ receive_from() [2/2]

gssize Gio::Socket::receive_from | ( | Glib::RefPtr< SocketAddress > & | address,
|  | char * | buffer,
|  | gsize | size,
|  | const Glib::RefPtr< Cancellable > & | cancellable
| ) |  |

## ◆ receive_with_blocking()

gssize Gio::Socket::receive_with_blocking | ( | gchar * | buffer,
|  | gsize | size,
|  | bool | blocking,
|  | const Glib::RefPtr< Cancellable > & | cancellable = `{}`
| ) |  |

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.

Since glibmm 2.26:

Parameters

  buffer | A buffer to read data into (which should be at least size bytes long).
  size | The number of bytes you want to read from the socket.
  blocking | Whether to do blocking or non-blocking I/O.
  cancellable | A Cancellable or `nullptr`.

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

## ◆ send() [1/2]

gssize Gio::Socket::send | ( | const gchar * | buffer,
|  | gsize | size
| ) |  |

A send() convenience overload.

## ◆ send() [2/2]

gssize Gio::Socket::send | ( | const gchar * | buffer,
|  | gsize | size,
|  | const Glib::RefPtr< Cancellable > & | cancellable
| ) |  |

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 `nullptr`.

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 Gio::Error::WOULD_BLOCK error will be returned. To be notified when space is available, wait for the Glib::IOCondition::OUT condition. Note though that you may still receive Gio::Error::WOULD_BLOCK from g_socket_send() even if you were previously notified of a Glib::IOCondition::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.

Since glibmm 2.22:

Parameters

  buffer | The buffer containing the data to send.
  size | The number of bytes to send.
  cancellable | A Cancellable or `nullptr`.

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

Exceptions

  Glib::Error |

## ◆ send_to() [1/2]

gssize Gio::Socket::send_to | ( | const Glib::RefPtr< SocketAddress > & | address,
|  | const char * | buffer,
|  | gsize | size
| ) |  |

A send_to() convenience overload.

## ◆ send_to() [2/2]

gssize Gio::Socket::send_to | ( | const Glib::RefPtr< SocketAddress > & | address,
|  | const char * | buffer,
|  | gsize | size,
|  | const Glib::RefPtr< Cancellable > & | cancellable
| ) |  |

Tries to send size bytes from buffer to address.

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

See g_socket_send() for additional information.

Since glibmm 2.22:

Parameters

  address | A SocketAddress, or `nullptr`.
  buffer | The buffer containing the data to send.
  size | The number of bytes to send.
  cancellable | A Cancellable or `nullptr`.

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

Exceptions

  Glib::Error |

## ◆ send_with_blocking()

gssize Gio::Socket::send_with_blocking | ( | gchar * | buffer,
|  | gsize | size,
|  | bool | blocking,
|  | const Glib::RefPtr< Cancellable > & | cancellable = `{}`
| ) |  |

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.

Since glibmm 2.26:

Parameters

  buffer | The buffer containing the data to send.
  size | The number of bytes to send.
  blocking | Whether to do blocking or non-blocking I/O.
  cancellable | A Cancellable or `nullptr`.

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

## ◆ set_blocking()

void Gio::Socket::set_blocking | ( | bool | 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 Gio::Error::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.

Since glibmm 2.22:

Parameters

  blocking | Whether to use blocking I/O or not.

## ◆ set_broadcast()

void Gio::Socket::set_broadcast | ( | bool | broadcast | ) |

Sets whether socket should allow sending to broadcast addresses.

This is `false` by default.

Since glibmm 2.32:

Parameters

  broadcast | Whether socket should allow sending to broadcast addresses.

## ◆ set_keepalive()

void Gio::Socket::set_keepalive | ( | bool | 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, Gio::Socket::Protocol::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.

Since glibmm 2.22:

Parameters

  keepalive | Value for the keepalive flag.

## ◆ set_listen_backlog()

void Gio::Socket::set_listen_backlog | ( | int | 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 g_socket_listen() and has no effect if called after that.

Since glibmm 2.22:

Parameters

  backlog | The maximum number of pending connections.

## ◆ set_multicast_loopback()

void Gio::Socket::set_multicast_loopback | ( | bool | loopback | ) |

Sets whether outgoing multicast packets will be received by sockets listening on that multicast address on the same host.

This is `true` by default.

Since glibmm 2.32:

Parameters

  loopback | Whether socket should receive messages sent to its multicast groups from the local host.

## ◆ set_multicast_ttl()

void Gio::Socket::set_multicast_ttl | ( | guint | ttl | ) |

Sets the time-to-live for outgoing multicast datagrams on socket.

By default, this is 1, meaning that multicast packets will not leave the local network.

Since glibmm 2.32:

Parameters

  ttl | The time-to-live value for all multicast datagrams on socket.

## ◆ set_option()

bool Gio::Socket::set_option | ( | int | level,
|  | int | optname,
|  | int | value
| ) |  |

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>][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.

Since glibmm 2.36:

Parameters

  level | The "API level" of the option (eg, `SOL_SOCKET`).
  optname | The "name" of the option (eg, `SO_BROADCAST`).
  value | The value to set the option to.

Returns
  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.

Exceptions

  Glib::Error |

## ◆ set_timeout()

void Gio::Socket::set_timeout | ( | guint | timeout | ) |

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

On a blocking socket, this means that any blocking Socket operation will time out after timeout seconds of inactivity, returning Gio::Error::TIMED_OUT.

On a non-blocking socket, calls to g_socket_condition_wait() will also fail with Gio::Error::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 g_socket_receive(), g_socket_send(), g_socket_check_connect_result(), etc, will fail with Gio::Error::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.

Since glibmm 2.26:

Parameters

  timeout | The timeout for socket, in seconds, or 0 for none.

## ◆ set_ttl()

void Gio::Socket::set_ttl | ( | guint | ttl | ) |

Sets the time-to-live for outgoing unicast packets on socket.

By default the platform-specific default value is used.

Since glibmm 2.32:

Parameters

  ttl | The time-to-live value for all unicast packets on socket.

## ◆ shutdown()

void Gio::Socket::shutdown | ( | bool | shutdown_read,
|  | bool | shutdown_write
| ) |  |

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.

Since glibmm 2.22:

Parameters

  shutdown_read | Whether to shut down the read side.
  shutdown_write | Whether to shut down the write side.

Exceptions

  Glib::Error |

## ◆ speaks_ipv4()

bool Gio::Socket::speaks_ipv4 | ( |  | ) | const

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.

Since glibmm 2.22:

Returns
  `true` if this socket can be used with IPv4.

## Friends And Related Function Documentation

## ◆ wrap()

Glib::RefPtr< Gio::Socket > wrap | ( | GSocket * | object,
|  | bool | take_copy = `false`
| ) |  |
| related

A Glib::wrap() method for this object.

Parameters

  object | The C instance.
  take_copy | False if the result should take ownership of the C instance. True if it should take a new copy or ref.

Returns
  A C++ instance that wraps this C instance.