Haskell logo CIS 5520: Advanced Programming

Fall 2024

  • Home
  • Schedule
  • Homework
  • Resources
  • Software
  • Style guide
Note: this is the stubbed version of module Client. Try to figure out how to fill in all parts of this file marked undefined. CIS 5520 students should be able to access this code through github. Eventually, the completed version will be 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