The UDP Protocol
#define MAXBUF 80    /*********  Iterative UDP Echo Client  **********/
#include "funcs.h"

main()
{
    int active, fromlen, tolen, n;
    struct sockaddr_in sin, from, local; 
    char buf[MAXBUF], line[MAXBUF];
  
    active = socket(AF_INET, SOCK_DGRAM, 0);
    memset(&sin, 0, sizeof(sin));
    sin.sin_port = htons(3009);
    sin.sin_family = AF_INET;
    inet_aton("153.18.17.12", &sin.sin_addr);

  /***
   ***  Bind to any available local port so that sendto statements
   ***  do not have to "compete" with rogue apps throwing packets
   ***  at the local port.
   ***/

    memset(&local, 0, sizeof(local));
    local.sin_port   = 0;
    local.sin_family = AF_INET;
    local.sin_addr.s_addr = htonl(INADDR_ANY);

    Bind(active, (struct sockaddr *) &local, sizeof(local));

    fromlen = sizeof(struct sockaddr_in);
    memset(buf, 0, MAXBUF);
    while(printf("Enter string: "), fgets(buf,MAXBUF,stdin),
          strcmp(buf,"quit\n") != 0)
    {
          n = sendto(active, buf, MAXBUF, 0, (struct sockaddr *) &sin, fromlen);
          memset(buf, 0, MAXBUF);
          recvfrom(active, buf, MAXBUF, 0, (struct sockaddr *)&from, &fromlen);
          printf("Message from echo server: %s \n", buf);
          memset(buf, 0, MAXBUF);
    }
}

#include "funcs.h"  /******  Iterative UDP Echo Server  ******/
#define MAX_BUF 80

main()
{
      int sd, ns, n, cli_len;
      char buf[80];
      struct sockaddr_in sin, cli_addr; 
    
      if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) <0)
      {
           perror("socket");
           exit(1);
      }


  /*********  Bind server port to avoid stray packets from
  **********  other aps.
  **********/
      sin.sin_port = htons(3009);
      sin.sin_addr.s_addr = htonl(INADDR_ANY);
      sin.sin_family = AF_INET;
      Bind(sd, (struct sockaddr *) &sin, sizeof(sin));

      cli_len = sizeof(sin);
     /**** No listen or accept calls on a connectionless socket!! *****/
      for (;;)
      {
        memset(buf, 0, MAX_BUF);
        while ((n = recvfrom(sd,buf,MAX_BUF,0,(struct sockaddr *) &cli_addr,
                             &cli_len))  > 0)
        {
              sendto(sd, buf, MAX_BUF, 0, (struct sockaddr *) &cli_addr,
                     cli_len); 
              memset(buf, 0, MAX_BUF);
        } 
      }
}


/*********************  Sample Sessions Below  ************************/

$ udpechos &
[1]     25783

$ udpechoc

Enter string: hello
Message from echo server: hello 
Enter string: what's up, doc?
Message from echo server: what's up, doc? 
Enter string:   /******  Hit CTRL-Z here.  ******/
[2] + Stopped                  udpechoc

$ udpechoc      /******  Second client.    ******/
Enter string: why
Message from echo server: why 
Enter string: who
Message from echo server: who 
Enter string: quit

$ fg    /*******  Bring back first client.  *******/
udpechoc
hello, again!
Message from echo server: hello, again! 
Enter string: quit