In making a connection, the client makes a connection while the server
waits for and accepts the connection. Here we discuss what the client
program must do with the
connect function, which is declared in
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
connect function initiates a connection from the socket
with file descriptor socket to the socket whose address is
specified by the addr and length arguments. (This socket
is typically on another machine, and it must be already set up as a
server.) See Socket Addresses, for information about how these
arguments are interpreted.
connect waits until the server responds to the request
before it returns. You can set nonblocking mode on the socket
socket to make
connect return immediately without waiting
for the response. See File Status Flags, for information about
The normal return value from
0. If an error
-1. The following
error conditions are defined for this function:
The socket socket is not a valid file descriptor.
File descriptor socket is not a socket.
The specified address is not available on the remote machine.
The namespace of the addr is not supported by this socket.
The socket socket is already connected.
The attempt to establish the connection timed out.
The server has actively refused to establish the connection.
The network of the given addr isn’t reachable from this host.
The socket address of the given addr is already in use.
The socket socket is non-blocking and the connection could not be
established immediately. You can determine when the connection is
completely established with
select; see Waiting for Input or Output.
connect call on the same socket, before the connection is
completely established, will fail with
The socket socket is non-blocking and already has a pending
connection in progress (see
This function is defined as a cancellation point in multi-threaded programs, so one has to be prepared for this and make sure that allocated resources (like memory, file descriptors, semaphores or whatever) are freed even if the thread is canceled.