Module Network.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: December 2018

Summary of exported operations:

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

Exported datatypes:


Socket

Constructors:


Exported operations:

listenOn :: Int -> IO Socket  Deterministic 

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

listenOnFresh :: IO (Int,Socket)  Deterministic 

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

Further infos:
  • externally defined

accept :: Socket -> IO (String,Handle)  Deterministic 

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. Only IPv4 connections are currently possible.

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

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)

close :: Socket -> IO ()  Deterministic 

Closes a server socket.

connectToSocket :: String -> Int -> IO Handle  Deterministic 

Creates a new connection to a Unix socket. Only IPv4 connections are currently possible.

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