<!doctype html public "-//w3c//dtd
html 4.0 transitional//en">
CS 779/879
Design
of Network Protocols
Spring
2004
Midterm
Exam
Time
2 & 1/2 hours
Open
Book & Notes
Name:
Login:
NOTE: In order to make the programs short, you may NOT write any include,
comments or non-essential error statements.
Question 1: (30 points)
% Q1C 128.82.4.7
Solution ( Q1C Code C or Java)
Question
2: (40 points)
Assume that a daytime server Q2S may serve only
those clients that either has the same IP address A “OR”
the same port number P as itself.
1. Is it possible
to replace the word “OR” with “AND”?
3. Write the code
for a client Q2C that can always talk to that server S if it
runs on the same host as S and do its best to talk to S if it runs on different
host from S. The server’s IP address A
and port number P are specified as command-line arguments for C.
E.g.,
% Q2C 128.82.4.7
1234
Solution ( Q2C Code C or Java)
4. Is it possible
to run the client as:
% Q2C 127.0.0.1
1234 &
% Q2C 127.0.0.1
1234
5. Assume the
client at host 128.82.4.8, is it possible to run two instances of the client
as:
% Q2C 128.82.4.7 1234 &
% Q2C 128.82.4.7 1234
6. Why Q2C cannot
“always” talk to Q2S if it runs on a different host?
Question 3: (30 points)
Q3v1.c:
int main(int
argc, char **argv)
{
int sockfd, n;
struct sockaddr_in servaddr;
char recvline[MAXLINE +
1];
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");
if ( (n = read(sockfd, recvline,
MAXLINE)) <= 0)
err_sys("read
error");
recvline[n] = 0; /* null terminate */
Fputs(recvline, stdout);
exit(0);
}
int
main(int argc, char **argv)
{
int sockfd, n, flags, error;
struct sockaddr_in servaddr;
char recvline[MAXLINE + 1];
fd_set rset;
struct timeval tval;
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]));
flags = Fcntl(sockfd, F_GETFL, 0);
Fcntl(sockfd, F_SETFL, flags |
O_NONBLOCK);
error = 0;
if ( (n = connect(sockfd,(SA *)
&servaddr, sizeof(servaddr))) < 0)
if (errno != EINPROGRESS){
Fputs("Connection
Error\n", stdout);
exit(0);
}
if (n != 0) { /* connect not completed immediately */
FD_ZERO(&rset);
FD_SET(sockfd, &rset);
tval.tv_sec =
htons(atoi(argv[3]));
tval.tv_usec = 0;
if ( select(sockfd+1, &rset, NULL, NULL,
&tval) == 0) {
Fputs("Connection
Timeout\n", stdout);
exit(0);
}
}
Fcntl(sockfd, F_SETFL, flags);
if ( (n = read(sockfd, recvline,
MAXLINE)) <= 0)
err_sys("read error");
recvline[n] = 0; /* null terminate */
Fputs(recvline, stdout);
exit(0);
}
Solution
(Q3v3.c)
int
main(int argc, char **argv)
{
int sockfd, n, flags, error;
struct sockaddr_in servaddr;
char recvline[MAXLINE + 1];
fd_set rset;
struct timeval tval;
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]));
flags = Fcntl(sockfd, F_GETFL, 0);
Fcntl(sockfd, F_SETFL, flags |
O_NONBLOCK);
error = 0;
if ( (n = connect(sockfd,(SA *)
&servaddr, sizeof(servaddr))) < 0)
if (errno != EINPROGRESS){
Fputs("Connection
Error\n", stdout);
exit(0);
}
if (n != 0) { /* connect not completed immediately */
FD_ZERO(&rset);
FD_SET(sockfd, &rset);
tval.tv_sec =
htons(atoi(argv[3]));
tval.tv_usec = 0;
if ( select(sockfd+1, &rset, NULL, NULL,
&tval) == 0) {
Fputs("Connection
Timeout\n", stdout);
exit(0);
}
}
Fcntl(sockfd, F_SETFL, flags);
if ( (n = read(sockfd, recvline,
MAXLINE)) <= 0)
err_sys("read
error");
recvline[n] = 0; /* null terminate */
Fputs(recvline, stdout);
exit(0);
}