5 Client-side programming

Matters on the client-side is a bit simpler. A client process also needs to call socket to acquire an end-point FD. However, there is no need to bind, listen or accept.

A client process uses the connect call to request a connection to a server, identified by an address (like an IP address+port number or the path to a named socket).

The connect call uses almost the same three parameters as the bind call for a server. However, the first parameter is the end-point FD of the client process, while the second parameter specifies the address of the server-side end-point. Like bind, connect returns 0 when it is successful.

connect is a blocking function. This means that is does not return unless the connection is accepted, rejected, or an error happens. Particularly, if a client request to connect arrives after the connection request queue is full, then a client may find connect returns -1, and (errno == EAGAIN).

If connect return 0, it means the connection is accomplished. At this point, the end-point FD becomes a connection FD. In other words, at this point, the FD returned by socket can be used in read and write.

Note that if SOCK_NONBLOCK is specified in type when socket is called, then the read and write calls become non-blocking. This means that if read requests to read more bytes than available, it returns with whatever number bytes it can read (and the return value reflects that). The same applies to write.