-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUdp.hs
More file actions
31 lines (24 loc) · 873 Bytes
/
Udp.hs
File metadata and controls
31 lines (24 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Udp where
import Network.Socket
-- | run UDP-Server on random port
initUdpServer :: IO (Socket, PortNumber)
initUdpServer = withSocketsDo $ do
addrinfos <- getAddrInfo (Just (defaultHints {addrFlags = [AI_PASSIVE]}))
Nothing (Just "0")
let serveraddr = head addrinfos
sock <- socket (addrFamily serveraddr) Datagram defaultProtocol
bindSocket sock (addrAddress serveraddr)
port <- socketPort sock
return (sock, port)
-- | wait for UDP-Packet
receiveUdp :: Socket -> IO String
receiveUdp sock = do
(msg, _, addr) <- recvFrom sock 1024
return msg
-- | send UDP-Packet to ip:port via a socket
sendUdp :: Socket -> String -> Integer -> String -> IO ()
sendUdp sock ip port str = do
host <- inet_addr ip
sendTo sock str (SockAddrInet (fromInteger port) host)
return ()
-- vim: sw=4 expandtab