![]() |
![]() |
![]() |
GIO Reference Manual | ![]() |
---|---|---|---|---|
Top | Description | Object Hierarchy | Implemented Interfaces | Properties |
#include <gio/gio.h> GSocket; gboolean (*GSocketSourceFunc) (GSocket *socket, GIOCondition condition, gpointer user_data); enum GSocketType; enum GSocketProtocol; enum GSocketMsgFlags; GInputVector; GOutputVector; GSocket * g_socket_new (GSocketFamily family, GSocketType type, GSocketProtocol protocol, GError **error); GSocket * g_socket_new_from_fd (gint fd, GError **error); gboolean g_socket_bind (GSocket *socket, GSocketAddress *address, gboolean allow_reuse, GError **error); gboolean g_socket_listen (GSocket *socket, GError **error); GSocket * g_socket_accept (GSocket *socket, GError **error); gboolean g_socket_connect (GSocket *socket, GSocketAddress *address, GError **error); gboolean g_socket_check_connect_result (GSocket *socket, GError **error); gssize g_socket_receive (GSocket *socket, gchar *buffer, gsize size, GError **error); gssize g_socket_receive_from (GSocket *socket, GSocketAddress **address, gchar *buffer, gsize size, GError **error); gssize g_socket_receive_message (GSocket *socket, GSocketAddress **address, GInputVector *vectors, gint num_vectors, GSocketControlMessage ***messages, gint *num_messages, gint *flags, GError **error); gssize g_socket_send (GSocket *socket, const gchar *buffer, gsize size, GError **error); gssize g_socket_send_to (GSocket *socket, GSocketAddress *address, const gchar *buffer, gsize size, GError **error); gssize g_socket_send_message (GSocket *socket, GSocketAddress *address, GOutputVector *vectors, gint num_vectors, GSocketControlMessage **messages, gint num_messages, gint flags, GError **error); gboolean g_socket_close (GSocket *socket, GError **error); gboolean g_socket_is_closed (GSocket *socket); gboolean g_socket_shutdown (GSocket *socket, gboolean shutdown_read, gboolean shutdown_write, GError **error); gboolean g_socket_is_connected (GSocket *socket); GSource * g_socket_create_source (GSocket *socket, GIOCondition condition, GCancellable *cancellable); GIOCondition g_socket_condition_check (GSocket *socket, GIOCondition condition); gboolean g_socket_condition_wait (GSocket *socket, GIOCondition condition, GCancellable *cancellable, GError **error); void g_socket_set_listen_backlog (GSocket *socket, gint backlog); gint g_socket_get_listen_backlog (GSocket *socket); gboolean g_socket_get_blocking (GSocket *socket); void g_socket_set_blocking (GSocket *socket, gboolean blocking); gboolean g_socket_get_keepalive (GSocket *socket); void g_socket_set_keepalive (GSocket *socket, gboolean keepalive); GSocketFamily g_socket_get_family (GSocket *socket); int g_socket_get_fd (GSocket *socket); GSocketAddress * g_socket_get_local_address (GSocket *socket, GError **error); GSocketProtocol g_socket_get_protocol (GSocket *socket); GSocketAddress * g_socket_get_remote_address (GSocket *socket, GError **error); GSocketType g_socket_get_socket_type (GSocket *socket);
"blocking" gboolean : Read / Write "family" GSocketFamily : Read / Write / Construct Only "fd" gint : Read / Write / Construct Only "keepalive" gboolean : Read / Write "listen-backlog" gint : Read / Write "local-address" GSocketAddress* : Read "protocol" GSocketProtocol : Read / Write / Construct Only "remote-address" GSocketAddress* : Read "type" GSocketType : Read / Write / Construct Only
A GSocket is a low-level networking primitive. It is a more or less direct mapping of the BSD socket API in a portable GObject based API. It supports both the UNIX socket implementations and winsock2 on Windows.
GSocket is the platform independent base upon which the higher level network primitives are based. Applications are not typically meant to use it directly, but rather through classes like GSocketClient, GSocketService and GSocketConnection. However there may be cases where direct use of GSocket is useful.
GSocket implements the GInitable interface, so if it is manually constructed
by e.g. g_object_new()
you must call g_initable_init()
and check the
results before using the object. This is done automatically in
g_socket_new()
and g_socket_new_from_fd()
, so these functions can return
NULL
.
Sockets operate in two general modes, blocking or non-blocking. When
in blocking mode all operations 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 G_IO_ERROR_WOULD_BLOCK
error.
To know when a call would successfully run you can call g_socket_condition_check()
,
or g_socket_condition_wait()
. You can also use g_socket_create_source()
and
attach it to a GMainContext to get callbacks when I/O is possible.
Note that all sockets are always set to non blocking mode in the system, and
blocking mode is emulated in GSocket.
When working in non-blocking mode applications should always be able to
handle getting a G_IO_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 G_IO_ERROR_WOULD_BLOCK
.
GSockets can be either connection oriented or datagram based. For connection oriented types you must first establish a connection by either connecting to an address or accepting a connection from another address. For connectionless socket types the target/source address is specified or received in each I/O operation.
All socket file descriptors are set to be close-on-exec.
Note that creating a GSocket causes the signal SIGPIPE
to be
ignored for the remainder of the program. If you are writing a
command-line utility that uses GSocket, you may need to take into
account the fact that your program will not automatically be killed
if it tries to write to stdout
after it has been closed.
gboolean (*GSocketSourceFunc) (GSocket *socket, GIOCondition condition, gpointer user_data);
This is the function type of the callback used for the GSource
returned by g_socket_create_source()
.
|
the GSocket |
|
the current condition at the source fired. |
|
data passed in by the user. |
Returns : |
it should return FALSE if the source should be removed.
|
Since 2.22
typedef enum { G_SOCKET_TYPE_INVALID, G_SOCKET_TYPE_STREAM, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_TYPE_SEQPACKET } GSocketType;
Flags used when creating a GSocket. Some protocols may not implement all the socket types.
Type unknown or wrong | |
Reliable connection-based byte streams (e.g. TCP). | |
Connectionless, unreliable datagram passing. (e.g. UDP) | |
Reliable connection-based passing of datagrams of fixed maximum length (e.g. SCTP). |
Since 2.22
typedef enum { G_SOCKET_PROTOCOL_UNKNOWN = -1, G_SOCKET_PROTOCOL_DEFAULT = 0, G_SOCKET_PROTOCOL_TCP = 6, G_SOCKET_PROTOCOL_UDP = 17, G_SOCKET_PROTOCOL_SCTP = 132 } GSocketProtocol;
A protocol identifier is specified when creating a GSocket, which is a family/type specific identifier, where 0 means the default protocol for the particular family/type.
This enum contains a set of commonly available and used protocols. You can also pass any other identifiers handled by the platform in order to use protocols not listed here.
The protocol type is unknown | |
The default protocol for the family/type | |
TCP over IP | |
UDP over IP | |
SCTP over IP |
Since 2.22
typedef enum { G_SOCKET_MSG_NONE, G_SOCKET_MSG_OOB = GLIB_SYSDEF_MSG_OOB, G_SOCKET_MSG_PEEK = GLIB_SYSDEF_MSG_PEEK, G_SOCKET_MSG_DONTROUTE = GLIB_SYSDEF_MSG_DONTROUTE } GSocketMsgFlags;
Flags used in g_socket_receive_message()
and g_socket_send_message()
.
The flags listed in the enum are some commonly available flags, but the
values used for them are the same as on the platform, and any other flags
are passed in/out as is. So to use a platform specific flag, just include
the right system header and pass in the flag.
No flags. | |
Request to send/receive out of band data. | |
Read data from the socket without removing it from the queue. | |
Don't use a gateway to send out the packet, only send to hosts on directly connected networks. |
Since 2.22
typedef struct { gpointer buffer; gsize size; } GInputVector;
Structure used for scatter/gather data input. You generally pass in an array of GInputVectors and the operation will store the read data starting in the first buffer, switching to the next as needed.
gpointer |
Pointer to a buffer where data will be written. |
gsize |
the available size in buffer .
|
Since 2.22
typedef struct { gconstpointer buffer; gsize size; } GOutputVector;
Structure used for scatter/gather data output. You generally pass in an array of GOutputVectors and the operation will use all the buffers as if they were one buffer.
gconstpointer |
Pointer to a buffer of data to read. |
gsize |
the size of buffer .
|
Since 2.22
GSocket * g_socket_new (GSocketFamily family, GSocketType type, GSocketProtocol protocol, GError **error);
Creates a new GSocket with the defined family, type and protocol.
If protocol
is 0 (G_SOCKET_PROTOCOL_DEFAULT
) the default protocol type
for the family and type is used.
The protocol
is a family and type specific int that specifies what
kind of protocol to use. GSocketProtocol lists several common ones.
Many families only support one protocol, and use 0 for this, others
support several and using 0 means to use the default protocol for
the family and type.
The protocol id is passed directly to the operating system, so you can use protocols not listed in GSocketProtocol if you know the protocol number used for it.
|
the socket family to use, e.g. G_SOCKET_FAMILY_IPV4 .
|
|
the socket type to use. |
|
the id of the protocol to use, or 0 for default. |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
a GSocket or NULL on error.
Free the returned object with g_object_unref() .
|
Since 2.22
GSocket * g_socket_new_from_fd (gint fd, GError **error);
Creates a new GSocket from a native file descriptor or winsock SOCKET handle.
This reads all the settings from the file descriptor so that all properties should work. Note that the file descriptor will be set to non-blocking mode, independent on the blocking mode of the GSocket.
|
a native socket file descriptor. |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
a GSocket or NULL on error.
Free the returned object with g_object_unref() .
|
Since 2.22
gboolean g_socket_bind (GSocket *socket, GSocketAddress *address, gboolean allow_reuse, GError **error);
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()
).
If allow_reuse
is TRUE
this allows the bind call to succeed in some
situation where it would otherwise return a G_IO_ERROR_ADDRESS_IN_USE
error. The main example is for a TCP server socket where there are
outstanding connections in the WAIT state, which are generally safe
to ignore. However, setting it to TRUE
doesn't mean the call will
succeed if there is a socket actively bound to the address.
In general, pass TRUE
if the socket will be used to accept connections,
otherwise pass FALSE
.
|
a GSocket. |
|
a GSocketAddress specifying the local address. |
|
whether to allow reusing this address |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
TRUE on success, FALSE on error.
|
Since 2.22
gboolean g_socket_listen (GSocket *socket, GError **error);
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()
.
|
a GSocket. |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
TRUE on success, FALSE on error.
|
Since 2.22
GSocket * g_socket_accept (GSocket *socket, GError **error);
Accept incoming connections on a connection-based socket. This removes the first outstanding connection request from the listening socket and creates a GSocket object for it.
The socket
must be bound to a local address with g_socket_bind()
and
must be listening for incoming connections (g_socket_listen()
).
If there are no outstanding connections then the operation will block
or return G_IO_ERROR_WOULD_BLOCK
if non-blocking I/O is enabled.
To be notified of an incoming connection, wait for the G_IO_IN
condition.
|
a GSocket. |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
a new GSocket, or NULL on error.
Free the returned object with g_object_unref() .
|
Since 2.22
gboolean g_socket_connect (GSocket *socket, GSocketAddress *address, GError **error);
Connect the socket to the specified remote address.
For connection oriented socket this generally means we attempt to make
a connection to the address
. For a connection-less socket it sets
the default address for g_socket_send()
and discards all incoming datagrams
from other sources.
Generally connection oriented sockets can only connect once, but connection-less sockets can connect multiple times to change the default address.
If the connect call needs to do network I/O it will block, unless
non-blocking I/O is enabled. Then G_IO_ERROR_PENDING
is returned
and the user can be notified of the connection finishing by waiting
for the G_IO_OUT condition. The result of the connection can then be
checked with g_socket_check_connect_result()
.
|
a GSocket. |
|
a GSocketAddress specifying the remote address. |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
TRUE if connected, FALSE on error.
|
Since 2.22
gboolean g_socket_check_connect_result (GSocket *socket, GError **error);
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.
|
a GSocket |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
TRUE if no error, FALSE otherwise, setting error to the error
|
Since 2.22
gssize g_socket_receive (GSocket *socket, gchar *buffer, gsize size, GError **error);
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 NULL
.
If a message is too long to fit in buffer
, excess bytes may be discarded
depending on the type of socket the message is received from.
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 G_IO_ERROR_WOULD_BLOCK
error
will be returned. To be notified of available data, wait for the G_IO_IN
condition.
On error -1 is returned and error
is set accordingly.
|
a GSocket |
|
a buffer to read data into (which should be at least size
bytes long).
|
|
the number of bytes that will be read from the stream |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
Number of bytes read, or -1 on error |
Since 2.22
gssize g_socket_receive_from (GSocket *socket, GSocketAddress **address, gchar *buffer, gsize size, GError **error);
Receive data (up to size
bytes) from a socket.
If address
is non-NULL
then address
will be set equal to the
source address of the received packet.
address
is owned by the caller.
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 G_IO_ERROR_WOULD_BLOCK
error
will be returned. To be notified of available data, wait for the G_IO_IN
condition.
On error -1 is returned and error
is set accordingly.
|
a GSocket |
|
a pointer to a GSocketAddress pointer, or NULL
|
|
a buffer to read data into (which should be at least size
bytes long).
|
|
the number of bytes that will be read from the stream |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
Number of bytes read, or -1 on error |
Since 2.22
gssize g_socket_receive_message (GSocket *socket, GSocketAddress **address, GInputVector *vectors, gint num_vectors, GSocketControlMessage ***messages, gint *num_messages, gint *flags, GError **error);
Receive data from a socket. This is the most complicated and
fully-featured version of this call. For easier use, see
g_socket_receive()
and g_socket_receive_from()
.
If address
is non-NULL
then address
will be set equal to the
source address of the received packet.
address
is owned by the caller.
vector
must point to an array of GInputVector structs and
num_vectors
must be the length of this array. These structs
describe the buffers that received data will be scattered into.
If num_vector
is -1, then vector
is assumed to be terminated
by a GInputVector with a NULL
buffer pointer.
As a special case, if the size of the array is zero (in which case,
vectors
may of course be NULL
), then a single byte is received
and discarded. This is to facilitate the common practice of
sending a single '\0' byte for the purposes of transferring
ancillary data.
messages
, if non-NULL
, is taken to point to a pointer that will
be set to point to a newly-allocated array of
GSocketControlMessage instances. These correspond to the control
messages received from the kernel, one GSocketControlMessage per
message from the kernel. This array is NULL
-terminated and must be
freed by the caller using g_free()
.
num_messages
, if non-NULL
, will be set to the number of control
messages received.
If both messages
and num_messages
are non-NULL
, then
num_messages
gives the number of GSocketControlMessage instances
in messages
(ie: not including the NULL
terminator).
flags
is an in/out parameter. The commonly available arguments
for this is available in the GSocketMsgFlags enum, but the
values there are the same as the system values, and the flags
are passed in as-is, so you can pass in system specific flags too.
If the socket is in blocking mode the call will block until there is
some data to receive or there is an error. If there is no data available
and the socket is in non-blocking mode a G_IO_ERROR_WOULD_BLOCK
error
will be returned. To be notified of available data, wait for the G_IO_IN
condition.
On error -1 is returned and error
is set accordingly.
|
a GSocket |
|
a pointer to a GSocketAddress pointer, or NULL
|
|
an array of GInputVector structs |
|
the number of elements in vectors , or -1
|
|
a pointer which will be filled with an array of
GSocketControlMessages, or NULL
|
|
a pointer which will be filled with the number of
elements in messages , or NULL
|
|
a pointer to an int containing GSocketMsgFlags flags |
|
a GError pointer, or NULL
|
Returns : |
Number of bytes read, or -1 on error |
Since 2.22
gssize g_socket_send (GSocket *socket, const gchar *buffer, gsize size, GError **error);
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 NULL
.
If the socket is in blocking mode the call will block until there is
space for the data in the socket queue. If there is no space available
and the socket is in non-blocking mode a G_IO_ERROR_WOULD_BLOCK
error
will be returned. To be notified of available space, wait for the
G_IO_OUT
condition.
Note that on Windows you can't rely on a G_IO_OUT
condition
not producing a G_IO_ERROR_WOULD_BLOCK
error, as this is how Winsock
write notification works. However, robust apps should always be able to
handle this since it can easily appear in other cases too.
On error -1 is returned and error
is set accordingly.
|
a GSocket |
|
the buffer containing the data to send. |
|
the number of bytes to send |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
Number of bytes read, or -1 on error |
Since 2.22
gssize g_socket_send_to (GSocket *socket, GSocketAddress *address, const gchar *buffer, gsize size, GError **error);
Tries to send size
bytes from buffer
to address
. If address
is
NULL
then the message is sent to the default receiver (set by
g_socket_connect()
).
If the socket is in blocking mode the call will block until there is
space for the data in the socket queue. If there is no space available
and the socket is in non-blocking mode a G_IO_ERROR_WOULD_BLOCK
error
will be returned. To be notified of available space, wait for the G_IO_OUT
condition.
Note that on Windows you can't rely on a G_IO_OUT
condition
not producing a G_IO_ERROR_WOULD_BLOCK
error, as this is how Winsock
write notification works. However, robust apps should always be able to
handle this since it can easily appear in other cases too.
On error -1 is returned and error
is set accordingly.
|
a GSocket |
|
a GSocketAddress, or NULL
|
|
the buffer containing the data to send. |
|
the number of bytes to send |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
Number of bytes read, or -1 on error |
Since 2.22
gssize g_socket_send_message (GSocket *socket, GSocketAddress *address, GOutputVector *vectors, gint num_vectors, GSocketControlMessage **messages, gint num_messages, gint flags, GError **error);
Send data to address
on socket
. This is the most complicated and
fully-featured version of this call. For easier use, see
g_socket_send()
and g_socket_send_to()
.
If address
is NULL
then the message is sent to the default receiver
(set by g_socket_connect()
).
vector
must point to an array of GOutputVector structs and
num_vectors
must be the length of this array. These structs
describe the buffers that the sent data will be gathered from.
If num_vector
is -1, then vector
is assumed to be terminated
by a GOutputVector with a NULL
buffer pointer.
messages
, if non-NULL
, is taken to point to an array of num_messages
GSocketControlMessage instances. These correspond to the control
messages to be sent on the socket.
If num_messages
is -1 then messages
is treated as a NULL
-terminated
array.
flags
modify how the message sent. The commonly available arguments
for this is available in the GSocketMsgFlags enum, but the
values there are the same as the system values, and the flags
are passed in as-is, so you can pass in system specific flags too.
If the socket is in blocking mode the call will block until there is
space for the data in the socket queue. If there is no space available
and the socket is in non-blocking mode a G_IO_ERROR_WOULD_BLOCK
error
will be returned. To be notified of available space, wait for the G_IO_OUT
condition.
Note that on Windows you can't rely on a G_IO_OUT
condition
not producing a G_IO_ERROR_WOULD_BLOCK
error, as this is how Winsock
write notification works. However, robust apps should always be able to
handle this since it can easily appear in other cases too.
On error -1 is returned and error
is set accordingly.
|
a GSocket |
|
a GSocketAddress, or NULL
|
|
an array of GOutputVector structs |
|
the number of elements in vectors , or -1
|
|
a pointer to an array of GSocketControlMessages, or
NULL .
|
|
number of elements in messages , or -1.
|
|
an int containing GSocketMsgFlags flags |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
Number of bytes read, or -1 on error |
Since 2.22
gboolean g_socket_close (GSocket *socket, GError **error);
Closes the socket, shutting down any active connection.
Closing a socket does not wait for all outstanding I/O operations to finish, so the caller should not rely on them to be guaranteed to complete even if the close returns with no error.
Once the socket is closed, all other operations will return
G_IO_ERROR_CLOSED
. Closing a stream 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.
|
a GSocket |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
TRUE on success, FALSE on error
|
Since 2.22
gboolean g_socket_is_closed (GSocket *socket);
Checks whether a socket is closed.
Since 2.22
gboolean g_socket_shutdown (GSocket *socket, gboolean shutdown_read, gboolean shutdown_write, GError **error);
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.
|
a GSocket |
|
whether to shut down the read side |
|
whether to shut down the write side |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
TRUE on success, FALSE on error
|
Since 2.22
gboolean g_socket_is_connected (GSocket *socket);
Check whether the socket is connected. This is only useful for connection-oriented sockets.
Since 2.22
GSource * g_socket_create_source (GSocket *socket, GIOCondition condition, GCancellable *cancellable);
Creates a GSource
that can be attached to a GMainContext
to monitor
for the availibility of the specified condition
on the socket.
The callback on the source is of the GSocketSourceFunc type.
It is meaningless to specify G_IO_ERR
or G_IO_HUP
in condition;
these conditions will always be reported output if they are true.
cancellable
if not NULL
can be used to cancel the source, which will
cause the source to trigger, reporting the current condition (which
is likely 0 unless cancellation happened at the same time as a
condition change). You can check for this in the callback using
g_cancellable_is_cancelled()
.
|
a GSocket |
|
a GIOCondition mask to monitor |
|
a GCancellable or NULL
|
Returns : |
a newly allocated GSource , free with g_source_unref() .
|
Since 2.22
GIOCondition g_socket_condition_check (GSocket *socket, GIOCondition 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 G_IO_ERR
or G_IO_HUP
in condition;
these conditions will always be set in the output if they are true.
This call never blocks.
|
a GSocket |
|
a GIOCondition mask to check |
Returns : |
the GIOCondition mask of the current state
|
Since 2.22
gboolean g_socket_condition_wait (GSocket *socket, GIOCondition condition, GCancellable *cancellable, GError **error);
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-NULL
, is set to G_IO_ERROR_CANCELLED
.
|
a GSocket |
|
a GIOCondition mask to wait for |
|
a GCancellable, or NULL
|
|
a GError pointer, or NULL
|
Returns : |
TRUE if the condition was met, FALSE otherwise
|
Since 2.22
void g_socket_set_listen_backlog (GSocket *socket, gint 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.
|
a GSocket. |
|
the maximum number of pending connections. |
Since 2.22
gint g_socket_get_listen_backlog (GSocket *socket);
Gets the listen backlog setting of the socket. For details on this,
see g_socket_set_listen_backlog()
.
|
a GSocket. |
Returns : |
the maximum number of pending connections. |
Since 2.22
gboolean g_socket_get_blocking (GSocket *socket);
Gets the blocking mode of the socket. For details on blocking I/O,
see g_socket_set_blocking()
.
Since 2.22
void g_socket_set_blocking (GSocket *socket, gboolean 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 G_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.
|
a GSocket. |
|
Whether to use blocking I/O or not. |
Since 2.22
gboolean g_socket_get_keepalive (GSocket *socket);
Gets the keepalive mode of the socket. For details on this,
see g_socket_set_keepalive()
.
Since 2.22
void g_socket_set_keepalive (GSocket *socket, gboolean keepalive);
Setting keepalive
to TRUE
enables the sending of periodic ping requests
on idle connections in order to keep the connection alive. This is only
useful for connection oriented sockets. The exact period used between
each ping is system and protocol dependent.
Sending keepalive requests like this has a few disadvantages. For instance, it uses more network bandwidth, and it makes your application more sensitive to temporary outages in the network (i.e. if a cable is pulled your otherwise idle connection could be terminated, whereas otherwise it would survive unless actually used before the cable was reinserted). However, it is sometimes useful to ensure that connections are eventually terminated if e.g. the remote side is disconnected, so as to avoid leaking resources forever.
|
a GSocket. |
|
Whether to use try to keep the connection alive or not. |
Since 2.22
GSocketFamily g_socket_get_family (GSocket *socket);
Gets the socket family of the socket.
|
a GSocket. |
Returns : |
a GSocketFamily |
Since 2.22
int g_socket_get_fd (GSocket *socket);
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.
|
a GSocket. |
Returns : |
the file descriptor of the socket. |
Since 2.22
GSocketAddress * g_socket_get_local_address (GSocket *socket, GError **error);
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.
|
a GSocket. |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
a GSocketAddress or NULL on error.
Free the returned object with g_object_unref() .
|
Since 2.22
GSocketProtocol g_socket_get_protocol (GSocket *socket);
Gets the socket protocol id the socket was created with. In case the protocol is unknown, -1 is returned.
|
a GSocket. |
Returns : |
a protocol id, or -1 if unknown |
Since 2.22
GSocketAddress * g_socket_get_remote_address (GSocket *socket, GError **error);
Try to get the remove address of a connected socket. This is only useful for connection oriented sockets that have been connected.
|
a GSocket. |
|
GError for error reporting, or NULL to ignore.
|
Returns : |
a GSocketAddress or NULL on error.
Free the returned object with g_object_unref() .
|
Since 2.22
GSocketType g_socket_get_socket_type (GSocket *socket);
Gets the socket type of the socket.
|
a GSocket. |
Returns : |
a GSocketType |
Since 2.22
"blocking"
property"blocking" gboolean : Read / Write
Whether or not I/O on this socket is blocking.
Default value: TRUE
"family"
property"family" GSocketFamily : Read / Write / Construct Only
The sockets address family.
Default value: G_SOCKET_FAMILY_INVALID
"fd"
property"fd" gint : Read / Write / Construct Only
The sockets file descriptor.
Default value: -1
"keepalive"
property"keepalive" gboolean : Read / Write
Keep connection alive by sending periodic pings.
Default value: FALSE
"listen-backlog"
property"listen-backlog" gint : Read / Write
Outstanding connections in the listen queue.
Allowed values: [0,128]
Default value: 10
"local-address"
property"local-address" GSocketAddress* : Read
The local address the socket is bound to.
"protocol"
property"protocol" GSocketProtocol : Read / Write / Construct Only
The id of the protocol to use, or -1 for unknown.
Default value: G_SOCKET_PROTOCOL_UNKNOWN
"remote-address"
property"remote-address" GSocketAddress* : Read
The remote address the socket is connected to.
"type"
property"type" GSocketType : Read / Write / Construct Only
The sockets type.
Default value: G_SOCKET_TYPE_STREAM