Making a Connection
-------------------
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
`sys/socket.h'.
- Function: int connect (int SOCKET, struct sockaddr *ADDR, socklen_t
LENGTH)
The `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.)
Note:Socket Addresses, for information about how these
arguments are interpreted.
Normally, `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. Note:File Status Flags, for information about
nonblocking mode.
The normal return value from `connect' is `0'. If an error
occurs, `connect' returns `-1'. The following `errno' error
conditions are defined for this function:
`EBADF'
The socket SOCKET is not a valid file descriptor.
`ENOTSOCK'
File descriptor SOCKET is not a socket.
`EADDRNOTAVAIL'
The specified address is not available on the remote machine.
`EAFNOSUPPORT'
The namespace of the ADDR is not supported by this socket.
`EISCONN'
The socket SOCKET is already connected.
`ETIMEDOUT'
The attempt to establish the connection timed out.
`ECONNREFUSED'
The server has actively refused to establish the connection.
`ENETUNREACH'
The network of the given ADDR isn't reachable from this host.
`EADDRINUSE'
The socket address of the given ADDR is already in use.
`EINPROGRESS'
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'; Note:Waiting for I/O. Another `connect' call on the same
socket, before the connection is completely established, will
fail with `EALREADY'.
`EALREADY'
The socket SOCKET is non-blocking and already has a pending
connection in progress (see `EINPROGRESS' above).
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, files descriptors, semaphores or
whatever) are freed even if the thread is canceled.