GObject.Object
Gio.Socket
Import line: | Gio = imports.gi.Gio; |
GIR File: | Gio-2.0.gir |
C documentation: | GSocket |
Class : | Socket |
Implements: | Gio.Initable |
Extends: | GObject.Object |
Properties | Defined By | |
---|---|---|
blocking : gboolean
|
Gio.Socket | |
family : Gio.SocketFamily
|
Gio.Socket | |
fd : gint32
|
Gio.Socket | |
keepalive : gboolean
|
Gio.Socket | |
listen_backlog : gint32
|
Gio.Socket | |
local_address : Gio.SocketAddress
read only
|
Gio.Socket | |
parent_instance : GObject.Object
read only
|
Gio.Socket | |
priv : Gio.SocketPrivate
read only
|
Gio.Socket | |
protocol : Gio.SocketProtocol
|
Gio.Socket | |
remote_address : Gio.SocketAddress
read only
|
Gio.Socket | |
timeout : guint32
The timeout in seconds on socket I/O
The timeout in seconds on socket I/O
|
Gio.Socket | |
type : Gio.SocketType
|
Gio.Socket |
Method / Constructor | Defined By | |
---|---|---|
new Gio.Socket
(Object properties)
Create a new Gio.Socket
Create a new Gio.Socket
|
||
Create a new Gio.Socket
Create a new Gio.Socket
|
||
Create a new Gio.Socket
Create a new Gio.Socket
|
||
accept
(Cancellable cancellable)
:
Gio.Socket
Accept incoming connections on a connection-based socket.
Accept incoming connections on a connection-based socket. This removes
the first outstanding connection request from the listening socket and creates a GSocket object for it. The socket must be bound to a local address with g_socket_bind() and must be listening for incoming connections (g_socket_listen()). If there are no outstanding connections then the operation will block or return G_IO_ERROR_WOULD_BLOCK if non-blocking I/O is enabled. To be notified of an incoming connection, wait for the G_IO_IN condition. Free the returned object with g_object_unref().
|
Gio.Socket | |
When a socket is created it is attached to an address family, but it
doesn't have an address in this family.
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. 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 G_IO_ERROR_ADDRESS_IN_USE if the server program is stopped and then immediately restarted.)
|
Gio.Socket | |
check_connect_result
()
:
gboolean
Checks and resets the pending connect error for the socket.
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.
|
Gio.Socket | |
close
()
:
gboolean
Closes the socket, shutting down any active connection.
Closes the socket, shutting down any active connection.
Closing a socket does not wait for all outstanding I/O operations to finish, so the caller should not rely on them to be guaranteed to complete even if the close returns with no error. Once the socket is closed, all other operations will return G_IO_ERROR_CLOSED. Closing a socket multiple times will not return an error. Sockets will be automatically closed when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible. Beware that due to the way that TCP works, it is possible for recently-sent data to be lost if either you close a socket while the G_IO_IN condition is set, or else if the remote connection tries to send something to you after you close the socket but before it has finished reading all of the data you sent. There is no easy generic way to avoid this problem; the easiest fix is to design the network protocol such that the client will never send data "out of turn". Another solution is for the server to half-close the connection by calling g_socket_shutdown() with only the shutdown_write flag set, and then wait for the client to notice this and close its side of the connection, after which the server can safely call g_socket_close(). (This is what GTcpConnection 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.)
|
Gio.Socket | |
condition_check
(IOCondition condition)
:
GLib.IOCondition
Checks on the readiness of socket to perform operations.
Checks on the readiness of socket to perform operations.
The operations specified in condition are checked for and masked against the currently-satisfied conditions on socket. The result is returned. Note that on Windows, it is possible for an operation to return G_IO_ERROR_WOULD_BLOCK even immediately after g_socket_condition_check() has claimed that the socket is ready for writing. Rather than calling g_socket_condition_check() and then writing to the socket if it succeeds, it is generally better to simply try writing to the socket right away, and try again later if the initial attempt returns G_IO_ERROR_WOULD_BLOCK. It is meaningless to specify G_IO_ERR or G_IO_HUP in condition; these conditions will always be set in the output if they are true. This call never blocks.
|
Gio.Socket | |
Waits for condition to become true on socket.
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-NULL, is set to the appropriate value (G_IO_ERROR_CANCELLED or G_IO_ERROR_TIMED_OUT).
|
Gio.Socket | |
Connect the socket to the specified remote address.
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().
|
Gio.Socket | |
connection_factory_create_connection
()
:
Gio.SocketConnection
Creates a GSocketConnection subclass of the right type for
Creates a GSocketConnection subclass of the right type for
|
Gio.Socket | |
get_blocking
()
:
gboolean
Gets the blocking mode of the socket.
Gets the blocking mode of the socket. For details on blocking I/O,
see g_socket_set_blocking().
|
Gio.Socket | |
get_credentials
()
:
Gio.Credentials
Returns the credentials of the foreign process connected to this
socket, if any (e.g.
Returns the credentials of the foreign process connected to this
socket, if any (e.g. it is only supported for G_SOCKET_FAMILY_UNIX sockets). If this operation isn't supported on the OS, the method fails with the G_IO_ERROR_NOT_SUPPORTED error. On Linux this is implemented by reading the SO_PEERCRED option on the underlying socket. Other ways to obtain credentials from a foreign peer includes the GUnixCredentialsMessage type and g_unix_connection_send_credentials() / g_unix_connection_receive_credentials() functions. that must be freed with g_object_unref().
|
Gio.Socket | |
get_family
()
:
Gio.SocketFamily
Gets the socket family of the socket.
Gets the socket family of the socket.
|
Gio.Socket | |
get_fd
()
:
gint32
Returns the underlying OS socket object.
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.
|
Gio.Socket | |
get_keepalive
()
:
gboolean
Gets the keepalive mode of the socket.
Gets the keepalive mode of the socket. For details on this,
see g_socket_set_keepalive().
|
Gio.Socket | |
get_listen_backlog
()
:
gint32
Gets the listen backlog setting of the socket.
Gets the listen backlog setting of the socket. For details on this,
see g_socket_set_listen_backlog().
|
Gio.Socket | |
get_local_address
()
:
Gio.SocketAddress
Try to get the local address of a bound socket.
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. Free the returned object with g_object_unref().
|
Gio.Socket | |
get_protocol
()
:
Gio.SocketProtocol
Gets the socket protocol id the socket was created with.
Gets the socket protocol id the socket was created with.
In case the protocol is unknown, -1 is returned.
|
Gio.Socket | |
get_remote_address
()
:
Gio.SocketAddress
Try to get the remove address of a connected socket.
Try to get the remove address of a connected socket. This is only
useful for connection oriented sockets that have been connected. Free the returned object with g_object_unref().
|
Gio.Socket | |
get_socket_type
()
:
Gio.SocketType
Gets the socket type of the socket.
Gets the socket type of the socket.
|
Gio.Socket | |
get_timeout
()
:
guint32
Gets the timeout setting of the socket.
Gets the timeout setting of the socket. For details on this, see
g_socket_set_timeout().
|
Gio.Socket | |
init
(Cancellable cancellable)
:
gboolean
Initializes the object implementing the interface.
Initializes the object implementing the interface. This must be
done before any real use of the object after initial construction. Implementations may also support cancellation. If cancellable is not NULL, then initialization can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If cancellable is not NULL and the object doesn't support cancellable initialization the error G_IO_ERROR_NOT_SUPPORTED will be returned. If this function is not called, or returns with an error then all operations on the object should fail, generally returning the error G_IO_ERROR_NOT_INITIALIZED. Implementations of this method must be idempotent, i.e. multiple calls to this function with the same argument should return the same results. Only the first call initializes the object, further calls return the result of the first call. This is so that its safe to implement the singleton pattern in the GObject constructor function. return FALSE and set error appropriately if present.
|
Gio.Initable | |
is_closed
()
:
gboolean
Checks whether a socket is closed.
Checks whether a socket is closed.
|
Gio.Socket | |
is_connected
()
:
gboolean
Check whether the socket is connected.
Check whether the socket is connected. This is only useful for
connection-oriented sockets.
|
Gio.Socket | |
listen
()
:
gboolean
Marks the socket as a server socket, i.e.
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().
|
Gio.Socket | |
Receive data (up to size bytes) from a socket.
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. For G_SOCKET_TYPE_DATAGRAM and G_SOCKET_TYPE_SEQPACKET sockets, g_socket_receive() will always read either 0 or 1 complete messages from the socket. If the received message is too large to fit in buffer, then the data beyond size bytes will be discarded, without any explicit indication that this has occurred. For G_SOCKET_TYPE_STREAM sockets, g_socket_receive() can return any number of bytes, up to size. If more than size bytes have been received, the additional data will be returned in future calls to g_socket_receive(). If the socket is in blocking mode the call will block until there is some data to receive or there is an error. If there is no data available and the socket is in non-blocking mode, a G_IO_ERROR_WOULD_BLOCK error will be returned. To be notified when data is available, wait for the G_IO_IN condition. On error -1 is returned and error is set accordingly.
|
Gio.Socket | |
Receive data (up to size bytes) from a socket.
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. See g_socket_receive() for additional information.
|
Gio.Socket | |
receive_message
(SocketAddress address, Array vectors, gint32 num_vectors, Array messages, gint32 num_messages, gint32 flags, Cancellable cancellable)
:
gint32
Receive data from a socket.
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. describe the buffers that received data will be scattered into. If num_vectors is -1, then vectors is assumed to be terminated by a GInputVector with a NULL buffer pointer. As a special case, if num_vectors is 0 (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. array of GSocketControlMessage instances or NULL if no such messages was received. 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() after calling g_object_unref() on each element. If messages is NULL, any control messages received will be discarded. messages received. If both messages and num_messages are non-NULL, then for this are 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 (and g_socket_receive_message() may pass system-specific flags out). As with g_socket_receive(), data may be discarded if socket is G_SOCKET_TYPE_DATAGRAM or G_SOCKET_TYPE_SEQPACKET and you do not provide enough buffer space to read a complete message. You can pass G_SOCKET_MSG_PEEK in flags to peek at the current message without removing it from the receive queue, but there is no portable way to find out the length of the message other than by reading it into a sufficiently-large buffer. If the socket is in blocking mode the call will block until there is some data to receive or there is an error. If there is no data available and the socket is in non-blocking mode, a G_IO_ERROR_WOULD_BLOCK error will be returned. To be notified when data is available, wait for the G_IO_IN condition. On error -1 is returned and error is set accordingly.
|
Gio.Socket | |
receive_with_blocking
(String buffer, guint32 size, gboolean blocking, Cancellable cancellable)
:
gint32
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.
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.
|
Gio.Socket | |
Tries to send size bytes from buffer on the socket.
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 when space is available, wait for the G_IO_OUT condition. Note though that you may still receive G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously notified of a G_IO_OUT condition. (On Windows in particular, this is very common due to the way the underlying APIs work.) On error -1 is returned and error is set accordingly. on error
|
Gio.Socket | |
send_message
(SocketAddress address, Array vectors, gint32 num_vectors, Array messages, gint32 num_messages, gint32 flags, Cancellable cancellable)
:
gint32
Send data to address on socket.
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()). then vectors is assumed to be terminated by a GOutputVector with a NULL buffer pointer.) The GOutputVector structs describe the buffers that the sent data will be gathered from. Using multiple GOutputVectors is more memory-efficient than manually copying data from multiple sources into a single buffer, and more network-efficient than making multiple calls to g_socket_send(). 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. for this are 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 when space is available, wait for the G_IO_OUT condition. Note though that you may still receive G_IO_ERROR_WOULD_BLOCK from g_socket_send() even if you were previously notified of a G_IO_OUT condition. (On Windows in particular, this is very common due to the way the underlying APIs work.) On error -1 is returned and error is set accordingly. on error
|
Gio.Socket | |
Tries to send size bytes from buffer to address.
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()). See g_socket_send() for additional information. on error
|
Gio.Socket | |
send_with_blocking
(Array buffer, guint32 size, gboolean blocking, Cancellable cancellable)
:
gint32
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.
This behaves exactly the same as g_socket_send(), except that
the choice of blocking or non-blocking behavior is determined by the blocking argument rather than by socket's properties. on error
|
Gio.Socket | |
set_blocking
(gboolean blocking)
:
none
Sets the blocking mode of the socket.
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.
|
Gio.Socket | |
set_keepalive
(gboolean keepalive)
:
none
Sets or unsets the SO_KEEPALIVE flag on the underlying socket.
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, G_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.
|
Gio.Socket | |
set_listen_backlog
(gint32 backlog)
:
none
Sets the maximum number of outstanding connections allowed
when listening on this socket.
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.
|
Gio.Socket | |
set_timeout
(guint32 timeout)
:
none
Sets the time in seconds after which I/O operations on socket will
time out if they have not yet completed.
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 GSocket operation will time out after timeout seconds of inactivity, returning G_IO_ERROR_TIMED_OUT. On a non-blocking socket, calls to g_socket_condition_wait() will also fail with G_IO_ERROR_TIMED_OUT after the given time. Sources created with g_socket_create_source() will trigger after set, at which point calling g_socket_receive(), g_socket_send(), g_socket_check_connect_result(), etc, will fail with G_IO_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.
|
Gio.Socket | |
Shut down part of a full-duplex connection.
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.
|
Gio.Socket | |
speaks_ipv4
()
:
gboolean
Checks if a socket is capable of speaking IPv4.
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.
|
Gio.Socket |
None |
Class / Namespace | Method / Signal / Properties |
---|---|
Gio.SocketConnection
Property |
socket : Gio.Socket
|
Gio.SocketConnection
Method |
get_socket
()
:
Gio.Socket
Gets the underlying GSocket object of the connection.
|
Gio.SocketListener
Method |
Blocks waiting for a client to connect to any of the sockets added
to the listener. |
Gio.SocketListener
Method |
Finishes an async accept operation.
|
Gio.SocketListener
Method |
Adds socket to the set of sockets that we try to accept
new clients from. |
Gio.TcpWrapperConnection
Method |
Create a new Gio.TcpWrapperConnection
|