2019-07-24 11:08:59 +03:00
|
|
|
module Main
|
|
|
|
|
2019-07-25 10:43:33 +03:00
|
|
|
import System
|
2019-07-24 11:08:59 +03:00
|
|
|
import Network.Socket
|
|
|
|
import Network.Socket.Data
|
|
|
|
import Network.Socket.Raw
|
|
|
|
|
2019-07-25 10:43:33 +03:00
|
|
|
%cg chez libidris_net.dylib
|
|
|
|
|
|
|
|
runServer : IO (Either String (Port, ThreadID))
|
2019-07-24 11:08:59 +03:00
|
|
|
runServer = do
|
|
|
|
osock <- socket AF_INET Stream 0
|
|
|
|
case osock of
|
|
|
|
Left fail => pure (Left $ "Failed to open socket: " ++ show fail)
|
|
|
|
Right sock => do
|
2019-07-25 10:43:33 +03:00
|
|
|
res <- bind sock (Just (Hostname "localhost")) 0
|
|
|
|
if res /= 0
|
|
|
|
then pure (Left $ "Failed to bind socket with error: " ++ show res)
|
|
|
|
else do
|
|
|
|
port <- getSockPort sock
|
|
|
|
res <- listen sock
|
|
|
|
if res /= 0
|
|
|
|
then pure (Left $ "Failed to listen on socket with error: " ++ show res)
|
|
|
|
else do
|
|
|
|
forked <- fork (serve port sock)
|
|
|
|
pure $ Right (port, forked)
|
2019-07-24 11:08:59 +03:00
|
|
|
|
2019-07-25 10:43:33 +03:00
|
|
|
where
|
|
|
|
serve : Port -> Socket -> IO ()
|
|
|
|
serve port sock = do
|
|
|
|
res <- accept sock
|
|
|
|
case res of
|
|
|
|
Left err => do
|
|
|
|
putStrLn ("Failed to accept on socket with error: " ++ show err)
|
|
|
|
Right (s, _) => do
|
|
|
|
received <- recv s 1024
|
|
|
|
case received of
|
|
|
|
Left err => do
|
|
|
|
putStrLn ("Failed to accept on socket with error: " ++ show err)
|
|
|
|
Right (str, _) => do
|
|
|
|
putStrLn ("Received: " ++ str)
|
|
|
|
sent <- send s ("echo: " ++ str)
|
|
|
|
case sent of
|
|
|
|
Left err => do
|
|
|
|
putStrLn ("Server failed to send data with error: " ++ show err)
|
|
|
|
Right n =>
|
|
|
|
putStrLn ("Server sent " ++ show n ++ " bytes")
|
2019-07-24 11:08:59 +03:00
|
|
|
|
|
|
|
runClient : Port -> IO ()
|
|
|
|
runClient serverPort = do
|
|
|
|
osock <- socket AF_INET Stream 0
|
|
|
|
case osock of
|
|
|
|
Left fail => putStrLn ("Failed to open socket: " ++ show fail)
|
|
|
|
Right sock => do
|
|
|
|
res <- connect sock (Hostname "localhost") serverPort
|
|
|
|
if res /= 0
|
2019-07-25 10:43:33 +03:00
|
|
|
then putStrLn ("Failed to connect client to port " ++ show serverPort ++ ": " ++ show res)
|
2019-07-24 11:08:59 +03:00
|
|
|
else do
|
|
|
|
sent <- send sock ("hello world!")
|
|
|
|
case sent of
|
|
|
|
Left err => do
|
|
|
|
putStrLn ("Client failed to send data with error: " ++ show err)
|
|
|
|
Right n => do
|
|
|
|
putStrLn ("Client sent " ++ show n ++ " bytes")
|
|
|
|
received <- recv sock 1024
|
|
|
|
case received of
|
|
|
|
Left err => do
|
|
|
|
putStrLn ("Client failed to receive on socket with error: " ++ show err)
|
|
|
|
Right (str, _) => do
|
|
|
|
putStrLn ("Received: " ++ str)
|
|
|
|
|
2019-07-25 10:43:33 +03:00
|
|
|
main : IO ()
|
|
|
|
main = do
|
|
|
|
server <- runServer
|
|
|
|
case server of
|
|
|
|
Left err => putStrLn $ "[server] " ++ err
|
|
|
|
Right (serverPort, tid) => do
|
|
|
|
runClient serverPort
|