network-transport-tcp-0.3.1: TCP instantiation of Network.Transport

Safe HaskellNone



Utility functions for TCP sockets





:: HostName


-> ServiceName


-> Int

Backlog (maximum number of queued connections)

-> Bool

Set ReuseAddr option?

-> (SomeException -> IO ())

Termination handler

-> (Socket -> IO ())

Request handler

-> IO ThreadId 

Start a server at the specified address.

This sets up a server socket for the specified host and port. Exceptions thrown during setup are not caught.

Once the socket is created we spawn a new thread which repeatedly accepts incoming connections and executes the given request handler. If any exception occurs the thread terminates and calls the terminationHandler. This exception may occur because of a call to accept, because the thread was explicitly killed, or because of a synchronous exception thrown by the request handler. Typically, you should avoid the last case by catching any relevant exceptions in the request handler.

The request handler should spawn threads to handle each individual request or the server will block. Once a thread has been spawned it will be the responsibility of the new thread to close the socket when an exception occurs.

recvWithLength :: Socket -> IO [ByteString]Source

Read a length and then a payload of that length



:: Socket

Socket to read from

-> Int32

Number of bytes to read

-> IO [ByteString] 

Read an exact number of bytes from a socket

Throws an I/O exception if the socket closes before the specified number of bytes could be read

recvInt32 :: Num a => Socket -> IO aSource

Receive a 32-bit integer

tryCloseSocket :: Socket -> IO ()Source

Close a socket, ignoring I/O exceptions