<!doctype html public "-//w3c//dtd html 4.0 transitional//en">

CS 779/879
Design of Network Protocols


Spring 2008


Second Exam
Time 2 & 1/2 hours
Open Book

 

 

                                                                         

Name:

 

Login:

 

All questions are of equal weights.


 Question 1:

 

The following functions can be used to read/write from/to a socket.

1. read                          write

2. recv                          send

3. recvfrom                  sendto

4. recvmsg                   sendmsg

 

Describe three examples where you must   use  a  specific function out of these four functions.

Example 1:

 

 

 

 

 

 

Example 2:

        

 

 

 

 

 

Example 3:

      

 


 Question 2:

 We may use   select    to implement  a TCP  single process sever that can serve many concurrent TCP clients without forking or threading.

 Describe two possible scenarios under which this server many hang-up and consequently do not serve its currently connected clients.

 

Scenario 1:

  

 

 

 

 

 

 

 

 

 

 

 

Scenario 1:

               


Question 3:

Consider the following udp client that sends a query to a udp server and display the returned answer:

 

sendmsg.c

main(int argc, char **argv)

{

   int sendsock;

   struct sockaddr_in dest;

   struct sockaddr_in cliaddr;

   int len;

   char Answer[1024];

   char *Question;

 

   sendsock = socket(PF_INET,SOCK_DGRAM, 0);

  

   dest.sin_family = AF_INET;

   dest.sin_addr.s_addr = inet_addr(argv[1]);

   dest.sin_port =  htons(atoi(argv[2]));

   Question = argv[3];

 

   sendto(sendsock, Question,strlen(Question),0, (SA*)&dest, sizeof(dest));

 

   len = sizeof(cliaddr);

   recvfrom (sendsock, Answer, sizeof(Answer), 0, (SA *) &cliaddr, &len);

 

   printf("%s\n", Answer);

 

}

Example:

% sendmsg  128.82.4.210  9877  “my question is ….”

           the answer to your question  is ….

%

Note that if there is no server running at 128.82.4.210 and port 9877 then the client hangs forever.

 

Modify this code in order to use select   to let the client times out  after  <n>  seconds.

The timeout value <n> is specified  as the last argument on the command line.

For example,  if there is no server running at 128.82.4.210 and port 9877 then:

% sendmsg  128.82.4.210  9877  “my question is ….”  2

Timeout after: 2 seconds

%


Answer: make your modifications here:

main(int argc, char **argv)

{

   int sendsock;

   struct sockaddr_in dest;

   struct sockaddr_in cliaddr;

   int len;

   char Answer[1024];

   char *Question;

 

 

 

   sendsock = socket(PF_INET,SOCK_DGRAM, 0);

  

   dest.sin_family = AF_INET;

   dest.sin_addr.s_addr = inet_addr(argv[1]);

   dest.sin_port =  htons(atoi(argv[2]));

   Question = argv[3];

 

   sendto(sendsock, Question,strlen(Question),0, (SA*)&dest, sizeof(dest));

 

 

 

 

 

   len = sizeof(cliaddr);

   recvfrom (sendsock, Answer, sizeof(Answer), 0, (SA *) &cliaddr, &len);

 

 

 

   printf("%s\n", Answer);

}


Question 4:

 

Repeat Question 3 to use alarm signal   in order to timeout  after  <n>  seconds.

Answer: make your modifications here:

 

main(int argc, char **argv)

{

   int sendsock;

   struct sockaddr_in dest;

   struct sockaddr_in cliaddr;

   int len;

   char Answer[1024];

   char *Question;

 

 

 

   sendsock = socket(PF_INET,SOCK_DGRAM, 0);

  

   dest.sin_family = AF_INET;

   dest.sin_addr.s_addr = inet_addr(argv[1]);

   dest.sin_port =  htons(atoi(argv[2]));

   Question = argv[3];

 

   sendto(sendsock, Question,strlen(Question),0, (SA*)&dest, sizeof(dest));

 

 

 

 

 

   len = sizeof(cliaddr);

   recvfrom (sendsock, Answer, sizeof(Answer), 0, (SA *) &cliaddr, &len);

 

 

 

   printf("%s\n", Answer);

}

 


Question 5:

 

Repeat Question 3 to use SCTP sequence packets instead of UDP.

Answer: make your modifications here:

 

main(int argc, char **argv)

{

   int sendsock;

   struct sockaddr_in dest;

   struct sockaddr_in cliaddr;

   int len;

   char Answer[1024];

   char *Question;

 

 

 

   sendsock = socket(PF_INET,SOCK_DGRAM, 0);

  

   dest.sin_family = AF_INET;

   dest.sin_addr.s_addr = inet_addr(argv[1]);

   dest.sin_port =  htons(atoi(argv[2]));

   Question = argv[3];

 

   sendto(sendsock, Question,strlen(Question),0, (SA*)&dest, sizeof(dest));

 

 

 

 

 

   len = sizeof(cliaddr);

   recvfrom (sendsock, Answer, sizeof(Answer), 0, (SA *) &cliaddr, &len);

 

 

 

   printf("%s\n", Answer);

}


Question 6:

 

Consider the Daytime Servers and Clients Examples presented in the class:

S4: Server IPv4 

S6: Server IPv6,

C4: Client IPv4

C6: Client IPv6.

 

Assume we are executing the programs on somethingelse host :

IPv4 address is: 128.82.4.120

IPv6 address is: fe80::203:baff:fe24:7ba7

 

Assume we have the following addresses:

 

1.      128.82.4.120

2.      fe80::203:baff:fe24:7ba7

3.      127.0.0.1

4.      ::1

5.      ::ffff:128.82.4.120

6.      ::ffff:127.0.0.1

 

Fill in the entries of the next matrix to specify all possible addresses the client can use to get the date from the corresponding server:

E.g., C4-S4  entry you may put: 1,4,6 (but this is a wrong answer!).

 

 

S4

S6

C4

 

 

C6

 

 

 


Question 7:

Consider the following daytime Server code. Modify the code so that the server acts as

an IPv4  to Name  translator Server.  The program reads a client message M:

·          If M is an IPv4 address (e.g., 128.82.120), it returns the official hostname (e.g., somethingelse).

·          If M is a host name (e.g., somethingelse), it returns the first known IPv4 address for that host (e.g., 128.82.4.120).

 

Answer: make your modifications here:

 

main(int argc, char **argv)

{

        int                             listenfd ;

        socklen_t                       len;

        struct sockaddr_in              servaddr, cliaddr;

        char                            buff[1024];

        time_t                          ticks;

        int n;

 

 

 

 

 

        listenfd = socket(AF_INET, SOCK_DGRAM, 0);

 

        bzero(&servaddr, sizeof(servaddr));

        servaddr.sin_family      = AF_INET;

        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

        servaddr.sin_port        = htons(1313);

 

        bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

        for ( ; ; ) {

            len = sizeof(cliaddr);

            n = recvfrom(listenfd, buff, 1024, 0, (SA *) &cliaddr, &len);

 

           

 

 

 

 

 

 

 

 

 

 

ticks = time(NULL);

            snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));

            sendto(listenfd, buff, strlen(buff), 0, (SA *) &cliaddr, len);

 

        }

}