Haskell logo CIS 552: Advanced Programming

Fall 2019

  • Home
  • Schedule
  • Homework
  • Resources
  • Style guide
  • Syllabus
Note: this is the stubbed version of module Client. You should download the lhs version of this module and replace all parts marked undefined. Eventually, the complete version will be made available.

Concurrency client

This module goes with the Concurrency lecture. It implements a network client that communicates with a server using sockets.

> module Client (local, client, send) where

This module uses the Network.Socket library, which provides basically the same interface to OS sockets as in C.

> import Network.Socket hiding (send)
> import System.IO
> -- | IP address of the local host
> local :: HostName
> local = "127.0.0.1"
> -- | Start the client given an IP address and a port. The port should
> -- be a string number > 1024
> client :: HostName -> ServiceName -> IO Handle
> client ip port = do
>   (sa:_) <- getAddrInfo Nothing (Just ip) (Just port)
>   sock   <- socket (addrFamily sa) Stream defaultProtocol
>   connect sock (addrAddress sa)
>   handle <- socketToHandle sock WriteMode
>   hSetBuffering handle (BlockBuffering Nothing)
>   return handle
> -- | send a message to the server
> send :: Handle -> String -> IO ()
> send h c = do
>   hPutStrLn h c
>   hFlush h
Design adapted from Minimalistic Design | Powered by Pandoc and Hakyll