UDP -- Chapter 12

UDP is built on IP.  Basically, it is a thin layer over IP that provides 'port numbers' and hides many of the IP options.  In other words, it provides unreliable, out of order, best effort service.  If you (a user program) thought you wanted IP, you really want UDP.

Port Numbers: Remember that the ultimate destination of data over a network is not a host, but a program on a host.  It can be hard to identify a process on a machine somewhere far away running an unknown operating system programed by people who use an unknown language, so they use port numbers to identify processes.

Each port number is an unsigned 16 bit integer.  That means there are 64K ports.  Some port numbers are reserved for well known services, like mail or web servers.  Other port numbers are created on the fly.

Purpose of UDP: UDP provides high performance unreliable, unordered delivery of datagrams to a specific port on a specific host.  Every UDP packet also carried a return address of the senders IP and port numbers.

Header of a UDP packet: See page 181.  Just has the sender port num, dest port num, message len, and udp checksum.

Writing Code That Uses UDP: You do NOT have to bind a socket to an address.  You do NOT have to accept a connection from a client to a server.  Those are connection oriented ideas.  Remember, UDP is a connectionless protocol.  There is NO error code for failure to deliver data.  UDP does not promis to deliver data.

Example Code Fragments:
    // create a socket
    int sock = socket(AF_INET, SOCK_DGRAM, SOCK_DGRAM);

    // read data
    struct sockaddr from_address;
    int length = recvfrom(sock, buffer, buflength, 0, &from_address, &from_address_length);

    // send data
    struct sockaddr dest_address;
    /* fill in the dest address */
    int error = sendto(sock, buf, buflen, 0, &dest_address, sizeof(dest_address);