gi-gio-2.0.19: Gio bindings

Copyright | Will Thompson Iñaki García Etxebarria and Jonas Platte
License | LGPL-2.1
Maintainer | Iñaki García Etxebarria (garetxe@gmail.com)
Safe Haskell | None
Language | Haskell2010

GI.Gio.Objects.Socket

Contents

- Exported types
- Methods
  - accept
  - bind
  - checkConnectResult
  - close
  - conditionCheck
  - conditionTimedWait
  - conditionWait
  - connect
  - connectionFactoryCreateConnection
  - getAvailableBytes
  - getBlocking
  - getBroadcast
  - getCredentials
  - getFamily
  - getFd
  - getKeepalive
  - getListenBacklog
  - getLocalAddress
  - getMulticastLoopback
  - getMulticastTtl
  - getOption
  - getProtocol
  - getRemoteAddress
  - getSocketType
  - getTimeout
  - getTtl
  - isClosed
  - isConnected
  - joinMulticastGroup
  - joinMulticastGroupSsm
  - leaveMulticastGroup
  - leaveMulticastGroupSsm
  - listen
  - new
  - newFromFd
  - receive
  - receiveFrom
  - receiveMessage
  - receiveMessages
  - receiveWithBlocking
  - send
  - sendMessage
  - sendMessageWithTimeout
  - sendMessages
  - sendTo
  - sendWithBlocking
  - setBlocking
  - setBroadcast
  - setKeepalive
  - setListenBacklog
  - setMulticastLoopback
  - setMulticastTtl
  - setOption
  - setTimeout
  - setTtl
  - shutdown
  - speaksIpv4
- Properties
  - blocking
  - broadcast
  - family
  - fd
  - keepalive
  - listenBacklog
  - localAddress
  - multicastLoopback
  - multicastTtl
  - protocol
  - remoteAddress
  - timeout
  - ttl
  - type

Description

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, so if it is manually constructed by e.g. `g_object_new()` you must call `initableInit` and check the results before using the object. This is done automatically in `socketNew` and `socketNewFromFd`, so these functions can return `Nothing`.

