Tcp supports connection to
inet sockets and
unix sockets. These are two
different types. We use
'a where_to_connect to specify a socket to connect to,
'a identifies the type of socket.
with_connection ~host ~port f looks up host from a string (using DNS as needed),
connects, then calls
f, passing the connected socket and a reader and writer for it.
When the deferred returned by
f is determined, or any exception is thrown, the
socket, reader and writer are closed. The return deferred is fulfilled after f has
finished processing and the file descriptor for the socket is closed. If
is supplied then the connection attempt will be aborted if interrupt is fulfilled
before the connection has been established. Similarly, all connection attempts have a
timeout (default 30s), that can be overridden with
It is fine for
f to ignore the supplied socket and just use the reader and writer.
The socket is there to make it convenient to call
connect_sock ~host ~port opens a TCP connection to the specified hostname
and port, returning the socket.
Any errors in the connection will be reported to the monitor that was current when connect was called.
connect ~host ~port is a convenience wrapper around
connect_sock that returns the
socket, and a reader and writer for the socket. The reader and writer share a file
descriptor, and so closing one will affect the other by closing its underlying fd. In
particular, closing the reader before closing the writer will cause the writer to
subsequently raise an exception when it attempts to flush internally-buffered bytes to
the OS, due to a closed fd. You should close the
Writer first to avoid this
If possible, use
with_connection, which automatically handles closing.
It is fine to ignore the returned socket and just use the reader and writer. The
socket is there to make it convenient to call
Where_to_listendescribes the socket that a tcp server should listen on.
Server.trepresents a TCP server listening on a socket.