Safe Haskell | None |
---|---|
Language | Haskell2010 |
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)
Socket Address
class SocketAddress sa where Source #
The core typeclass to unify socket addresses.
sizeOfSocketAddress :: sa -> Int Source #
peekSocketAddress :: Ptr sa -> IO sa Source #
pokeSocketAddress :: Ptr a -> sa -> IO () Source #
Instances
SocketAddress SockAddr Source # | |
Defined in Network.Socket.Types |
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
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
:: 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.
Sending data to closed socket may lead to undefined behaviour.
:: 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.
Sending data to closed socket may lead to undefined behaviour.
:: 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.
Receiving data from closed socket may lead to undefined behaviour.
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)