Created
July 22, 2011 20:55
-
-
Save kreed131/1100407 to your computer and use it in GitHub Desktop.
Simple TCP client on Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
-- kreed131.blogspot.com/2011/07/tcp.html | |
--- | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fails to connect, can be fixed by replacing
"localhost"
with"127.0.0.1"
.