Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple TCP client on Haskell
import Network.Socket hiding (send, sendTo, recv, recvFrom)
import Network.Socket.ByteString (send, recv)
import qualified Data.ByteString.Char8 as B8
import System.Environment (getArgs)
main :: IO ()
main = do
[host', port'] <- getArgs
client host' (read port' :: Int)
client :: String -> Int -> IO ()
client host port = withSocketsDo $ do
addrInfo <- getAddrInfo Nothing (Just host) (Just $ show port)
let serverAddr = head addrInfo
sock <- socket (addrFamily serverAddr) Stream defaultProtocol
connect sock (addrAddress serverAddr)
msgSender sock
sClose sock
msgSender :: Socket -> IO ()
msgSender sock = do
msg <- B8.getLine
send sock msg
rMsg <- recv sock 10
B8.putStrLn rMsg
if msg == B8.pack "q" then putStrLn "Disconnected!" else msgSender sock

This comment has been minimized.

Copy link

@ghost ghost commented Jun 10, 2016

Fails to connect, can be fixed by replacing "localhost" with "".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment