Go to the first, previous, next, last section, table of contents.

Network-interface toolkit -- `Socket' and `SocketPrim'

(Darren Moffat supplied the network-interface toolkit.)

Your best bet for documentation is to look at the code -- really! -- normally in `ghc/lib/ghc/{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 Int      -- User defined Port Number
  | UnixSocket String   -- Unix family socket in file system

type Hostname = String

Various examples of networking Haskell code are provided in `ghc/misc/examples/', notably the `net???/Main.hs' programs.

Go to the first, previous, next, last section, table of contents.