Hash2Pub/democlient.hs

28 lines
1,014 B
Haskell

{-# LANGUAGE OverloadedStrings #-}
module Main where
import Network.Socket hiding (send, sendTo, recv, recvFrom)
import Network.Socket.ByteString
import Control.Monad (forever)
import System.IO (
IOMode (ReadWriteMode)
)
import qualified Data.ByteString as BS
import qualified Data.ByteString.UTF8 as BSU
main = do
sock <- socket AF_INET6 Datagram defaultProtocol
setSocketOption sock IPv6Only 1
-- lookup destination to get a SockAddr
let hints = defaultHints { addrFamily = AF_INET6, addrSocketType = Datagram}
destAddr <- addrAddress . head <$> getAddrInfo (Just hints) (Just "::1") (Just "7331")
forever $ do
inp <- getLine
sendTo sock (BSU.fromString inp) destAddr
print "Yeah, sent sth."
-- important: as the socket is unconnected, it also accepts replies from a different source port
(msg, fromAddr) <- recvFrom sock 65535
putStrLn $ "Woop, received " ++ show msg ++ " from " ++ show fromAddr
return ()