Sockets operate in two general modes, blocking or non-blocking. When in blocking mode all operations (which don’t take an explicit blocking parameter) 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 `IOErrorEnumWouldBlock` error. To know when a call would successfully run you can call `socketConditionCheck`, or `socketConditionWait`. You can also use `g_socket_create_source()` and attach it to a `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 GSocket.

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

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

Like most other APIs in GLib, `Socket` is not inherently thread safe. To use a `Socket` concurrently from multiple threads, you must implement your own locking.

Since: 2.22

Synopsis

- newtype Socket = Socket (ManagedPtr Socket)
- class GObject o => IsSocket o
- toSocket :: (MonadIO m, IsSocket o) => o -> m Socket
- noSocket :: Maybe Socket
- socketAccept :: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) => a -> Maybe b -> m Socket
- socketBind :: (HasCallStack, MonadIO m, IsSocket a, IsSocketAddress b) => a -> b -> Bool -> m ()
- socketCheckConnectResult :: (HasCallStack, MonadIO m, IsSocket a) => a -> m ()
- socketClose :: (HasCallStack, MonadIO m, IsSocket a) => a -> m ()
- socketConditionCheck :: (HasCallStack, MonadIO m, IsSocket a) => a -> [IOCondition] -> m [IOCondition]
- socketConditionTimedWait :: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) => a -> [IOCondition] -> Int64 -> Maybe b -> m ()
- socketConditionWait :: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) => a -> [IOCondition] -> Maybe b -> m ()
- socketConnect :: (HasCallStack, MonadIO m, IsSocket a, IsSocketAddress b, IsCancellable c) => a -> b -> Maybe c -> m ()
- socketConnectionFactoryCreateConnection :: (HasCallStack, MonadIO m, IsSocket a) => a -> m SocketConnection
- socketGetAvailableBytes :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Int64
- socketGetBlocking :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Bool
- socketGetBroadcast :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Bool
- socketGetCredentials :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Credentials
- socketGetFamily :: (HasCallStack, MonadIO m, IsSocket a) => a -> m SocketFamily
- socketGetFd :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Int32
- socketGetKeepalive :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Bool
- socketGetListenBacklog :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Int32
- socketGetLocalAddress :: (HasCallStack, MonadIO m, IsSocket a) => a -> m SocketAddress
- socketGetMulticastLoopback :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Bool
- socketGetMulticastTtl :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Word32
- socketGetOption :: (HasCallStack, MonadIO m, IsSocket a) => a -> Int32 -> Int32 -> m Int32
- socketGetProtocol :: (HasCallStack, MonadIO m, IsSocket a) => a -> m SocketProtocol
- socketGetRemoteAddress :: (HasCallStack, MonadIO m, IsSocket a) => a -> m SocketAddress
- socketGetSocketType :: (HasCallStack, MonadIO m, IsSocket a) => a -> m SocketType
- socketGetTimeout :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Word32
- socketGetTtl :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Word32
- socketIsClosed :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Bool
- socketIsConnected :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Bool
- socketJoinMulticastGroup :: (HasCallStack, MonadIO m, IsSocket a, IsInetAddress b) => a -> b -> Bool -> Maybe Text -> m ()
- socketJoinMulticastGroupSsm :: (HasCallStack, MonadIO m, IsSocket a, IsInetAddress b, IsInetAddress c) => a -> b -> Maybe c -> Maybe Text -> m ()
- socketLeaveMulticastGroup :: (HasCallStack, MonadIO m, IsSocket a, IsInetAddress b) => a -> b -> Bool -> Maybe Text -> m ()
- socketLeaveMulticastGroupSsm :: (HasCallStack, MonadIO m, IsSocket a, IsInetAddress b, IsInetAddress c) => a -> b -> Maybe c -> Maybe Text -> m ()
- socketListen :: (HasCallStack, MonadIO m, IsSocket a) => a -> m ()
- socketNew :: (HasCallStack, MonadIO m) => SocketFamily -> SocketType -> SocketProtocol -> m Socket
- socketNewFromFd :: (HasCallStack, MonadIO m) => Int32 -> m Socket
- socketReceive :: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) => a -> ByteString -> Maybe b -> m Int64
- socketReceiveFrom :: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) => a -> ByteString -> Maybe b -> m (Int64, SocketAddress)
- socketReceiveMessage :: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) => a -> [InputVector] -> Int32 -> Maybe b -> m (Int64, Maybe SocketAddress, Maybe [SocketControlMessage], Int32)
- socketReceiveMessages :: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) => a -> [InputMessage] -> Int32 -> Maybe b -> m Int32
- socketReceiveWithBlocking :: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) => a -> ByteString -> Bool -> Maybe b -> m Int64
- socketSend :: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) => a -> ByteString -> Maybe b -> m Int64
- socketSendMessage :: (HasCallStack, MonadIO m, IsSocket a, IsSocketAddress b, IsCancellable c) => a -> Maybe b -> [OutputVector] -> Maybe [SocketControlMessage] -> Int32 -> Maybe c -> m Int64
- socketSendMessageWithTimeout :: (HasCallStack, MonadIO m, IsSocket a, IsSocketAddress b, IsCancellable c) => a -> Maybe b -> [OutputVector] -> Maybe [SocketControlMessage] -> Int32 -> Int64 -> Maybe c -> m (PollableReturn, Word64)
- socketSendMessages :: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) => a -> [OutputMessage] -> Int32 -> Maybe b -> m Int32
- socketSendTo :: (HasCallStack, MonadIO m, IsSocket a, IsSocketAddress b, IsCancellable c) => a -> Maybe b -> ByteString -> Maybe c -> m Int64
- socketSendWithBlocking :: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) => a -> ByteString -> Bool -> Maybe b -> m Int64
- socketSetBlocking :: (HasCallStack, MonadIO m, IsSocket a) => a -> Bool -> m ()
- socketSetBroadcast :: (HasCallStack, MonadIO m, IsSocket a) => a -> Bool -> m ()
- socketSetKeepalive :: (HasCallStack, MonadIO m, IsSocket a) => a -> Bool -> m ()
- socketSetListenBacklog :: (HasCallStack, MonadIO m, IsSocket a) => a -> Int32 -> m ()
- socketSetMulticastLoopback :: (HasCallStack, MonadIO m, IsSocket a) => a -> Bool -> m ()
- socketSetMulticastTtl :: (HasCallStack, MonadIO m, IsSocket a) => a -> Word32 -> m ()
- socketSetOption :: (HasCallStack, MonadIO m, IsSocket a) => a -> Int32 -> Int32 -> Int32 -> m ()
- socketSetTimeout :: (HasCallStack, MonadIO m, IsSocket a) => a -> Word32 -> m ()
- socketSetTtl :: (HasCallStack, MonadIO m, IsSocket a) => a -> Word32 -> m ()
- socketShutdown :: (HasCallStack, MonadIO m, IsSocket a) => a -> Bool -> Bool -> m ()
- socketSpeaksIpv4 :: (HasCallStack, MonadIO m, IsSocket a) => a -> m Bool
- constructSocketBlocking :: IsSocket o => Bool -> IO (GValueConstruct o)
- getSocketBlocking :: (MonadIO m, IsSocket o) => o -> m Bool
- setSocketBlocking :: (MonadIO m, IsSocket o) => o -> Bool -> m ()
- constructSocketBroadcast :: IsSocket o => Bool -> IO (GValueConstruct o)
- getSocketBroadcast :: (MonadIO m, IsSocket o) => o -> m Bool
- setSocketBroadcast :: (MonadIO m, IsSocket o) => o -> Bool -> m ()
- constructSocketFamily :: IsSocket o => SocketFamily -> IO (GValueConstruct o)
- getSocketFamily :: (MonadIO m, IsSocket o) => o -> m SocketFamily
- constructSocketFd :: IsSocket o => Int32 -> IO (GValueConstruct o)
- getSocketFd :: (MonadIO m, IsSocket o) => o -> m Int32
- constructSocketKeepalive :: IsSocket o => Bool -> IO (GValueConstruct o)
- getSocketKeepalive :: (MonadIO m, IsSocket o) => o -> m Bool
- setSocketKeepalive :: (MonadIO m, IsSocket o) => o -> Bool -> m ()
- constructSocketListenBacklog :: IsSocket o => Int32 -> IO (GValueConstruct o)
- getSocketListenBacklog :: (MonadIO m, IsSocket o) => o -> m Int32
- setSocketListenBacklog :: (MonadIO m, IsSocket o) => o -> Int32 -> m ()
- getSocketLocalAddress :: (MonadIO m, IsSocket o) => o -> m (Maybe SocketAddress)
- constructSocketMulticastLoopback :: IsSocket o => Bool -> IO (GValueConstruct o)
- getSocketMulticastLoopback :: (MonadIO m, IsSocket o) => o -> m Bool
- setSocketMulticastLoopback :: (MonadIO m, IsSocket o) => o -> Bool -> m ()
- constructSocketMulticastTtl :: IsSocket o => Word32 -> IO (GValueConstruct o)
- getSocketMulticastTtl :: (MonadIO m, IsSocket o) => o -> m Word32
- setSocketMulticastTtl :: (MonadIO m, IsSocket o) => o -> Word32 -> m ()
- constructSocketProtocol :: IsSocket o => SocketProtocol -> IO (GValueConstruct o)
- getSocketProtocol :: (MonadIO m, IsSocket o) => o -> m SocketProtocol
- getSocketRemoteAddress :: (MonadIO m, IsSocket o) => o -> m (Maybe SocketAddress)
- constructSocketTimeout :: IsSocket o => Word32 -> IO (GValueConstruct o)
- getSocketTimeout :: (MonadIO m, IsSocket o) => o -> m Word32
- setSocketTimeout :: (MonadIO m, IsSocket o) => o -> Word32 -> m ()
- constructSocketTtl :: IsSocket o => Word32 -> IO (GValueConstruct o)
- getSocketTtl :: (MonadIO m, IsSocket o) => o -> m Word32
- setSocketTtl :: (MonadIO m, IsSocket o) => o -> Word32 -> m ()
- constructSocketType :: IsSocket o => SocketType -> IO (GValueConstruct o)
- getSocketType :: (MonadIO m, IsSocket o) => o -> m SocketType

# Exported types

newtype Socket Source #

Memory-managed wrapper type.

Constructors

Socket (ManagedPtr Socket) |

Instances

GObject Socket Source # |
Instance details Defined in GI.Gio.Objects.Socket Methods gobjectType :: Socket -> IO GType Source #
IsObject Socket Source # |
Instance details Defined in GI.Gio.Objects.Socket
IsDatagramBased Socket Source # |
Instance details Defined in GI.Gio.Objects.Socket
IsInitable Socket Source # |
Instance details Defined in GI.Gio.Objects.Socket
IsSocket Socket Source # |
Instance details Defined in GI.Gio.Objects.Socket

class GObject o => IsSocket o Source #

Type class for types which can be safely cast to `Socket`, for instance with `toSocket`.

Instances

(GObject a, (UnknownAncestorError Socket a :: Constraint)) => IsSocket a Source # |
Instance details Defined in GI.Gio.Objects.Socket
IsSocket Socket Source # |
Instance details Defined in GI.Gio.Objects.Socket

toSocket :: (MonadIO m, IsSocket o) => o -> m Socket Source #

Cast to `Socket`, for types for which this is known to be safe. For general casts, use `castTo`.

noSocket :: Maybe Socket Source #

A convenience alias for `Nothing` :: `Maybe` `Socket`.

# Methods

## accept

socketAccept Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) |
=> a | `socket`: a `Socket`.
-> Maybe b | `cancellable`: a `GCancellable` or `Nothing`
-> m Socket | Returns: a new `Socket`, or `Nothing` on error. Free the returned object with `objectUnref`. (Can throw `GError`)

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 `socketBind` and must be listening for incoming connections (`socketListen`).

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

Since: 2.22

## bind

socketBind Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsSocketAddress b) |
=> a | `socket`: a `Socket`.
-> b | `address`: a `SocketAddress` specifying the local address.
-> Bool | `allowReuse`: whether to allow reusing this address
-> m () | (Can throw `GError`)

When a socket is created it is attached to an address family, but it doesn't have an address in this family. `socketBind` 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 `socketListen` and `socketAccept` ). 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 `allowReuse` controls the setting of the `SO_REUSEADDR` socket option; normally it should be `True` for server sockets (sockets that you will eventually call `socketAccept` on), and `False` for client sockets. (Failing to set this flag on a server socket may cause `socketBind` to return `IOErrorEnumAddressInUse` if the server program is stopped and then immediately restarted.)

If `socket` is a UDP socket, then `allowReuse` 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.)

Since: 2.22

## checkConnectResult

socketCheckConnectResult Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`
-> m () | (Can throw `GError`)

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

