CS 779/879
Design of Network Protocols
Spring 2002
Final Exam
Time 2 & 1/2 hours
Open Book & Notes
 
 
Name:
Login:
 
 
Question 1: (20 points)

Write a udp-based echo server with the following description:

UdpEchoServer <m> <p> Where <m> is an ipv4 multicast address (e.g., 224.1.1.1) and <p> is a udp port (e.g., 1234). The server creates a udp socket S and joins it the multiacst group given by <m> and <p>. Any message received on S from a UdpEchoClient (see Question 2 below) is sent back to the same client where the message came from using a unicast message.
 
 

Solution (Q1)
 

 Java Version

 
 
 
 
 

 Question 2: (20 points)

Write a udp-based echo client with the following description:

UdpEchoClient <m> <p> Where <m> is an ipv4 multicast address (e.g., 224.1.1.1) and <p> is a udp port (e.g., 1234). The client reads the firstmessage from the user and sends it the UdpEchoServer (see Question 1 above) using the multicast group given by <m> and <p>. The client discovers the unicast ip address <u> of the server from the reply to the first message. All subsequent messages from the user will be sent to the server using the unicast address given by <u> and <p>.

For the purpose of this question, assume that:

  1. the server started before the client.
  2. multicast routing is enabled between the client and the server.
  3. no udp message is lost between the client and the server.
Solution(Q2)
 
 Java Version

 
 
 

Question 3: (20 points)

In designing a server based on preforking, if all the children processes are busy serving clients, new clients will not be severed immediately, they have to be queued or turned away.

To server these additional clients, write the code for a server that combines preforking with forking-on-demand, i.e., the parent process following the creating of all the children (preforking) will continue to accept new clients and create a new process that handle each accepted client (forking-on-demand).

For simplicity, use no locking around accept.
 
 

Solution(Q3)
 
 

BiServers.c
Question 4: (20 points, each part 10 points)
A. Show how to convert the sever code of Question 3 to a deamon.
     

    Solution Part A:
     
     
     
     
     

                    daemon_init(argv[0], 0);

     
     
     
     
     
     

    B. Give an example of an Ipv6 multicast address that ensures that a packet will not leave the local link.

    Solution Part B:

            FF02::22
Question 5: (20 points, each part 10 points)

Consider the EchoClient and the EchoServer programs shown below.
A. Modify the EchoClient program to exit if the connection is not accepted immediately.

B. Assume that the client and server are running on two different hosts, modify each program such that each will exit if the machine running its partner is not reachable for more than 20 seconds due to either machine or network failure.

EchoClient:

#include "unp.h"

int

main(int argc, char **argv)

{

int sockfd, n;

struct sockaddr_in servaddr;

char sendline[MAXLINE], recvline[MAXLINE];

int flags = Fcntl(sockfd, F_GETFL, 0);
Fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
 

if ( (sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)

err_sys("socket error");

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

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

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

if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)

err_sys("connect error");
 

heartbeat_cli(sockfd, 1 , 20);
 
 

while (Fgets(sendline, MAXLINE, stdin) != NULL) {

Writen(sockfd, sendline, strlen(sendline));

if (Readline(sockfd, recvline, MAXLINE) == 0)

err_quit("str_cli: server terminated prematurely");

Fputs(recvline, stdout);

}

exit(0);

}
 
 

EchoServer:

#include "unp.h"

int

main(int argc, char **argv)

{

int listenfd, connfd;

struct sockaddr_in servaddr;

ssize_t n;

char line[MAXLINE];

listenfd = Socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(SERV_PORT);

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

Listen(listenfd, LISTENQ);

if ( (connfd = accept(listenfd, 0,0)) < 0) {

err_sys("accept error");

}
 

heartbeat_serv(sockfd, 1 , 20);

for ( ; ; ) {

if ( (n = Readline(connfd, line, MAXLINE)) == 0)

return; /* connection closed by other end */

Writen(sockfd, line, n);

}

}