Created
August 7, 2011 02:04
-
-
Save luciferous/1129979 to your computer and use it in GitHub Desktop.
Looping Echo
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
import Network (listenOn, PortID(..)) | |
import Network.Socket | |
import Control.Exception | |
import Control.Concurrent | |
import Control.Monad (forever) | |
import System.IO | |
type HandlerFn = String -> Socket -> IO () | |
loopEcho :: HandlerFn | |
loopEcho msg cs = do | |
forever $ do | |
send cs msg | |
threadDelay 1000000 | |
server :: PortID -> HandlerFn -> IO () | |
server port handler = do | |
mastersocket <- listenOn port | |
forever $ do | |
(cs, _) <- accept mastersocket | |
forkIO $ handler "hello" cs `finally` sClose cs | |
main = server (PortNumber 1234) loopEcho |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment