glibmm: Gio::Socket Class Reference

# Gio::Socket Class Reference
[Portable Network I/O Functionality]

Low-level socket object. More...

`#include <giomm/socket.h>`

Inheritance diagram for Gio::Socket:
[legend]

List of all members.

## Public Member Functions
virtual | ~Socket ()
GSocket* | gobj ()
| Provides access to the underlying C GObject.
const GSocket* | gobj () const
| Provides access to the underlying C GObject.
GSocket* | gobj_copy ()
| Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.
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.
void | listen ()
| Marks the socket as a server socket, i.e. a socket that is used to accept incoming requests using g_socket_accept().
Glib::RefPtr< Socket > | accept (const Glib::RefPtr< Cancellable >& cancellable)
| Accept incoming connections on a connection-based socket.
Glib::RefPtr< Socket > | accept ()
void | connect (const Glib::RefPtr< SocketAddress >& address, const Glib::RefPtr< Cancellable >& cancellable)
| Connect the socket to the specified remote address.
void | connect (const Glib::RefPtr< SocketAddress >& address)
void | check_connect_result ()
| Checks and resets the pending connect error for the socket.
gssize | receive (char* buffer, gsize size, const Glib::RefPtr< Cancellable >& cancellable)
| Receive data (up to size bytes) from a socket.
gssize | receive (char* buffer, gsize size)
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.
gssize | send (const gchar* buffer, gsize size)
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.
gssize | send_to (const Glib::RefPtr< SocketAddress >& address, const char* buffer, gsize size)
void | close ()
| Closes the socket, shutting down any active connection.
bool | is_closed ()
| Checks whether a socket is closed.
void | shutdown (bool shutdown_read, bool shutdown_write)
| Shut down part of a full-duplex connection.
bool | is_connected ()
| Check whether the socket is connected.
Glib::IOCondition | condition_check (Glib::IOCondition condition)
| Checks on the readiness of socket to perform operations.
void | condition_wait (Glib::IOCondition condition, const Glib::RefPtr< Cancellable >& cancellable)
| Waits for condition to become true on socket.
void | condition_wait (Glib::IOCondition condition)
void | set_listen_backlog (int backlog)
| Sets the maximum number of outstanding connections allowed when listening on this socket.
int | get_listen_backlog () const
| Gets the listen backlog setting of the socket.
void | set_blocking (bool blocking)
| Sets the blocking mode of the socket.
bool | get_blocking () const
| Gets the blocking mode of the socket.
void | set_keepalive (bool keepalive)
| Sets or unsets the So::KEEPALIVE flag on the underlying socket.
bool | get_keepalive () const
| Gets the keepalive mode of the socket.
SocketFamily | get_family () const
| Gets the socket family of the socket.
int | get_fd () const
| Returns the underlying OS socket object.
Glib::RefPtr< SocketAddress > | get_local_address () const
| Try to get the local address of a bound socket.
Glib::RefPtr< SocketAddress > | get_remote_address () const
| Try to get the remove address of a connected socket.
SocketProtocol | get_protocol () const
| Gets the socket protocol id the socket was created with.
SocketType | get_socket_type () const
| Gets the socket type of the socket.
bool | speaks_ipv4 () const
| Checks if a socket is capable of speaking IPv4.
Glib::PropertyProxy< bool > | property_blocking ()
| Whether or not I/O on this socket is blocking.
Glib::PropertyProxy_ReadOnly < bool > | property_blocking () const
| Whether or not I/O on this socket is blocking.
Glib::PropertyProxy_ReadOnly < SocketFamily > | property_family () const
| The sockets address family.
Glib::PropertyProxy_ReadOnly< int > | property_fd () const
| The sockets file descriptor.
Glib::PropertyProxy< bool > | property_keepalive ()
| Keep connection alive by sending periodic pings.
Glib::PropertyProxy_ReadOnly < bool > | property_keepalive () const
| Keep connection alive by sending periodic pings.
Glib::PropertyProxy< int > | property_listen_backlog ()
| Outstanding connections in the listen queue.
Glib::PropertyProxy_ReadOnly< int > | property_listen_backlog () const
| Outstanding connections in the listen queue.
Glib::PropertyProxy_ReadOnly < Glib::RefPtr< SocketAddress > > | property_local_address () const
| The local address the socket is bound to.
Glib::PropertyProxy_ReadOnly < Glib::RefPtr< SocketAddress > > | property_remote_address () const
| The remote address the socket is connected to.
Glib::PropertyProxy_ReadOnly < SocketProtocol > | property_protocol () const
| The id of the protocol to use.
Glib::PropertyProxy_ReadOnly < SocketType > | property_type () const
| The sockets type.
## Static Public Member Functions
static Glib::RefPtr< Socket > | create (SocketFamily family, SocketType type, SocketProtocol protocol, const Glib::RefPtr< Cancellable >& cancellable=Glib::RefPtr< Cancellable >())
| Creates a new Socket with the defined family, type and protocol.
static Glib::RefPtr< Socket > | create_from_fd (int fd, const Glib::RefPtr< Cancellable >& cancellable=Glib::RefPtr< Cancellable >())
| Creates a new Socket from a native file descriptor or winsock SOCKET handle.
## Protected Member Functions
| Socket (SocketFamily family, SocketType type, SocketProtocol protocol, const Glib::RefPtr< Cancellable >& cancellable)
| Socket (int fd, const Glib::RefPtr< Cancellable >& cancellable)
## 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.

