Chapter 4. The net category: networking support

Table of Contents
4.1. Network-interface toolkit—Socket and SocketPrim
4.2. URI

4.1. Network-interface toolkit—Socket and SocketPrim

(Darren Moffat supplied the initial version of this library.)

Your best bet for documentation is to look at the code—really!— normally in fptools/hslibs/net/{BSD,Socket,SocketPrim.lhs}.

The BSD module provides functions to get at system-database info; pretty straightforward if you're into this sort of thing:
getHostName         :: IO String

getServiceByName    :: ServiceName -> IO ServiceEntry
getServicePortNumber:: ServiceName -> IO PortNumber
getServiceEntry     :: IO ServiceEntry
setServiceEntry     :: Bool -> IO ()
endServiceEntry     :: IO ()

getProtocolByName   :: ProtocolName -> IO ProtocolEntry
getProtocolByNumber :: ProtocolNumber -> IO ProtcolEntry
getProtocolNumber   :: ProtocolName -> ProtocolNumber
getProtocolEntry    :: IO ProtocolEntry
setProtocolEntry    :: Bool -> IO ()
endProtocolEntry    :: IO ()

getHostByName       :: HostName -> IO HostEntry
getHostByAddr       :: Family -> HostAddress -> IO HostEntry
getHostEntry        :: IO HostEntry
setHostEntry        :: Bool -> IO ()
endHostEntry        :: IO ()

The SocketPrim interface provides quite direct access to the socket facilities in a BSD Unix system, including all the complications. We hope you don't need to use it! See the source if needed…

The Socket interface is a ``higher-level'' interface to sockets, and it is what we recommend. Please tell us if the facilities it offers are inadequate to your task!

The interface is relatively modest:
connectTo       :: Hostname -> PortID -> IO Handle
listenOn        :: PortID -> IO Socket

accept          :: Socket -> IO (Handle, HostName)
sendTo          :: Hostname -> PortID -> String -> IO ()

recvFrom        :: Hostname -> PortID -> IO String
socketPort      :: Socket -> IO PortID

data PortID     -- PortID is a non-abstract type
  = Service String        -- Service Name eg "ftp"
  | PortNumber PortNumber -- User defined Port Number
  | UnixSocket String     -- Unix family socket in file system

type Hostname = String

 -- 16-bit value (stored in network byte order).
data PortNumber
 -- instance of: Eq, Num, Show.

mkPortNumber :: Int -> PortNumber