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

Safe HaskellNone



Utility functions for TCP sockets



data ControlHeader Source #

Control headers



Tell the remote endpoint that we created a new connection


Tell the remote endpoint we will no longer be using a connection


Request to close the connection (see module description)


Sent by an endpoint when it is closed.


Message sent to probe a socket


Acknowledgement of the ProbeSocket message

data ConnectionRequestResponse Source #

Response sent by B to A when A tries to connect



B does not support the protocol version requested by A.


B accepts the connection


A requested an invalid endpoint


As request crossed with a request from B (see protocols)


A gave an incorrect host (did not match the host that B observed).

forkServer Source #


:: HostName


-> ServiceName


-> Int

Backlog (maximum number of queued connections)

-> Bool

Set ReuseAddr option?

-> (SomeException -> IO ())

Error handler. Called with an exception raised when accepting a connection.

-> (SomeException -> IO ())

Termination handler. Called when the error handler throws an exception.

-> (IO () -> (Socket, SockAddr) -> IO ())

Request handler. Gets an action which completes when the socket is closed.

-> IO (ServiceName, 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 in another thread. If any exception occurs the accepting thread terminates and calls the terminationHandler. Threads spawned for previous accepted connections are not killed. This exception may occur because of a call to accept, or because the thread was explicitly killed.

The request handler is not responsible for closing the socket. It will be closed once that handler returns. Take care to ensure that the socket is not used after the handler returns, or you will get undefined behavior (the file descriptor may be re-used).

The return value includes the port was bound to. This is not always the same port as that given in the argument. For example, binding to port 0 actually binds to a random port, selected by the OS.

recvWithLength :: Word32 -> Socket -> IO [ByteString] Source #

Read a length and then a payload of that length, subject to a limit on the length. If the length (first Word32 received) is greater than the limit then an exception is thrown.

recvExact Source #


:: Socket

Socket to read from

-> Word32

Number of bytes to read

-> IO [ByteString]

Data read

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

recvWord32 :: Socket -> IO Word32 Source #

Receive a 32-bit unsigned integer

encodeWord32 :: Word32 -> ByteString #

Serialize 32-bit to network byte order

tryCloseSocket :: Socket -> IO () Source #

Close a socket, ignoring I/O exceptions.

tryShutdownSocketBoth :: Socket -> IO () Source #

Shutdown socket sends and receives, ignoring I/O exceptions.

decodeSockAddr :: SockAddr -> IO (Maybe (HostName, ServiceName)) Source #

Produce a HostName and ServiceName from a SockAddr. Only gives Just for IPv4 addresses.

type EndPointId = Word32 Source #

Local identifier for an endpoint within this transport

type ProtocolVersion = Word32 Source #

Identifies the version of the network-transport-tcp protocol. It's the first piece of data sent when a new heavyweight connection is established.