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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.