some tweaks to the demo UDP server
This commit is contained in:
parent
303a6ecbfa
commit
6958fc2f98
|
@ -1,7 +1,8 @@
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import Network.Socket hiding (sendTo, recvFrom)
|
import Network.Socket hiding (send, sendTo, recv, recvFrom)
|
||||||
import Network.Socket.ByteString
|
import Network.Socket.ByteString
|
||||||
import Control.Concurrent
|
import Control.Concurrent
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
|
@ -15,19 +16,20 @@ import qualified Data.ByteString as BS
|
||||||
main = do
|
main = do
|
||||||
sock <- socket AF_INET6 Datagram defaultProtocol
|
sock <- socket AF_INET6 Datagram defaultProtocol
|
||||||
let hints = defaultHints { addrFamily = AF_INET6, addrSocketType = Datagram}
|
let hints = defaultHints { addrFamily = AF_INET6, addrSocketType = Datagram}
|
||||||
addrInfos <- getAddrInfo (Just hints) (Just "::1") (Just "7331")
|
serverAddr <- addrAddress . head <$> getAddrInfo (Just hints) (Just "::1") (Just "7331")
|
||||||
let serverAddr = addrAddress . head $ addrInfos
|
|
||||||
print serverAddr
|
print serverAddr
|
||||||
bind sock serverAddr
|
bind sock serverAddr
|
||||||
print sock
|
print sock
|
||||||
forever $ do
|
forever $ do
|
||||||
receivedStuff <- recvFrom sock 65535 -- blocks
|
receivedStuff <- recvFrom sock 65535 -- blocks
|
||||||
forkIO $ bracket newSendSocket close' (serveReceive receivedStuff)
|
forkIO $ bracket newSendSocket close (serveReceive receivedStuff)
|
||||||
return ()
|
|
||||||
|
|
||||||
|
serveReceive :: (BS.ByteString, SockAddr) -> Socket -> IO ()
|
||||||
serveReceive (msg, fromAddr) sendSocket = do
|
serveReceive (msg, fromAddr) sendSocket = do
|
||||||
|
print sendSocket
|
||||||
putStrLn $ "Got message " ++ show msg ++ " from " ++ show fromAddr
|
putStrLn $ "Got message " ++ show msg ++ " from " ++ show fromAddr
|
||||||
sendTo sendSocket ("Hi, thx for " `BS.append` msg) fromAddr
|
sentBytes <- sendTo sendSocket ("Hi, thx for " `BS.append` msg) fromAddr
|
||||||
|
putStrLn $ "sent response of " ++ show sentBytes ++ "bytes"
|
||||||
return ()
|
return ()
|
||||||
|
|
||||||
newSendSocket :: IO Socket
|
newSendSocket :: IO Socket
|
||||||
|
|
Loading…
Reference in a new issue