network-3.1.4.0: Low-level networking interface

Safe Haskell | Safe-Inferred
Language | Haskell2010

Network.Socket.Address

Contents

- Socket Address
- Socket operations
- Sending and receiving ByteString
- Sending and receiving data from a buffer
- Advanced IO

Description

This module provides extensible APIs for socket addresses.

Synopsis

- class SocketAddress sa where
  - sizeOfSocketAddress :: sa -> Int
  - peekSocketAddress :: Ptr sa -> IO sa
  - pokeSocketAddress :: Ptr a -> sa -> IO ()
- getPeerName :: SocketAddress sa => Socket -> IO sa
- getSocketName :: SocketAddress sa => Socket -> IO sa
- connect :: SocketAddress sa => Socket -> sa -> IO ()
- bind :: SocketAddress sa => Socket -> sa -> IO ()
- accept :: SocketAddress sa => Socket -> IO (Socket, sa)
- sendTo :: SocketAddress sa => Socket -> ByteString -> sa -> IO Int
- sendAllTo :: SocketAddress sa => Socket -> ByteString -> sa -> IO ()
- recvFrom :: SocketAddress sa => Socket -> Int -> IO (ByteString, sa)
- sendBufTo :: SocketAddress sa => Socket -> Ptr a -> Int -> sa -> IO Int
- recvBufFrom :: SocketAddress sa => Socket -> Ptr a -> Int -> IO (Int, sa)
- sendBufMsg :: SocketAddress sa => Socket -> sa -> [(Ptr Word8, Int)] -> [Cmsg] -> MsgFlag -> IO Int
- recvBufMsg :: SocketAddress sa => Socket -> [(Ptr Word8, Int)] -> Int -> MsgFlag -> IO (sa, Int, [Cmsg], MsgFlag)

# Socket Address

class SocketAddress sa where Source #

The core typeclass to unify socket addresses.

Methods

sizeOfSocketAddress :: sa -> Int Source #

peekSocketAddress :: Ptr sa -> IO sa Source #

pokeSocketAddress :: Ptr a -> sa -> IO () Source #

#### Instances

Instances details

SocketAddress SockAddr Source # |
Instance details Defined in Network.Socket.Types Methods sizeOfSocketAddress :: SockAddr -> Int Source # peekSocketAddress :: Ptr SockAddr -> IO SockAddr Source # pokeSocketAddress :: Ptr a -> SockAddr -> IO () Source #

getPeerName :: SocketAddress sa => Socket -> IO sa Source #

Getting peer's socket address.

getSocketName :: SocketAddress sa => Socket -> IO sa Source #

Getting my socket address.

# Socket operations

connect :: SocketAddress sa => Socket -> sa -> IO () Source #

Connect to a remote socket at address.

bind :: SocketAddress sa => Socket -> sa -> IO () Source #

Bind the socket to an address. The socket must not already be bound. The `Family` passed to `bind` must be the same as that passed to `socket`. If the special port number `defaultPort` is passed then the system assigns the next available use port.

accept :: SocketAddress sa => Socket -> IO (Socket, sa) Source #

Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair `(conn, address)` where `conn` is a new socket object usable to send and receive data on the connection, and `address` is the address bound to the socket on the other end of the connection. On Unix, FD_CLOEXEC is set to the new `Socket`.

# Sending and receiving ByteString

sendTo Source #

Arguments

:: SocketAddress sa |
=> Socket | Socket
-> ByteString | Data to send
-> sa | Recipient address
-> IO Int | Number of bytes sent

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.

sendAllTo Source #

Arguments

:: SocketAddress sa |
=> Socket | Socket
-> ByteString | Data to send
-> sa | Recipient address
-> 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.

recvFrom Source #

Arguments

:: SocketAddress sa |
=> Socket | Socket
-> Int | Maximum number of bytes to receive
-> IO (ByteString, sa) | Data received and sender address

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.

If the first return value is zero, it means EOF.

# Sending and receiving data from a buffer

sendBufTo :: SocketAddress sa => Socket -> Ptr a -> Int -> sa -> IO Int Source #

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.

recvBufFrom :: SocketAddress sa => Socket -> Ptr a -> Int -> IO (Int, sa) Source #

Receive data from the socket, writing it into buffer instead of creating a new string. The socket need not be in a connected state. Returns `(nbytes, address)` where `nbytes` is the number of bytes received and `address` is a `SockAddr` representing the address of the sending socket.

If the first return value is zero, it means EOF.

For `Stream` sockets, the second return value would be invalid.

NOTE: blocking on Windows unless you compile with -threaded (see GHC ticket #1129)

# Advanced IO

sendBufMsg Source #

Arguments

:: SocketAddress sa |
=> Socket | Socket
-> sa | Destination address
-> [(Ptr Word8, Int)] | Data to be sent
-> [Cmsg] | Control messages
-> MsgFlag | Message flags
-> IO Int | The length actually sent

Send data to the socket using sendmsg(2).

recvBufMsg Source #

Arguments

:: SocketAddress sa |
=> Socket | Socket
-> [(Ptr Word8, Int)] | A list of (buffer, buffer-length) pairs. If the total length is not large enough, `MSG_TRUNC` is returned
-> Int | The buffer size for control messages. If the length is not large enough, `MSG_CTRUNC` is returned
-> MsgFlag | Message flags
-> IO (sa, Int, [Cmsg], MsgFlag) | Source address, total bytes received, control messages and message flags

Receive data from the socket using recvmsg(2). The supplied buffers are filled in order, with subsequent buffers used only after all the preceding buffers are full. If the message is short enough some of the supplied buffers may remain unused.