C Programming/Networking in UNIX

Network programming under UNIX is relatively simple in C.

This guide assumes you already have a good general idea about C, UNIX and networks.

A simple client
To start with, we'll look at one of the simplest things you can do: initialize a stream connection and receive a message from a remote server.

This is the very bare bones of a client; in practice, we would check every function that we call for failure, however, error checking has been left out for clarity.

As you can see, the code mainly revolves around  which is a struct of type. This struct stores information about the machine we want to connect to.

The  function tells our OS that we want a file descriptor for a socket which we can use for a network stream connection; what the parameters mean is mostly irrelevant for now.

Now we get on to the interesting part:

The first line uses  to zero the struct.

The second line sets the address family. This should be the same value that was passed as the first parameter to ; for most purposes   will serve.

The third line is where we set the IP of the machine we need to connect to. The variable  is just an integer stored in Big Endian format, but we don't have to know that as the   function will do the conversion from string into Big Endian integer for us.

The fourth line sets the destination port number. The  function converts the port number into a Big Endian short integer. If your program is going to be run solely on machines which use Big Endian numbers as default then  would work just as well. However, for portability reasons  should always be used.

Now that all of the preliminary work is done, we can actually make the connection and use it:

This tells our OS to use the socket  to create a connection to the machine specified in.

Now this receives up to  bytes of data from the connection and stores them in the buffer string. The number of characters received is returned by. It is important to note that the data received will not automatically be null terminated when stored in the buffer, so we need to do it ourselves with.

And that's about it!

The next step after learning how to receive data is learning how to send it. If you've understood the previous section then this is quite easy. All you have to do is use the  function, which uses the same parameters as. If in our previous example  had the text we wanted to send and its length was stored in   we would write. returns the number of bytes that were sent. It is important to remember that, for various reasons, may not be able to send all of the bytes, so it is important to check that its return value is equal to the number of bytes you tried to send. In most cases this can be resolved by resending the unsent data.

A simple server
Superficially, this is very similar to the client. The first important difference is that rather than creating a  with information about the machine we're connecting to, we create it with information about the server, and then we   it to the socket. This allows the machine to know the data received on the port specified in the  should be handled by our specified socket.

The  function then tells our program to start listening using the given socket. The second parameter of  allows us to specify the maximum number of connections that can be queued. Each time a connection is made to the server it is added to the queue. We take connections from the queue using the  function. If there is no connection waiting on the queue the program waits until a connection is received. The  function returns another socket. This socket is essentially a "session" socket, and can be used solely for communicating with connection we took off the queue. The original socket continues to listen on the specified port for further connections.

Once we have "session" socket we can handle it in the same way as with the client, using  and   to handle data transfers.

Note that this server can only accept one connection at a time; if you want to simultaneously handle multiple clients then you'll need to  off separate processes, or use threads, to handle the connections.

Useful network functions
The parameters are a pointer to an array of chars and the size of that array. If possible, it finds the hostname and stores it in the array. On failure it returns -1.

This function obtains information about a domain name and stores it in a  struct. The most useful part of a  structure is the   field, which is a null terminated array of the IP addresses associated with that domain. The field  is a pointer to the first IP address in the   array. Returns  on failure.

What about stateless connections?
If you don't want to exploit the properties of TCP in your program and would rather just use a UDP connection, then you can just replace  with   in your call to   and use the result in the same way. It is important to remember that UDP does not guarantee delivery of packets and order of delivery, so checking is important.

If you want to exploit the properties of UDP, then you can use  and , which operate like   and   except you need to provide extra parameters specifying who you are communicating with.

How do I check for errors?
The functions,   and   all return -1 on failure and use errno for further details.