Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Best current practice library for UDP clients and servers.
- Efficient receiving function without memory copy
- Proper buffer size
- Type-safe APIs
- TCP-like APIs (creating a UDP connection from a listing socket) in the server side
- Auto migration (network interface selection) in the client side
The recv
family in
Network.Socket.ByteString uses createAndTrim
internaly. So, one buffer is allocated before corresponding
system calls are called. Then another buffer is allocated
according to the input size and the input is copied.
Receiving functions provided by this library uses createUptoN
to avoid the memory copy.
Recent application protocols are designed to avoid IP
fragmentation. So, the UDP payload size is never over 1,500.
This library uses 2,048 for the buffer size. This size ensures
no global locking when allocating ByteString
(i.e. a buffer).
To know the background of TCP-like API in the server side, see:
To know the background of auto migration in the client side, see:
Synopsis
- data UDPSocket = UDPSocket {}
- clientSocket :: HostName -> ServiceName -> Bool -> IO UDPSocket
- recv :: UDPSocket -> IO ByteString
- recvBuf :: UDPSocket -> Ptr Word8 -> Int -> IO Int
- send :: UDPSocket -> ByteString -> IO ()
- sendBuf :: UDPSocket -> Ptr Word8 -> Int -> IO ()
- data ListenSocket = ListenSocket {
- listenSocket :: Socket
- mySockAddr :: SockAddr
- wildcard :: Bool
- serverSocket :: (IP, PortNumber) -> IO ListenSocket
- data ClientSockAddr = ClientSockAddr SockAddr [Cmsg]
- recvFrom :: ListenSocket -> IO (ByteString, ClientSockAddr)
- sendTo :: ListenSocket -> ByteString -> ClientSockAddr -> IO ()
- accept :: ListenSocket -> ClientSockAddr -> IO UDPSocket
- stop :: ListenSocket -> IO ()
- close :: UDPSocket -> IO ()
- natRebinding :: UDPSocket -> IO UDPSocket
Sockets used by clients and servers after accept
clientSocket :: HostName -> ServiceName -> Bool -> IO UDPSocket Source #
Creating a unconnected UDP socket.
recv :: UDPSocket -> IO ByteString Source #
Receiving data with a UDP socket. If the socket is connected, recv() is called. Otherwise, recvfrom() is called.
recvBuf :: UDPSocket -> Ptr Word8 -> Int -> IO Int Source #
Receiving data in a buffer with a UDP socket. If the socket is connected, recv() is called. Otherwise, recvfrom() is called.
send :: UDPSocket -> ByteString -> IO () Source #
Sending data with a UDP socket. If the socket is connected, send() is called. Otherwise, sento() is called.
sendBuf :: UDPSocket -> Ptr Word8 -> Int -> IO () Source #
Sending data in a buffer with a UDP socket. If the socket is connected, send() is called. Otherwise, sento() is called.
Server's wildcard socket
data ListenSocket Source #
A listening socket for UDP which can be used
for recvFrom
and sendTo
.
Optionally, a connected UDP socket can be created
with accept
as an emulation of TCP.
ListenSocket | |
|
Instances
Show ListenSocket Source # | |
Defined in Network.UDP showsPrec :: Int -> ListenSocket -> ShowS # show :: ListenSocket -> String # showList :: [ListenSocket] -> ShowS # | |
Eq ListenSocket Source # | |
Defined in Network.UDP (==) :: ListenSocket -> ListenSocket -> Bool # (/=) :: ListenSocket -> ListenSocket -> Bool # |
serverSocket :: (IP, PortNumber) -> IO ListenSocket Source #
Creating a listening UDP socket.
data ClientSockAddr Source #
A client socket address from the server point of view.
Instances
Show ClientSockAddr Source # | |
Defined in Network.UDP showsPrec :: Int -> ClientSockAddr -> ShowS # show :: ClientSockAddr -> String # showList :: [ClientSockAddr] -> ShowS # | |
Eq ClientSockAddr Source # | |
Defined in Network.UDP (==) :: ClientSockAddr -> ClientSockAddr -> Bool # (/=) :: ClientSockAddr -> ClientSockAddr -> Bool # |
recvFrom :: ListenSocket -> IO (ByteString, ClientSockAddr) Source #
Receiving data with a listening UDP socket. For a wildcard socket, recvmsg() is called. For an interface specific socket, recvfrom() is called.
sendTo :: ListenSocket -> ByteString -> ClientSockAddr -> IO () Source #
Sending data with a listening UDP socket. For a wildcard socket, sendmsg() is called. For an interface specific socket, sento() is called.
Server's connected socket
accept :: ListenSocket -> ClientSockAddr -> IO UDPSocket Source #
Creating a connected UDP socket like TCP's accept().
Closing
stop :: ListenSocket -> IO () Source #
Closing a socket.