Name:
Login:
Write a udp-based echo server with the following description:
Solution
(Q1)
Java Version
Question 2: (20 points)
Write a udp-based echo client with the following description:
For the purpose of this question, assume that:
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.cQuestion 4: (20 points, each part 10 points)
A. Show how to convert the sever code of Question 3 to a deamon.
Solution Part A:
B. Give an example of an Ipv6 multicast address that ensures that a packet will not leave the local link.
Solution Part B:
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);
}
}