Module Socket

Library to support network programming with sockets. In standard applications, the server side uses the operations listenOn and socketAccept to provide some service on a socket, and the client side uses the operation connectToSocket to request a service.

Author: Michael Hanus

Version: February 2008

Summary of exported operations:

listenOn :: Int -> IO Socket   
Creates a server side socket bound to a given port number.
listenOnFresh :: IO (Int,Socket)   
Creates a server side socket bound to a free port.
socketAccept :: Socket -> IO (String,Handle)   
Returns a connection of a client to a socket.
waitForSocketAccept :: Socket -> Int -> IO (Maybe (String,Handle))   
Waits until a connection of a client to a socket is available.
sClose :: Socket -> IO ()   
Closes a server socket.
connectToSocket :: String -> Int -> IO Handle   
Creates a new connection to a Unix socket.

Exported datatypes:


Socket

Constructors:


Exported operations:

listenOn :: Int -> IO Socket   

Creates a server side socket bound to a given port number.

listenOnFresh :: IO (Int,Socket)   

Creates a server side socket bound to a free port. The port number and the socket is returned.

Further infos:
  • externally defined

socketAccept :: Socket -> IO (String,Handle)   

Returns a connection of a client to a socket. The connection is returned as a pair consisting of a string identifying the client (the format of this string is implementation-dependent) and a handle to a stream communication with the client. The handle is both readable and writable.

waitForSocketAccept :: Socket -> Int -> IO (Maybe (String,Handle))   

Waits until a connection of a client to a socket is available. If no connection is available within the time limit, it returns Nothing, otherwise the connection is returned as a pair consisting of a string identifying the client (the format of this string is implementation-dependent) and a handle to a stream communication with the client.

Example call:
(waitForSocketAccept socket timeout)
Parameters:
  • socket : a socket
  • timeout : milliseconds to wait for input (< 0 : no time out)

sClose :: Socket -> IO ()   

Closes a server socket.

connectToSocket :: String -> Int -> IO Handle   

Creates a new connection to a Unix socket.

Example call:
(connectToSocket host port)
Parameters:
  • host : the host name of the connection
  • port : the port number of the connection
Returns:
the handle of the stream (connected to the port port@host) which is both readable and writable