Since: 2.22

## close

socketClose Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`
-> m () | (Can throw `GError`)

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 `IOErrorEnumClosed`. 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 `IOConditionIn` 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 `socketShutdown` with only the `shutdownWrite` 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 `socketClose`. (This is what `TcpConnection` does if you call `tcpConnectionSetGracefulDisconnect`. But of course, this only works if the client will close its connection after the server does.)

Since: 2.22

## conditionCheck

socketConditionCheck Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`
-> [IOCondition] | `condition`: a `IOCondition` mask to check
-> m [IOCondition] | Returns: the `gIOCondition` mask of the current state

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 `IOErrorEnumWouldBlock` even immediately after `socketConditionCheck` has claimed that the socket is ready for writing. Rather than calling `socketConditionCheck` 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 `IOErrorEnumWouldBlock`.

It is meaningless to specify `IOConditionErr` or `IOConditionHup` in condition; these conditions will always be set in the output if they are true.

This call never blocks.

Since: 2.22

## conditionTimedWait

socketConditionTimedWait Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) |
=> a | `socket`: a `Socket`
-> [IOCondition] | `condition`: a `IOCondition` mask to wait for
-> Int64 | `timeoutUs`: the maximum time (in microseconds) to wait, or -1
-> Maybe b | `cancellable`: a `Cancellable`, or `Nothing`
-> m () | (Can throw `GError`)

Waits for up to `timeoutUs` 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 `timeoutUs` (or the socket's `Socket`:`timeout`) is reached before the condition is met, then `False` is returned and `error`, if non-`Nothing`, is set to the appropriate value (`IOErrorEnumCancelled` or `IOErrorEnumTimedOut`).

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

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

Since: 2.32

## conditionWait

socketConditionWait Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) |
=> a | `socket`: a `Socket`
-> [IOCondition] | `condition`: a `IOCondition` mask to wait for
-> Maybe b | `cancellable`: a `Cancellable`, or `Nothing`
-> m () | (Can throw `GError`)

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-`Nothing`, is set to the appropriate value (`IOErrorEnumCancelled` or `IOErrorEnumTimedOut`).

See also `socketConditionTimedWait`.

Since: 2.22

## connect

socketConnect Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsSocketAddress b, IsCancellable c) |
=> a | `socket`: a `Socket`.
-> b | `address`: a `SocketAddress` specifying the remote address.
-> Maybe c | `cancellable`: a `GCancellable` or `Nothing`
-> m () | (Can throw `GError`)

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 `socketSend` 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 `IOErrorEnumPending` is returned and the user can be notified of the connection finishing by waiting for the G_IO_OUT condition. The result of the connection must then be checked with `socketCheckConnectResult`.

Since: 2.22

## connectionFactoryCreateConnection

socketConnectionFactoryCreateConnection Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`
-> m SocketConnection | Returns: a `SocketConnection`

Creates a `SocketConnection` subclass of the right type for `socket`.

Since: 2.22

## getAvailableBytes

socketGetAvailableBytes Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`
-> m Int64 | Returns: the number of bytes that can be read from the socket without blocking or truncating, or -1 on error.

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 `socketReceive` with a buffer of that size, rather than calling `socketGetAvailableBytes` first and then doing a receive of exactly the right size.