## 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

virtual Gio::Socket::~Socket | ( |  | ) | `[virtual]`

Gio::Socket::Socket | ( | SocketFamily | family,
|  | SocketType | type,
|  | SocketProtocol | protocol,
|  | const Glib::RefPtr< Cancellable >& | cancellable |
| ) |  |  | `[protected]`

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

## Member Function Documentation

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

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 Socket 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 IO_ERROR_WOULD_BLOCK if non-blocking I/O is enabled. To be notified of an incoming connection, wait for the IO_IN condition.

Since glibmm 2.22:

Parameters:

  | cancellable | A Cancellable or `0`.

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

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.

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.

allow_reuse should be `true` for server sockets (sockets that you will eventually call g_socket_accept() on), and `false` for client sockets. (Specifically, if it is `true`, then g_socket_bind() will set the So::REUSEADDR flag on the socket, allowing it to bind address even if that address was previously used by another socket that has not yet been fully cleaned-up by the kernel. Failing to set this flag on a server socket may cause the bind call to return IO_ERROR_ADDRESS_IN_USE if the server program is stopped and then immediately restarted.)

Since glibmm 2.22:

Parameters:

  | address | A SocketAddress specifying the local address.
  | allow_reuse | Whether to allow reusing this address.

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

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:

Returns:
  `true` if no error, `false` otherwise, setting error to the error.

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 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 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 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:

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

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.

It is meaningless to specify IO_ERR or IO_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.

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

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 then `false` is returned and error, if non-`0`, is set to IO_ERROR_CANCELLED.

Since glibmm 2.22:

Parameters:

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

Returns:
  `true` if the condition was met, `false` otherwise.

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

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 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 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 can then be checked with g_socket_check_connect_result().

Since glibmm 2.22:

Parameters:

  | address | A SocketAddress specifying the remote address.
  | cancellable | A Cancellable or `0`.

Returns:
  `true` if connected, `false` on error.

static Glib::RefPtr<Socket> Gio::Socket::create | ( | SocketFamily | family,
|  | SocketType | type,
|  | SocketProtocol | protocol,
|  | const Glib::RefPtr< Cancellable >& | cancellable = `Glib::RefPtr< Cancellable >()` |
| ) |  |  | `[static]`

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

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

Since glibmm 2.22:

Parameters:

  | family | The socket family to use, e.g. SOCKET_FAMILY_IPV4.
  | type | The socket type to use.
  | protocol | The id of the protocol to use, or 0 for default.

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

Exceptions:

  | Glib::Error |

static Glib::RefPtr<Socket> Gio::Socket::create_from_fd | ( | int | fd,
|  | const Glib::RefPtr< Cancellable >& | cancellable = `Glib::RefPtr< 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.

Since glibmm 2.22:

Parameters:

  | fd | A native socket file descriptor.

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

Exceptions:

  | Glib::Error |

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.

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

Gets the socket family of the socket.

Since glibmm 2.22:

Returns:
  A SocketFamily.

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.

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.

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.

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 `0` on error. Free the returned object with Glib::object_unref().

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

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

Try to get the remove 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 `0` on error. Free the returned object with Glib::object_unref().

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

Gets the socket type of the socket.

Since glibmm 2.22:

Returns:
  A SocketType.

const GSocket* Gio::Socket::gobj | ( |  | ) | const`[inline]`

Provides access to the underlying C GObject.

Reimplemented from Gio::Initable.

GSocket* Gio::Socket::gobj | ( |  | ) | `[inline]`

Provides access to the underlying C GObject.

Reimplemented from Gio::Initable.

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.

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

Checks whether a socket is closed.

Since glibmm 2.22:

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

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

Check whether the socket is connected.

This is only useful for connection-oriented sockets.

Since glibmm 2.22:

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

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

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

Since glibmm 2.22:

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

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

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

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

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

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

The sockets address family.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

The sockets file descriptor.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

Keep connection alive by sending periodic pings.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

Keep connection alive by sending periodic pings.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

Outstanding connections in the listen queue.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

Outstanding connections in the listen queue.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

The local address the socket is bound to.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

The id of the protocol to use.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

The remote address the socket is connected to.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

The sockets type.

You rarely need to use properties because there are get_ and set_ methods for almost all of them.

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

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

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

For SOCKET_TYPE_DATAGRAM and 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 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 or there is an error. If there is no data available and the socket is in non-blocking mode, a IO_ERROR_WOULD_BLOCK error will be returned. To be notified when data is available, wait for the IO_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 `0`.

Returns:
  Number of bytes read, or -1 on error.

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

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

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

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

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

Since glibmm 2.22:

Parameters:

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

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

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

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 `0` 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 `0`.
  | buffer | The buffer containing the data to send.
  | size | The number of bytes to send.
  | cancellable | A Cancellable or `0`.

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

void Gio::Socket::set_blocking | ( | bool | blocking | ) |

Sets the blocking mode of the socket.

In blocking mode all operations block until they succeed or there is an error. In non-blocking mode all functions return results immediately or with a IO_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.

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

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.

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

Shut down part of a full-duplex connection.

If shutdown_read is `true` then the recieving 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 this is used 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.

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

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

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.