CS 779/879 Design of Network Protocols Spring 2015 Midterm Exam

advertisement
CS 779/879
Design of Network Protocols
Spring 2015
Midterm Exam
Time 2 & 1/2 hours
Open Book
Name:
Login:
Question 1: (10 points)
Write a program or a script in any language called whereIam to display the host name and its
IP address.
For example:
% whereIam
Name:
atria
Address: 128.82.4.182
Question 2: (40 points)
A. The following code is a TCP server that accepts connections from any clients at any of the
host interfaces.
Modify the code to accept ONLY clients from the local host.
main( int argc, char *argv[] )
{
int
sd, psd, fromlen;
struct
sockaddr_in server, from ;
struct sockaddr_in from;
int fromlen;
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(10111);
sd = socket (AF_INET,SOCK_STREAM,0);
bind( sd, (SA *) &server, sizeof(server) );
listen(sd,1);
fromlen = sizeof(from);
for(;;){
psd = accept(sd, (SA *)&from, &fromlen);
if ( fork() == 0) Serve(psd, from);
}
}
Serve(int psd, struct sockaddr_in from)
{
printf("Serving %s:%d\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port));
pause();
}
B. The code below is the same code listed in A.
Modify the code so that it does NOT accept clients from local host.
main( int argc, char *argv[] )
{
int
sd, psd, fromlen;
struct
sockaddr_in server, from ;
struct sockaddr_in from;
int fromlen;
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(10111);
sd = socket (AF_INET,SOCK_STREAM,0);
bind( sd, (SA *) &server, sizeof(server) );
listen(sd,1);
fromlen = sizeof(from);
for(;;){
psd = accept(sd, (SA *)&from, &fromlen);
if ( fork() == 0) Serve(psd, from);
}
}
Serve(int psd, struct sockaddr_in from)
{
printf("Serving %s:%d\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port));
pause();
}
C. The code below is the same code listed in A.
Modify the code to accept ONLY clients whose socket port is 10222.
main( int argc, char *argv[] )
{
int
sd, psd, fromlen;
struct
sockaddr_in server, from ;
struct sockaddr_in from;
int fromlen;
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(10111);
sd = socket (AF_INET,SOCK_STREAM,0);
bind( sd, (SA *) &server, sizeof(server) );
listen(sd,1);
fromlen = sizeof(from);
for(;;){
psd = accept(sd, (SA *)&from, &fromlen);
if ( fork() == 0) Serve(psd, from);
}
}
Serve(int psd, struct sockaddr_in from)
{
printf("Serving %s:%d\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port));
pause();
}
D. The following is a TCP client code.
Modify the code to use port 10222 to connect to TCP server of part C.
main(int argc, char *argv[] )
{
int sd;
struct
sockaddr_in server;
struct hostent *hp, *gethostbyname();
char *msg = argv[3];
sd = socket (AF_INET,SOCK_STREAM,0);
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
bcopy ( hp->h_addr, &(server.sin_addr.s_addr), hp->h_length);
server.sin_port = htons(atoi(argv[2]));
connect(sd, (SA *) &server, sizeof(server));
write(sd, msg, strlen(msg));
pause();
}
Question 3: (40 points)
A. The following is a UDP server code to receive unicast messages from any client at any of
the host interfaces.
Modify the code to ALSO accept multicast messages sent to 224.0.0.1 and port 10111.
main( int argc, char *argv[])
{
int
sd, psd, fromlen, length;
struct
sockaddr_in server, from ;
char buf[100];
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(10111);
sd = socket (AF_INET,SOCK_DGRAM,0);
bind( sd, (SA *) &server, sizeof(server) );
fromlen = sizeof(from);
for(;;){
length = recvfrom(sd,buf,sizeof(buf),0,(SA*)&from,&fromlen);
buf[length]=NULL;
printf("Received: %s\n", buf);
printf("From %s:%d\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port));
}
}
B. The code below is the same code listed in A.
Modify the code to accept ONLY multicast messages sent to 224.0.0.1 and port 10111.
main( int argc, char *argv[])
{
int
sd, psd, fromlen, length;
struct
sockaddr_in server, from ;
char buf[100];
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(10111);
sd = socket (AF_INET,SOCK_DGRAM,0);
bind( sd, (SA *) &server, sizeof(server) );
fromlen = sizeof(from);
for(;;){
length = recvfrom(sd,buf,sizeof(buf),0,(SA*)&from,&fromlen);
buf[length]=NULL;
printf("Received: %s\n", buf);
printf("From %s:%d\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port));
}
}
C. The following is the TCP client code of Question 2 D.
1. Modify the code to be a UDP client to send its argv[3] message.
main(int argc, char *argv[] )
{
int sd;
struct
sockaddr_in server;
struct hostent *hp, *gethostbyname();
char *msg = argv[3];
sd = socket (AF_INET,SOCK_STREAM,0);
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
bcopy ( hp->h_addr, &(server.sin_addr.s_addr), hp->h_length);
server.sin_port = htons(atoi(argv[2]));
connect(sd, (SA *) &server, sizeof(server));
write(sd, msg, strlen(msg));
pause();
}
2. Give an example to use the code to send a unicast message to the UDP server of Question 3 A:
3. Give an example to use the code send a multicast message to the UDP server of Question 3 A:
D.
1. Is it possible to run two instances of the TCP server of Question 2A on the same host?
Explain.
2. Is it possible to run two instances of the UDP server of Question 3A on the same host?
Explain.
3. Is it possible to run two instances of the UDP server of Question 3B on the same host?
Explain.
4. Is it possible to run one instance of the TCP server of Question 2A and one instance of
the UDP server of Question 3A or 3B on the same host?
Explain.
Question 4: (10 points)
To allow multiple groups (e.g., ODU, Harvard & Oxford) to exchange multicast messages we
may use the following scheme:
Chat Server
Chat Client 1
Chat Client 2
Chat Client 3
One way to implement the ChatClient is to have two sockects:
A TCP socket T connected to the ChatServer & a Multicast socket M.
Any message received from M is sent to T and any message received from T is send to M.
The syntax for the ChatClient is:
% ChatClient <ChatServerIP> <ChatServerPort> <MulticastIP> <MulticastPort>.
The following is an outline of the ChatClient code.
Use either fork, thread or select to complete the program.
You are not required to complete the codes for the two functions CreateT & CreateM.
char ChatServerIP[100];
char ChatServerPort[100];
char MulticastIP[100];
char MulticastPort[100];
int T, M;
int main(int argc, char **argv)
{
strcpy(ChatServerIP, argv[1]);
strcpy(ChatServerPort, argv[2]);
strcpy(MulticastIP, argv[3]);
strcpy(MulticastPort, argv[4]);
T= CreateT();
M= CreateM();
/* complete the code */
}
CreateT(){
/* Create a tcp socket T and connect it to the ChatServer */
}
CreateM(){
/* Create a multicast socket M to receive multicast messages */
}
Download