UDP made simple

Abstract: This page describes how to write a simple UDP client/server system in a C/Unix environment. The code is explained step by step.

Motivation: I needed a page like this when working with a small test program for my master's thesis at Appius / Fält Communications. It is quite hard to remember all the socket API details off the top of your head, so I wanted a small reference page to get me started quickly without having to wade through tons of man pages. As I did not find a page I liked, I decided to write one. I hope it will be useful for others, too.

Caveats: The code is known to work under recent (fall 1999) versions of Linux. It should work on other Unices too, though some of the header files may be located in other directories on your system.

The server

     1  #include <arpa/inet.h>
     2  #include <netinet/in.h>
     3  #include <stdio.h>
     4  #include <sys/types.h>
     5  #include <sys/socket.h>
     6  #include <unistd.h>
     7
     8  #define BUFLEN 512
     9  #define NPACK 10
    10  #define PORT 9930
    11
    12  void diep(char *s)
    13  {
    14    perror(s);
    15    exit(1);
    16  }
    17
    18  int main(void)
    19  {
    20    struct sockaddr_in si_me, si_other;
    21    int s, i, slen=sizeof(si_other);
    22    char buf[BUFLEN];
    23
    24    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    25      diep("socket");
    26
    27    memset((char *) &si_me, 0, sizeof(si_me));
    28    si_me.sin_family = AF_INET;
    29    si_me.sin_port = htons(PORT);
    30    si_me.sin_addr.s_addr = htonl(INADDR_ANY);
    31    if (bind(s, &si_me, sizeof(si_me))==-1)
    32        diep("bind");
    33
    34    for (i=0; i<NPACK; i++) {
    35      if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
    36        diep("recvfrom()");
    37      printf("Received packet from %s:%d\nData: %s\n\n", 
    38             inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
    39    }
    40
    41    close(s);
    42    return 0;
    43 }

Comments

The client

     1  #define SRV_IP "999.999.999.999"
     2  /* diep(), #includes and #defines like in the server */
     3
     4  int main(void)
     5  {
     6    struct sockaddr_in si_other;
     7    int s, i, slen=sizeof(si_other);
     8    char buf[BUFLEN];
     9
    10    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    11      diep("socket");
    12
    13    memset((char *) &si_other, 0, sizeof(si_other));
    14    si_other.sin_family = AF_INET;
    15    si_other.sin_port = htons(PORT);
    16    if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
    17      fprintf(stderr, "inet_aton() failed\n");
    18      exit(1);
    19    }
    20
    21    for (i=0; i<NPACK; i++) {
    22      printf("Sending packet %d\n", i);
    23      sprintf(buf, "This is packet %d\n", i);
    24      if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
    25        diep("sendto()");
    26    }
    27
    28    close(s);
    29    return 0;
    30  }

Note: The client is quite similar to the server. Only the differences will be discussed.

General tips


Last modified 07-10-12 by Gunnar Gunnarsson. Comments, bug fixes, and suggestions welcome.