Home arrow Network Programming arrow Network Programming Quick Tutorial
Network Programming Quick Tutorial PDF
Tuesday, 03 May 2011
work in progress! come back later.

For network programming we will use Client / Server architecture. Server is a program that stays in memory and accept requests on a port. A client connects to the server using an IP address of the server and port of the server. Server respond to the client and send back the data client requested.

TCP Server functions (the order is also important)
  socket();       // function creates an endpoint for comunication
  bind();          // asigns a local protocol address to a socket
  listen();         // listen for connections on a socket
  accept();       // after this function server block execution of the program until a client
                      connects to the server.


If we do not assign a port to the structure used by bind for the tcp server, the kernel will create an ephemeral port for the socket when listen() or connect() functions are called.
In this case we must use getsockname to return the value of the efemeral port assigned by the kernel.

TCP Client functions
  connect();    // connects to the server


Functions in detail

socket() function
socket() function creates an endpoint for comunication.

socket() function
#include <sys/types.h>
#include <sys/socket.h>

int socket(int domain, int type, int protocol);

Where domain can be:
  AF_INET - for IPv4 protocol
  AF_INET6 - for IPv6 protocol
  AF_LOCAL - for UNIX domain protocol
  AF_ROUTE - for routing sockets
  AF_KEY - for key socket

bind(2) function
function asigns a local protocol address to a socket.

bind() function
#include <sys/types.h>
#include <sys/socket.h>

int bind(int s, const struct sockaddr *addr, socklen_t addrlen);

If we want to specify a wildcard for IP address, we must use the constant INADDR_ANY (default value is 0):

  // IPv4

  struct sockaddr_in   servaddr;
  servaddr.sin_addr.s_addr = htonl (INADDR_ANY);


  // IPv6
  struct sockaddr_in6    servaddr;
  servaddr.sin6_addr = in6addr_any;

listen() function
function listens for connections on a socket.

listen() function
#include <sys/types.h>
#include <sys/socket.h>

int listen(int sockfd, int backlog);

listen() function moves the socket from CLOSED to LISTEN state.

accept() function
function accepts a connection on a socket.

#include <sys/types.h>
#include <sys/socket.h>

int accept(int s, struct sockaddr * restrict addr,
         socklen_t * restrict addrlen);

connect(2) function
function initiate/establish a connection (on a socket) with a TCP server (three way handshake).
return value: 0 if everything is ok, -1 if any error

connect() function
#include <sys/types.h>
#include <sys/socket.h>

int connect(int s, const struct sockaddr *name, socklen_t namelen);

If connect() fails we must close() the socket descriptor before calling connect() again.

fork() function

fork() function creates a new process.

fork() function
#include <sys/types.h>
#include <unistd.h>

pid_t fork(void);

fork() function returns 0 in child process, process ID of child in parent and -1 on error.

- between client and the server before sending the data is a connection negotiatiation called three way handshake, after that data is requested by the client, server send the data to the client and then client send back to the server a End of File notification.

For more information see man pages of those functions.

Tip1. When working with network functions always check for errors for every function call. To simplify the code build your own wrapper to well known function you use. And use a naming convention for that. For example your socket function wrapper could be called Socket (with capital S).

Last Updated ( Thursday, 01 March 2012 )

Other BSD Systems





Best BSD firewall?