Client Coding

Code on the client is very similar to the server:

  1. Figure out the address information of the server.

  2. Construct a struct sockaddr_in address.

  3. Get a socket().

  4. connect() to the server.

  5. Use read() and write() to send and receive data.

connect

connect() makes the connection to the server. It returns a file descriptor that you read and write with.

int connect (int socket, const struct sockaddr *address, socklen_t addresslen);

Give it the address to connect to, and it returns zero if the connection succeeds or -1 for an error, with errno set.

Example 14.4 is a client that is a little more convenient than telnet. It follows telnet’s example in searching for the host address: first it does an IPv6 lookup and tries all addresses listed ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.