Since: 2.32

## getBlocking

socketGetBlocking Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m Bool | Returns: `True` if blocking I/O is used, `False` otherwise.

Gets the blocking mode of the socket. For details on blocking I/O, see `socketSetBlocking`.

Since: 2.22

## getBroadcast

socketGetBroadcast Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m Bool | Returns: the broadcast setting on `socket`

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

Since: 2.32

## getCredentials

socketGetCredentials Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m Credentials | Returns: `Nothing` if `error` is set, otherwise a `Credentials` object that must be freed with `objectUnref`. (Can throw `GError`)

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

If this operation isn't supported on the OS, the method fails with the `IOErrorEnumNotSupported` 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 `UnixCredentialsMessage` type and `unixConnectionSendCredentials` / `unixConnectionReceiveCredentials` functions.

Since: 2.26

## getFamily

socketGetFamily Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m SocketFamily | Returns: a `SocketFamily`

Gets the socket family of the socket.

Since: 2.22

## getFd

socketGetFd Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m Int32 | Returns: the file descriptor of the 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.

Since: 2.22

## getKeepalive

socketGetKeepalive Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m Bool | Returns: `True` if keepalive is active, `False` otherwise.

Gets the keepalive mode of the socket. For details on this, see `socketSetKeepalive`.

Since: 2.22

## getListenBacklog

socketGetListenBacklog Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m Int32 | Returns: the maximum number of pending connections.

Gets the listen backlog setting of the socket. For details on this, see `socketSetListenBacklog`.

Since: 2.22

## getLocalAddress

socketGetLocalAddress Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m SocketAddress | Returns: a `SocketAddress` or `Nothing` on error. Free the returned object with `objectUnref`. (Can throw `GError`)

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

## getMulticastLoopback

socketGetMulticastLoopback Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m Bool | Returns: the multicast loopback setting on `socket`

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

## getMulticastTtl

socketGetMulticastTtl Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m Word32 | Returns: the multicast time-to-live setting on `socket`

Gets the multicast time-to-live setting on `socket`; see `socketSetMulticastTtl` for more details.

Since: 2.32

## getOption

socketGetOption Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`
-> Int32 | `level`: the "API level" of the option (eg, `SOL_SOCKET`)
-> Int32 | `optname`: the "name" of the option (eg, `SO_BROADCAST`)
-> m Int32 | (Can throw `GError`)

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 `gint` variable, not a `guchar`; `socketGetOption` will handle the conversion internally.

Since: 2.36

## getProtocol

socketGetProtocol Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m SocketProtocol | Returns: a protocol id, or -1 if unknown

Gets the socket protocol id the socket was created with. In case the protocol is unknown, -1 is returned.

Since: 2.22

## getRemoteAddress

socketGetRemoteAddress Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m SocketAddress | Returns: a `SocketAddress` or `Nothing` on error. Free the returned object with `objectUnref`. (Can throw `GError`)

Try to get the remote address of a connected socket. This is only useful for connection oriented sockets that have been connected.

Since: 2.22

## getSocketType

socketGetSocketType Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m SocketType | Returns: a `SocketType`

Gets the socket type of the socket.

Since: 2.22

## getTimeout

socketGetTimeout Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m Word32 | Returns: the timeout in seconds

Gets the timeout setting of the socket. For details on this, see `socketSetTimeout`.

Since: 2.26

## getTtl

socketGetTtl Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m Word32 | Returns: the time-to-live setting on `socket`

Gets the unicast time-to-live setting on `socket`; see `socketSetTtl` for more details.

Since: 2.32

## isClosed

socketIsClosed Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`
-> m Bool | Returns: `True` if socket is closed, `False` otherwise

Checks whether a socket is closed.

Since: 2.22

## isConnected

socketIsConnected Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m Bool | Returns: `True` if socket is connected, `False` otherwise.

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

If using `socketShutdown`, 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 `socketCheckConnectResult`.

Since: 2.22

## joinMulticastGroup

socketJoinMulticastGroup Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsInetAddress b) |
=> a | `socket`: a `Socket`.
-> b | `group`: a `InetAddress` specifying the group address to join.
-> Bool | `sourceSpecific`: `True` if source-specific multicast should be used
-> Maybe Text | `iface`: Name of the interface to use, or `Nothing`
-> m () | (Can throw `GError`)

Registers `socket` to receive multicast messages sent to `group`. `socket` must be a `SocketTypeDatagram` socket, and must have been bound to an appropriate interface and port with `socketBind`.

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

If `sourceSpecific` is `True`, source-specific multicast as defined in RFC 4604 is used. Note that on older platforms this may fail with a `IOErrorEnumNotSupported` error.

To bind to a given source-specific multicast address, use `socketJoinMulticastGroupSsm` instead.

Since: 2.32

## joinMulticastGroupSsm

socketJoinMulticastGroupSsm Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsInetAddress b, IsInetAddress c) |
=> a | `socket`: a `Socket`.
-> b | `group`: a `InetAddress` specifying the group address to join.
-> Maybe c | `sourceSpecific`: a `InetAddress` specifying the source-specific multicast address or `Nothing` to ignore.
-> Maybe Text | `iface`: Name of the interface to use, or `Nothing`
-> m () | (Can throw `GError`)

Registers `socket` to receive multicast messages sent to `group`. `socket` must be a `SocketTypeDatagram` socket, and must have been bound to an appropriate interface and port with `socketBind`.

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

If `sourceSpecific` is not `Nothing`, use source-specific multicast as defined in RFC 4604. Note that on older platforms this may fail with a `IOErrorEnumNotSupported` error.

Note that this function can be called multiple times for the same `group` with different `sourceSpecific` in order to receive multicast packets from more than one source.

Since: 2.56

## leaveMulticastGroup

socketLeaveMulticastGroup Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsInetAddress b) |
=> a | `socket`: a `Socket`.
-> b | `group`: a `InetAddress` specifying the group address to leave.
-> Bool | `sourceSpecific`: `True` if source-specific multicast was used
-> Maybe Text | `iface`: Interface used
-> m () | (Can throw `GError`)

Removes `socket` from the multicast group defined by `group`, `iface`, and `sourceSpecific` (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 `socketLeaveMulticastGroupSsm` instead.

Since: 2.32

## leaveMulticastGroupSsm

socketLeaveMulticastGroupSsm Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsInetAddress b, IsInetAddress c) |
=> a | `socket`: a `Socket`.
-> b | `group`: a `InetAddress` specifying the group address to leave.
-> Maybe c | `sourceSpecific`: a `InetAddress` specifying the source-specific multicast address or `Nothing` to ignore.
-> Maybe Text | `iface`: Name of the interface to use, or `Nothing`
-> m () | (Can throw `GError`)

Removes `socket` from the multicast group defined by `group`, `iface`, and `sourceSpecific` (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.

Since: 2.56

## listen

socketListen Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> m () | (Can throw `GError`)

Marks the socket as a server socket, i.e. a socket that is used to accept incoming requests using `socketAccept`.

Before calling this the socket must be bound to a local address using `socketBind`.

To set the maximum amount of outstanding clients, use `socketSetListenBacklog`.

Since: 2.22

## new

socketNew Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> SocketFamily | `family`: the socket family to use, e.g. `SocketFamilyIpv4`.
-> SocketType | `type`: the socket type to use.
-> SocketProtocol | `protocol`: the id of the protocol to use, or 0 for default.
-> m Socket | Returns: a `Socket` or `Nothing` on error. Free the returned object with `objectUnref`. (Can throw `GError`)

Creates a new `Socket` with the defined family, type and protocol. If `protocol` is 0 (`SocketProtocolDefault`) 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: 2.22

## newFromFd

socketNewFromFd Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Int32 | `fd`: a native socket file descriptor.
-> m Socket | Returns: a `Socket` or `Nothing` on error. Free the returned object with `objectUnref`. (Can throw `GError`)

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 `IOErrorEnumFailed`

Since: 2.22

## receive

socketReceive Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) |
=> a | `socket`: a `Socket`
-> ByteString | `buffer`: a buffer to read data into (which should be at least `size` bytes long).
-> Maybe b | `cancellable`: a `GCancellable` or `Nothing`
-> m Int64 | Returns: Number of bytes read, or 0 if the connection was closed by the peer, or -1 on error (Can throw `GError`)

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

For `SocketTypeDatagram` and `SocketTypeSeqpacket` sockets, `socketReceive` 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 `SocketTypeStream` sockets, `socketReceive` 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 `socketReceive`.

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

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

Since: 2.22

## receiveFrom

socketReceiveFrom Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) |
=> a | `socket`: a `Socket`
-> ByteString | `buffer`: a buffer to read data into (which should be at least `size` bytes long).
-> Maybe b | `cancellable`: a `GCancellable` or `Nothing`
-> m (Int64, SocketAddress) | Returns: Number of bytes read, or 0 if the connection was closed by the peer, or -1 on error (Can throw `GError`)

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

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

See `socketReceive` for additional information.

Since: 2.22

## receiveMessage

socketReceiveMessage Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) |
=> a | `socket`: a `Socket`
-> [InputVector] | `vectors`: an array of `InputVector` structs
-> Int32 | `flags`: a pointer to an int containing `SocketMsgFlags` flags
-> Maybe b | `cancellable`: a `GCancellable` or `Nothing`
-> m (Int64, Maybe SocketAddress, Maybe [SocketControlMessage], Int32) | Returns: Number of bytes read, or 0 if the connection was closed by the peer, or -1 on error (Can throw `GError`)

Receive data from a socket. For receiving multiple messages, see `socketReceiveMessages`; for easier use, see `socketReceive` and `socketReceiveFrom`.

If `address` is non-`Nothing` 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 `InputVector` structs and `numVectors` must be the length of this array. These structs describe the buffers that received data will be scattered into. If `numVectors` is -1, then `vectors` is assumed to be terminated by a `InputVector` with a `Nothing` buffer pointer.

As a special case, if `numVectors` is 0 (in which case, `vectors` may of course be `Nothing`), 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-`Nothing`, will be set to point to a newly-allocated array of `SocketControlMessage` instances or `Nothing` if no such messages was received. These correspond to the control messages received from the kernel, one `SocketControlMessage` per message from the kernel. This array is `Nothing`-terminated and must be freed by the caller using `free` after calling `objectUnref` on each element. If `messages` is `Nothing`, any control messages received will be discarded.

`numMessages`, if non-`Nothing`, will be set to the number of control messages received.

If both `messages` and `numMessages` are non-`Nothing`, then `numMessages` gives the number of `SocketControlMessage` instances in `messages` (ie: not including the `Nothing` terminator).

`flags` is an in/out parameter. The commonly available arguments for this are available in the `SocketMsgFlags` 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 `socketReceiveMessage` may pass system-specific flags out). Flags passed in to the parameter affect the receive operation; flags returned out of it are relevant to the specific returned message.

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

If the socket is in blocking mode the call will block until there is some data to receive, the connection is closed, or there is an error. If there is no data available and the socket is in non-blocking mode, a `IOErrorEnumWouldBlock` error will be returned. To be notified when data is available, wait for the `IOConditionIn` condition.

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

Since: 2.22

## receiveMessages

socketReceiveMessages Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) |
=> a | `socket`: a `Socket`
-> [InputMessage] | `messages`: an array of `InputMessage` structs
-> Int32 | `flags`: an int containing `SocketMsgFlags` flags for the overall operation
-> Maybe b | `cancellable`: a `GCancellable` or `Nothing`
-> m Int32 | Returns: number of messages received, or -1 on error. Note that the number of messages received may be smaller than `numMessages` if in non-blocking mode, if the peer closed the connection, or if `numMessages` was larger than `UIO_MAXIOV` (1024), in which case the caller may re-try to receive the remaining messages. (Can throw `GError`)

Receive multiple data messages from `socket` in one go. This is the most complicated and fully-featured version of this call. For easier use, see `socketReceive`, `socketReceiveFrom`, and `socketReceiveMessage`.

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

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

