network-udp-0.0.0: UDP library
Safe HaskellSafe-Inferred
LanguageHaskell2010

Network.UDP

Description

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

Sockets used by clients and servers after accept

data UDPSocket Source #

A UDP socket which are used with recv and send.

Constructors

UDPSocket 

Fields

Instances

Instances details
Show UDPSocket Source # 
Instance details

Defined in Network.UDP

Eq UDPSocket Source # 
Instance details

Defined in Network.UDP

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.

Constructors

ListenSocket 

Fields

Instances

Instances details
Show ListenSocket Source # 
Instance details

Defined in Network.UDP

Eq ListenSocket Source # 
Instance details

Defined in Network.UDP

serverSocket :: (IP, PortNumber) -> IO ListenSocket Source #

Creating a listening UDP socket.

data ClientSockAddr Source #

A client socket address from the server point of view.

Constructors

ClientSockAddr SockAddr [Cmsg] 

Instances

Instances details
Show ClientSockAddr Source # 
Instance details

Defined in Network.UDP

Eq ClientSockAddr Source # 
Instance details

Defined in Network.UDP

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.

close :: UDPSocket -> IO () Source #

Closing a socket.

Misc

natRebinding :: UDPSocket -> IO UDPSocket Source #

Emulation of NAT rebiding in the client side. This is mainly used for test purposes.