[Haskell-cafe] windows network programming

Nils Schweinsberg ml at n-sch.de
Tue Nov 2 16:24:21 EDT 2010


Am 02.11.2010 19:57, schrieb Michael Litchard:
> got any urls with examples?

Sure, see this short server-client-ping-pong application.

By the way, I noticed that you don't need withSocketsDo on windows 7, 
but I guess it's there for a reason for older windows versions. :)



     import Control.Concurrent
     import Network
     import System.IO

     main :: IO ()
     main = withSocketsDo $ do
         forkIO waitAndPong
         ping

     -- The basic server
     waitAndPong :: IO ()
     waitAndPong = do
         socket <- listenOn (PortNumber 1234)
         (handle,_,_) <- accept socket
         hSetBuffering handle LineBuffering
         incoming <- hGetLine handle
         putStrLn ("> " ++ incoming)
         hPutStrLn handle "pong"

     -- The basic client
     ping :: IO ()
     ping = do
         handle <- connectTo "localhost" (PortNumber 1234)
         hSetBuffering handle LineBuffering
         hPutStrLn handle "ping"
         incoming <- hGetLine handle
         putStrLn ("< " ++ incoming)


More information about the Haskell-Cafe mailing list