The other members of `InputMessage` are treated as described in its documentation.

If `Socket`:`blocking` is `True` the call will block until `numMessages` have been received, or the end of the stream is reached.

If `Socket`:`blocking` is `False` the call will return up to `numMessages` without blocking, or `IOErrorEnumWouldBlock` if no messages are queued in the operating system to be received.

In blocking mode, if `Socket`:`timeout` is positive and is reached before any messages are received, `IOErrorEnumTimedOut` is returned, otherwise up to `numMessages` are returned. (Note: This is effectively the behaviour of `MSG_WAITFORONE` with `recvmmsg()`.)

To be notified when messages are available, wait for the `IOConditionIn` condition. Note though that you may still receive `IOErrorEnumWouldBlock` from `socketReceiveMessages` even if you were previously notified of a `IOConditionIn` condition.

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

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

Since: 2.48

## receiveWithBlocking

socketReceiveWithBlocking Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) |
=> a | `socket`: a `Socket`
-> ByteString | `buffer`: a buffer to read data into (which should be at least `size` bytes long).
-> Bool | `blocking`: whether to do blocking or non-blocking I/O
-> Maybe b | `cancellable`: a `GCancellable` or `Nothing`
-> m Int64 | Returns: Number of bytes read, or 0 if the connection was closed by the peer, or -1 on error (Can throw `GError`)

This behaves exactly the same as `socketReceive`, except that the choice of blocking or non-blocking behavior is determined by the `blocking` argument rather than by `socket`'s properties.

Since: 2.26

## send

socketSend Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) |
=> a | `socket`: a `Socket`
-> ByteString | `buffer`: the buffer containing the data to send.
-> Maybe b | `cancellable`: a `GCancellable` or `Nothing`
-> m Int64 | Returns: Number of bytes written (which may be less than `size`), or -1 on error (Can throw `GError`)

Tries to send `size` bytes from `buffer` on the socket. This is mainly used by connection-oriented sockets; it is identical to `socketSendTo` with `address` set to `Nothing`.

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 `IOErrorEnumWouldBlock` error will be returned. To be notified when space is available, wait for the `IOConditionOut` condition. Note though that you may still receive `IOErrorEnumWouldBlock` from `socketSend` even if you were previously notified of a `IOConditionOut` 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: 2.22

## sendMessage

socketSendMessage Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsSocketAddress b, IsCancellable c) |
=> a | `socket`: a `Socket`
-> Maybe b | `address`: a `SocketAddress`, or `Nothing`
-> [OutputVector] | `vectors`: an array of `OutputVector` structs
-> Maybe [SocketControlMessage] | `messages`: a pointer to an array of `GSocketControlMessages`, or `Nothing`.
-> Int32 | `flags`: an int containing `SocketMsgFlags` flags
-> Maybe c | `cancellable`: a `GCancellable` or `Nothing`
-> m Int64 | Returns: Number of bytes written (which may be less than `size`), or -1 on error (Can throw `GError`)

Send data to `address` on `socket`. For sending multiple messages see `socketSendMessages`; for easier use, see `socketSend` and `socketSendTo`.

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

`vectors` must point to an array of `OutputVector` structs and `numVectors` must be the length of this array. (If `numVectors` is -1, then `vectors` is assumed to be terminated by a `OutputVector` with a `Nothing` buffer pointer.) The `OutputVector` 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 `socketSend`.

`messages`, if non-`Nothing`, is taken to point to an array of `numMessages` `SocketControlMessage` instances. These correspond to the control messages to be sent on the socket. If `numMessages` is -1 then `messages` is treated as a `Nothing`-terminated array.

`flags` modify how the message is sent. The commonly available arguments for this are available in the `SocketMsgFlags` 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 `IOErrorEnumWouldBlock` error will be returned. To be notified when space is available, wait for the `IOConditionOut` condition. Note though that you may still receive `IOErrorEnumWouldBlock` from `socketSend` even if you were previously notified of a `IOConditionOut` 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: 2.22

## sendMessageWithTimeout

socketSendMessageWithTimeout Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsSocketAddress b, IsCancellable c) |
=> a | `socket`: a `Socket`
-> Maybe b | `address`: a `SocketAddress`, or `Nothing`
-> [OutputVector] | `vectors`: an array of `OutputVector` structs
-> Maybe [SocketControlMessage] | `messages`: a pointer to an array of `GSocketControlMessages`, or `Nothing`.
-> Int32 | `flags`: an int containing `SocketMsgFlags` flags
-> Int64 | `timeoutUs`: the maximum time (in microseconds) to wait, or -1
-> Maybe c | `cancellable`: a `GCancellable` or `Nothing`
-> m (PollableReturn, Word64) | Returns: `PollableReturnOk` if all data was successfully written, `PollableReturnWouldBlock` if the socket is currently not writable, or `PollableReturnFailed` if an error happened and `error` is set. (Can throw `GError`)

This behaves exactly the same as `socketSendMessage`, except that the choice of timeout behavior is determined by the `timeoutUs` argument rather than by `socket`'s properties.

On error `PollableReturnFailed` is returned and `error` is set accordingly, or if the socket is currently not writable `PollableReturnWouldBlock` is returned. `bytesWritten` will contain 0 in both cases.

Since: 2.60

## sendMessages

socketSendMessages Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) |
=> a | `socket`: a `Socket`
-> [OutputMessage] | `messages`: an array of `OutputMessage` structs
-> Int32 | `flags`: an int containing `SocketMsgFlags` flags
-> Maybe b | `cancellable`: a `GCancellable` or `Nothing`
-> m Int32 | Returns: number of messages sent, or -1 on error. Note that the number of messages sent may be smaller than `numMessages` if the socket is non-blocking or if `numMessages` was larger than UIO_MAXIOV (1024), in which case the caller may re-try to send the remaining messages. (Can throw `GError`)

Send multiple data messages from `socket` in one go. This is the most complicated and fully-featured version of this call. For easier use, see `socketSend`, `socketSendTo`, and `socketSendMessage`.

