Haskell Network Programming - UDP Client and Server
Using the network package we can build a low level UDP server and client.
import Control.Concurrent (forkIO, threadDelay) import Control.Monad (forever) import qualified Data.ByteString.Char8 as C import Network.Socket hiding (recv) import Network.Socket.ByteString (recv, sendAll) runUDPServer :: IO () runUDPServer = do addrinfos <- getAddrInfo Nothing (Just "127.0.0.1") (Just "7000") let serveraddr = head addrinfos
It is important to remember to use
Datagram to receive data via UDP. Then we can bind the socket to the address and wait to receive data.
The client code is similar. We need to sned data via
Datagram or the server will ignore it. Then we run
sendAll with a
ByteString and the server will receive the message.
We run the server in a separate thread because
recv is blocking. We add a few
threadDelays to make sure that the server has started up and that the
main terminates all of the other threads in the program will terminate as well .
A real world UDP server will likely need to constantly receive requests. We can change the last line to use
forever. It will repeatedly process incoming data.