[Haskell-cafe] UDP

Andrew Coppin andrewcoppin at btinternet.com
Sat Jan 31 16:48:16 EST 2009


John Van Enk wrote:
> Try something like this:
>
> module Main where
>
> import Network.Socket
>
> main = withSocketsDo $ do
>     -- Make a UDP socket
>     s <- socket AF_INET Datagram defaultProtocol
>
>     -- We want to listen on all interfaces (0.0.0.0)
>     bindAddr <- inet_addr "0.0.0.0"
>
>     -- Bind to 0.0.0.0:30000 <http://0.0.0.0:30000>
>     bindSocket s (SockAddrInet 30000 bindAddr)
>
>     -- Read a message of max length 1000 from some one
>     (msg,len,from) <- recvFrom s 1000
>
>     putStrLn $ "Got the following message from " ++ (show from)
>     putStrLn msg
>
> Does this help? As Stephan said, you missed the bind step.

That works great, thanks.

Yeah, I just assumed that the bind step was only necessary for 
connection-oriented protocols. (Interestingly enough, the matching 
"send" program doesn't bind at all, yet seems to work fine...)



More information about the Haskell-Cafe mailing list