`messages` must point to an array of `OutputMessage` structs and `numMessages` must be the length of this array. Each `OutputMessage` contains an address to send the data to, and a pointer to an array of `OutputVector` structs to describe the buffers that the data to be sent for each message 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 `socketSend`. Sending multiple messages in one go avoids the overhead of making a lot of syscalls in scenarios where a lot of data packets need to be sent (e.g. high-bandwidth video streaming over RTP/UDP), or where the same data needs to be sent to multiple recipients.

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

If the socket is in blocking mode the call will block until there is space for all the data in the socket queue. If there is no space available and the socket is in non-blocking mode a `IOErrorEnumWouldBlock` error will be returned if no data was written at all, otherwise the number of messages sent will be returned. To be notified when space is available, wait for the `IOConditionOut` condition. Note though that you may still receive `IOErrorEnumWouldBlock` from `socketSend` even if you were previously notified of a `IOConditionOut` condition. (On Windows in particular, this is very common due to the way the underlying APIs work.)

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

Since: 2.44

## sendTo

socketSendTo Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsSocketAddress b, IsCancellable c) |
=> a | `socket`: a `Socket`
-> Maybe b | `address`: a `SocketAddress`, or `Nothing`
-> ByteString | `buffer`: the buffer containing the data to send.
-> Maybe c | `cancellable`: a `GCancellable` or `Nothing`
-> m Int64 | Returns: Number of bytes written (which may be less than `size`), or -1 on error (Can throw `GError`)

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

See `socketSend` for additional information.

Since: 2.22

## sendWithBlocking

socketSendWithBlocking Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a, IsCancellable b) |
=> a | `socket`: a `Socket`
-> ByteString | `buffer`: the buffer containing the data to send.
-> Bool | `blocking`: whether to do blocking or non-blocking I/O
-> Maybe b | `cancellable`: a `GCancellable` or `Nothing`
-> m Int64 | Returns: Number of bytes written (which may be less than `size`), or -1 on error (Can throw `GError`)

This behaves exactly the same as `socketSend`, except that the choice of blocking or non-blocking behavior is determined by the `blocking` argument rather than by `socket`'s properties.

Since: 2.26

## setBlocking

socketSetBlocking Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> Bool | `blocking`: Whether to use blocking I/O or not.
-> m () |

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 `IOErrorEnumWouldBlock` 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: 2.22

## setBroadcast

socketSetBroadcast Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> Bool | `broadcast`: whether `socket` should allow sending to broadcast addresses
-> m () |

Sets whether `socket` should allow sending to broadcast addresses. This is `False` by default.

Since: 2.32

## setKeepalive

socketSetKeepalive Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> Bool | `keepalive`: Value for the keepalive flag
-> m () |

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, `SocketProtocolTcp` 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: 2.22

## setListenBacklog

socketSetListenBacklog Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> Int32 | `backlog`: the maximum number of pending connections.
-> m () |

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 `socketListen` and has no effect if called after that.

Since: 2.22

## setMulticastLoopback

socketSetMulticastLoopback Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> Bool | `loopback`: whether `socket` should receive messages sent to its multicast groups from the local host
-> m () |

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

## setMulticastTtl

socketSetMulticastTtl Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> Word32 | `ttl`: the time-to-live value for all multicast datagrams on `socket`
-> m () |

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

## setOption

socketSetOption Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`
-> Int32 | `level`: the "API level" of the option (eg, `SOL_SOCKET`)
-> Int32 | `optname`: the "name" of the option (eg, `SO_BROADCAST`)
-> Int32 | `value`: the value to set the option to
-> m () | (Can throw `GError`)

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

## setTimeout

socketSetTimeout Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> Word32 | `timeout`: the timeout for `socket`, in seconds, or 0 for none
-> m () |

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

On a non-blocking socket, calls to `socketConditionWait` will also fail with `IOErrorEnumTimedOut` 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 `socketReceive`, `socketSend`, `socketCheckConnectResult`, etc, will fail with `IOErrorEnumTimedOut`.

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

## setTtl

socketSetTtl Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`.
-> Word32 | `ttl`: the time-to-live value for all unicast packets on `socket`
-> m () |

Sets the time-to-live for outgoing unicast packets on `socket`. By default the platform-specific default value is used.

Since: 2.32

## shutdown

