HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulenetwork-3.2.8.0Haskell2010

Network.Socket.ByteString

This module provides access to the BSD socket interface. For detailed documentation, consult your favorite POSIX socket reference. All functions communicate failures by converting the error number to an System.IO.Error.IOError.

This module is made to be imported with Network.Socket like so:

import Network.Socket
import Network.Socket.ByteString
  • 11 values
  • Packagenetwork-3.2.8.0
  • Exports11
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceByteString.hs

Send data to a socket

4 declarations
valuesend
  1. :: Socket

    Connected socket

  2. -> ByteString

    Data to send

  3. -> IO Int

    Number of bytes sent

#

Send data to the socket. The socket must be connected to a remote socket. Returns the number of bytes sent. Applications are responsible for ensuring that all data has been sent.

valuesendAll
  1. :: Socket

    Connected socket

  2. -> ByteString

    Data to send

  3. -> IO ()
#

Send data to the socket. The socket must be connected to a remote socket. Unlike send, this function continues to send data until either all data has been sent or an error occurs. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully sent.

valuesendTo :: Socket -> ByteString -> SockAddr -> IO Int
#

Send data to the socket. The recipient can be specified explicitly, so the socket need not be in a connected state. Returns the number of bytes sent. Applications are responsible for ensuring that all data has been sent.

valuesendAllTo :: Socket -> ByteString -> SockAddr -> IO ()
#

Send data to the socket. The recipient can be specified explicitly, so the socket need not be in a connected state. Unlike sendTo, this function continues to send data until either all data has been sent or an error occurs. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully sent.

Vectored I/O

Vectored I/O, also known as scatter/gather I/O, allows multiple data segments to be sent using a single system call, without first concatenating the segments. For example, given a list of ByteStrings, xs,

sendMany sock xs

is equivalent to

sendAll sock (concat xs)

but potentially more efficient.

Vectored I/O are often useful when implementing network protocols that, for example, group data into segments consisting of one or more fixed-length headers followed by a variable-length body.

valuesendMany
  1. :: Socket

    Connected socket

  2. -> [ByteString]

    Data to send

  3. -> IO ()
#

Send data to the socket. The socket must be in a connected state. The data is sent as if the parts have been concatenated. This function continues to send data until either all data has been sent or an error occurs. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully sent.

valuesendManyTo
  1. :: Socket

    Socket

  2. -> [ByteString]

    Data to send

  3. -> SockAddr

    Recipient address

  4. -> IO ()
#

Send data to the socket. The recipient can be specified explicitly, so the socket need not be in a connected state. The data is sent as if the parts have been concatenated. This function continues to send data until either all data has been sent or an error occurs. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully sent.

valuesendManyWithFds
  1. :: Socket

    Socket

  2. -> [ByteString]

    Data to send

  3. -> [Fd]

    File descriptors

  4. -> IO ()
#

Send data and file descriptors over a UNIX-domain socket in a single system call. This function does not work on Windows.

Receive data from a socket

2 declarations
valuerecv
  1. :: Socket

    Connected socket

  2. -> Int

    Maximum number of bytes to receive

  3. -> IO ByteString

    Data received

#

Receive data from the socket. The socket must be in a connected state. This function may return fewer bytes than specified. If the message is longer than the specified length, it may be discarded depending on the type of socket. This function may block until a message arrives.

Considering hardware and network realities, the maximum number of bytes to receive should be a small power of 2, e.g., 4096.

For TCP sockets, a zero length return value means the peer has closed its half side of the connection.

Currently, the recv family is blocked on Windows because a proper IO manager is not implemented. To use with timeout on Windows, use setSocketOption with RecvTimeOut as well.

valuerecvFrom :: Socket -> Int -> IO (ByteString, SockAddr)
#

Receive data from the socket. The socket need not be in a connected state. Returns (bytes, address) where bytes is a ByteString representing the data received and address is a SockAddr representing the address of the sending socket.

Advanced send and recv

2 declarations
valuerecvMsg
  1. :: Socket

    Socket

  2. -> Int

    The maximum length of data to be received If the total length is not large enough, MSG_TRUNC is returned

  3. -> Int

    The buffer size for control messages. If the length is not large enough, MSG_CTRUNC is returned

  4. -> MsgFlag

    Message flags

  5. -> IO (SockAddr, ByteString, [Cmsg], MsgFlag)

    Source address, received data, control messages and message flags

#

Receive data from the socket using recvmsg(2).