6. Datagram Sockets

  1. Run the programs bellow. Make a clear description of the way this program works.

  2. What are the advantages and disadvantages of using Datagram sockets over Stream sockets?

  3. Rewrite the calculator program you wrote for the last laboratory assignment substituting the pipes for datagram sockets .

    Note:

    Compile this programs in a Solaris system using the command:

    gcc foo.c -o foo -lsocket -lnsl

    Client Program

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdio.h>
     
    #define DATA "Quote"
     
    main (int argc, char* argv[])
    {
       int sock;
       struct sockaddr_in server;
       struct hostent *hp, *gethostbyname();
       char buf[1024];
     
       sock = socket (AF_INET, SOCK_DGRAM, 0);
       if (sock == -1) {
          perror ("Opening Datagram socket");
          exit (1);
       }
     
       hp = gethostbyname(argv[1]);
       if (hp == (struct hostent *) 0) {
          perror("Unknown Host");
          exit(0);
       }
     
       memcpy ((char*)&server.sin_addr, (char*) hp->h_addr, hp->h_length);
     
       server.sin_family = AF_INET;
       server.sin_port = htons(atoi(argv[2]));
     
       if (sendto( sock, DATA, sizeof DATA, 0,
                   (struct sockaddr *) &server, sizeof server) == -1) {
          perror("Sending datagram message");
          exit(1);
       }
       if (read( sock, buf, 1024) == -1) {
          perror("Receiving quotation");
          exit(1);
       }
       printf("Quote of the Moment: %s\n", buf);
       close(sock);
    }

    Server Program

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdio.h>
    
    #define TRUE 1
    
    int quoteInd= 0;
    int quoteNumber= 2;
    char* quotes[]= { "lixo1", "lixo2"};
    
    char* getquote() {
       int ind;
    
       ind= quoteInd;
       quoteInd++;
       if (quoteInd>=quoteNumber) quoteInd=0;
       return quotes[ind];
    }
    
    main()
    {
       int sock, length;
       struct sockaddr_in server;
       struct sockaddr_in client;
       char buf[16];
       char* quote;
    
       sock = socket (AF_INET, SOCK_DGRAM, 0);
       if (sock==-1) {
          perror("Opening datagram socket");
          exit(1);
       }
    
       server.sin_family = AF_INET;
       server.sin_addr.s_addr = INADDR_ANY; 
       server.sin_port = 0;
       if (bind (sock, (struct sockaddr *)&server, sizeof(server)) == -1){
          perror("Binding datagram socket");
    
          exit (1);
       }
       length = sizeof server;
       if (getsockname (sock, (struct sockaddr *) &server, &length) == -1){
          perror("Getting socket name");
          exit (1);
       }
       printf("Socket port # %d\n", ntohs(server.sin_port));
       
       do{
          if ( recvfrom( sock, buf, 16, 0,
                        (struct sockaddr *) &client, &length) == -1){
             perror("Reading packet");
             exit(1);
          }
          quote= getquote();
          if ( sendto( sock, quote, strlen(quote)+1, 0,
                       (struct sockaddr *) &client, sizeof client) == -1) {
             perror("Writing quota");
             exit(0);
          }
       } while (TRUE);
    }