start prototyping a UDP socket server, ideas on UDP parsing

- start implementing a socket-based UDP server (cannot reply yet)
- some brainstorming about how to parse incoming UDP packet commands
This commit is contained in:
Trolli Schmittlauch 2020-04-04 21:27:34 +02:00
parent 76e7c60b96
commit 303a6ecbfa
3 changed files with 36 additions and 0 deletions

2
Hash2Pub/.gitignore vendored
View file

@ -2,3 +2,5 @@ dist-newstyle/
*.local
*.swp
*.swo
*.o
*.hi

BIN
Hash2Pub/demoserver Executable file

Binary file not shown.

34
Hash2Pub/demoserver.hs Normal file
View file

@ -0,0 +1,34 @@
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Network.Socket hiding (sendTo, recvFrom)
import Network.Socket.ByteString
import Control.Concurrent
import Control.Exception
import Control.Monad (forever)
import System.IO (
IOMode (ReadWriteMode)
, hPutStrLn
)
import qualified Data.ByteString as BS
main = do
sock <- socket AF_INET6 Datagram defaultProtocol
let hints = defaultHints { addrFamily = AF_INET6, addrSocketType = Datagram}
addrInfos <- getAddrInfo (Just hints) (Just "::1") (Just "7331")
let serverAddr = addrAddress . head $ addrInfos
print serverAddr
bind sock serverAddr
print sock
forever $ do
receivedStuff <- recvFrom sock 65535 -- blocks
forkIO $ bracket newSendSocket close' (serveReceive receivedStuff)
return ()
serveReceive (msg, fromAddr) sendSocket = do
putStrLn $ "Got message " ++ show msg ++ " from " ++ show fromAddr
sendTo sendSocket ("Hi, thx for " `BS.append` msg) fromAddr
return ()
newSendSocket :: IO Socket
newSendSocket = socket AF_INET6 Datagram defaultProtocol