socketShutdown Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`
-> Bool | `shutdownRead`: whether to shut down the read side
-> Bool | `shutdownWrite`: whether to shut down the write side
-> m () | (Can throw `GError`)

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

If `shutdownRead` is `True` then the receiving side of the connection is shut down, and further reading is disallowed.

If `shutdownWrite` is `True` then the sending side of the connection is shut down, and further writing is disallowed.

It is allowed for both `shutdownRead` and `shutdownWrite` 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: 2.22

## speaksIpv4

socketSpeaksIpv4 Source #

Arguments

:: (HasCallStack, MonadIO m, IsSocket a) |
=> a | `socket`: a `Socket`
-> m Bool | Returns: `True` if this socket can be used with 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.

Since: 2.22

# Properties

## blocking

No description available in the introspection data.

constructSocketBlocking :: IsSocket o => Bool -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`blocking`” property. This is rarely needed directly, but it is used by `new`.

getSocketBlocking :: (MonadIO m, IsSocket o) => o -> m Bool Source #

Get the value of the “`blocking`” property. When overloading is enabled, this is equivalent to

    get socket #blocking

setSocketBlocking :: (MonadIO m, IsSocket o) => o -> Bool -> m () Source #

Set the value of the “`blocking`” property. When overloading is enabled, this is equivalent to

    set socket [ #blocking := value ]

## broadcast

Whether the socket should allow sending to broadcast addresses.

Since: 2.32

constructSocketBroadcast :: IsSocket o => Bool -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`broadcast`” property. This is rarely needed directly, but it is used by `new`.

getSocketBroadcast :: (MonadIO m, IsSocket o) => o -> m Bool Source #

Get the value of the “`broadcast`” property. When overloading is enabled, this is equivalent to

    get socket #broadcast

setSocketBroadcast :: (MonadIO m, IsSocket o) => o -> Bool -> m () Source #

Set the value of the “`broadcast`” property. When overloading is enabled, this is equivalent to

    set socket [ #broadcast := value ]

## family

No description available in the introspection data.

constructSocketFamily :: IsSocket o => SocketFamily -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`family`” property. This is rarely needed directly, but it is used by `new`.

getSocketFamily :: (MonadIO m, IsSocket o) => o -> m SocketFamily Source #

Get the value of the “`family`” property. When overloading is enabled, this is equivalent to

    get socket #family

## fd

No description available in the introspection data.

constructSocketFd :: IsSocket o => Int32 -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`fd`” property. This is rarely needed directly, but it is used by `new`.

getSocketFd :: (MonadIO m, IsSocket o) => o -> m Int32 Source #

Get the value of the “`fd`” property. When overloading is enabled, this is equivalent to

    get socket #fd

## keepalive

No description available in the introspection data.

constructSocketKeepalive :: IsSocket o => Bool -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`keepalive`” property. This is rarely needed directly, but it is used by `new`.

getSocketKeepalive :: (MonadIO m, IsSocket o) => o -> m Bool Source #

Get the value of the “`keepalive`” property. When overloading is enabled, this is equivalent to

    get socket #keepalive

setSocketKeepalive :: (MonadIO m, IsSocket o) => o -> Bool -> m () Source #

Set the value of the “`keepalive`” property. When overloading is enabled, this is equivalent to

    set socket [ #keepalive := value ]

## listenBacklog

No description available in the introspection data.

constructSocketListenBacklog :: IsSocket o => Int32 -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`listen-backlog`” property. This is rarely needed directly, but it is used by `new`.

getSocketListenBacklog :: (MonadIO m, IsSocket o) => o -> m Int32 Source #

Get the value of the “`listen-backlog`” property. When overloading is enabled, this is equivalent to

    get socket #listenBacklog

setSocketListenBacklog :: (MonadIO m, IsSocket o) => o -> Int32 -> m () Source #

Set the value of the “`listen-backlog`” property. When overloading is enabled, this is equivalent to

    set socket [ #listenBacklog := value ]

## localAddress

No description available in the introspection data.

getSocketLocalAddress :: (MonadIO m, IsSocket o) => o -> m (Maybe SocketAddress) Source #

Get the value of the “`local-address`” property. When overloading is enabled, this is equivalent to

    get socket #localAddress

## multicastLoopback

Whether outgoing multicast packets loop back to the local host.

Since: 2.32

constructSocketMulticastLoopback :: IsSocket o => Bool -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`multicast-loopback`” property. This is rarely needed directly, but it is used by `new`.

getSocketMulticastLoopback :: (MonadIO m, IsSocket o) => o -> m Bool Source #

Get the value of the “`multicast-loopback`” property. When overloading is enabled, this is equivalent to

    get socket #multicastLoopback

setSocketMulticastLoopback :: (MonadIO m, IsSocket o) => o -> Bool -> m () Source #

Set the value of the “`multicast-loopback`” property. When overloading is enabled, this is equivalent to

    set socket [ #multicastLoopback := value ]

## multicastTtl

Time-to-live out outgoing multicast packets

Since: 2.32

constructSocketMulticastTtl :: IsSocket o => Word32 -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`multicast-ttl`” property. This is rarely needed directly, but it is used by `new`.

getSocketMulticastTtl :: (MonadIO m, IsSocket o) => o -> m Word32 Source #

Get the value of the “`multicast-ttl`” property. When overloading is enabled, this is equivalent to

    get socket #multicastTtl

setSocketMulticastTtl :: (MonadIO m, IsSocket o) => o -> Word32 -> m () Source #

Set the value of the “`multicast-ttl`” property. When overloading is enabled, this is equivalent to

    set socket [ #multicastTtl := value ]

## protocol

No description available in the introspection data.

constructSocketProtocol :: IsSocket o => SocketProtocol -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`protocol`” property. This is rarely needed directly, but it is used by `new`.

getSocketProtocol :: (MonadIO m, IsSocket o) => o -> m SocketProtocol Source #

Get the value of the “`protocol`” property. When overloading is enabled, this is equivalent to

    get socket #protocol

## remoteAddress

No description available in the introspection data.

getSocketRemoteAddress :: (MonadIO m, IsSocket o) => o -> m (Maybe SocketAddress) Source #

Get the value of the “`remote-address`” property. When overloading is enabled, this is equivalent to

    get socket #remoteAddress

## timeout

The timeout in seconds on socket I/O

Since: 2.26

constructSocketTimeout :: IsSocket o => Word32 -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`timeout`” property. This is rarely needed directly, but it is used by `new`.

getSocketTimeout :: (MonadIO m, IsSocket o) => o -> m Word32 Source #

Get the value of the “`timeout`” property. When overloading is enabled, this is equivalent to

    get socket #timeout

setSocketTimeout :: (MonadIO m, IsSocket o) => o -> Word32 -> m () Source #

Set the value of the “`timeout`” property. When overloading is enabled, this is equivalent to

    set socket [ #timeout := value ]

## ttl

Time-to-live for outgoing unicast packets

Since: 2.32

constructSocketTtl :: IsSocket o => Word32 -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`ttl`” property. This is rarely needed directly, but it is used by `new`.

getSocketTtl :: (MonadIO m, IsSocket o) => o -> m Word32 Source #

Get the value of the “`ttl`” property. When overloading is enabled, this is equivalent to

    get socket #ttl

setSocketTtl :: (MonadIO m, IsSocket o) => o -> Word32 -> m () Source #

Set the value of the “`ttl`” property. When overloading is enabled, this is equivalent to

    set socket [ #ttl := value ]

## type

No description available in the introspection data.

constructSocketType :: IsSocket o => SocketType -> IO (GValueConstruct o) Source #

Construct a `GValueConstruct` with valid value for the “`type`” property. This is rarely needed directly, but it is used by `new`.

getSocketType :: (MonadIO m, IsSocket o) => o -> m SocketType Source #

Get the value of the “`type`” property. When overloading is enabled, this is equivalent to

    get socket #type