Back to libut API Reference


    #include "libut/ut.h"
    int UT_net_connect(char *name, UT_fd_cb *cb, void *data, int flags,...);


This function attempts to establish a TCP/IP socket to a particular host and port. The function also permits the source IP address and port to be specified, or these can be chosen automatically by the kernel.

The name argument is a descriptive name for the connection (which will be copied, and silently truncated if too long). This name is only used in the output of the fds control port command and in log messages.

The cb argument specifies the callback to be invoked when the connection succeeds or fails, and (in the case of success) when data from the remote side is readable. See the CALLBACK section below.

The data argument is an opaque pointer that is passed back to the callback.

The flags argument is a bitwise-OR using these pre-defined constants:

If this flag is specified, the following argument is an IP:port string (e.g., ``'') specifying the source IP address and port number to use. The port number can be 0, in which case the choice is left to the kernel. This flag may be omitted in which case the source IP address and port are automatically chosen by the kernel.

This flag is mandatory. The last argument to the function is an IP:port string (e.g., ``'') specifying the destination IP address and port number to which the connection should be made.


The I/O callback must have this prototype: int (UT_fd_cb)(int fd, char *name, int status, void *data);

The name and data arguments are passed to the callback exactly as they were passed to UT_net_connect(). The fd argument is the file descriptor of the socket. The status argument informs the callback of the disposition of the file descriptor using the following bits (other bits may be set and must be ignored):

This is initial notification that the connection has been accepted by the remote side.

The connection attempt failed. The reason is indicated in the log. Do not close the file descriptor; libut does this.

The file descriptor is readable. (This can also indicate EOF).

Closing the connection

When the connection is no longer needed, or the callback detects that the remote side has closed the socket, the callback should call close(2) and UT_fd_unreg(2) on the file descriptor.

Callback return value

The callback's return value is ignored.


If an error occurs, -1 is returned and the reason is logged. A non-negative return value indicates that a connection attempt has been initiated; it may or may not be accepted. The callback will be notified when the connection is accepted or fails.


The fds control port command displays connecting sockets, and other file descriptors.


UT_net_resolve(3), UT_fd_cntl(3), UT_net_listen(3)


Troy D. Hanson <>