Created
July 22, 2011 20:16
-
-
Save kreed131/1100325 to your computer and use it in GitHub Desktop.
Simple TCP Server 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-haskell-networksocket.html | |
--- | |
import Network.Socket hiding (send, recv) | |
import Network.Socket.ByteString | |
import Control.Concurrent (forkIO) | |
import qualified Data.ByteString.Char8 as B8 | |
import System.Environment (getArgs) | |
main :: IO () | |
main = do | |
[port'] <- getArgs | |
server (fromIntegral $ read port') | |
server :: PortNumber -> IO () | |
server port = withSocketsDo $ do | |
sock <- socket AF_INET Stream defaultProtocol | |
bindSocket sock (SockAddrInet port 0) | |
listen sock 5 | |
sockHandler sock | |
sClose sock | |
sockHandler :: Socket -> IO () | |
sockHandler sock = do | |
(sockh, _) <- accept sock | |
forkIO $ putStrLn "Client connected!" >> receiveMessage sockh | |
sockHandler sock | |
receiveMessage :: Socket -> IO () | |
receiveMessage sockh = do | |
msg <- recv sockh 10 | |
B8.putStrLn msg | |
if msg == B8.pack "q" || B8.null msg | |
then sClose sockh >> putStrLn "Client disconnected" | |
else receiveMessage sockh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment