Skip to content

Instantly share code, notes, and snippets.

@soupi
Created July 1, 2020 14:14
Show Gist options
  • Save soupi/1cc542a97b3e4c6cf0f7a577e8b4fb24 to your computer and use it in GitHub Desktop.
Save soupi/1cc542a97b3e4c6cf0f7a577e8b4fb24 to your computer and use it in GitHub Desktop.
Echo server/client haskell
module Main (main) where
import Control.Monad (forever, unless)
import qualified Data.ByteString.Char8 as C
import Network.Socket
import Network.Run.TCP (runTCPClient)
import Network.Socket.ByteString (recv, sendAll)
import System.IO (hFlush, stdout)
main :: IO ()
main =
runTCPClient "127.0.0.1" "8686" $ \s -> forever $ do
putStr "> "
hFlush stdout
line <- C.getLine
sendAll s line
msg <- recv s 1024
C.putStrLn msg
module Main (main) where
import Control.Monad (forever, unless)
import qualified Data.ByteString as S
import Network.Socket
import Network.Run.TCP (runTCPServer)
import Network.Socket.ByteString (recv, sendAll)
main :: IO ()
main =
runTCPServer Nothing "8686" (forever . talk)
where
talk s = do
msg <- recv s 1024
unless (S.null msg) $
sendAll s msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment