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

advertisement
CS 779/879
Design of Network Protocols
Spring 2014
Midterm Exam
Time 2 & 1/2 hours
Open Book
Name:
Login:
Question 1: (20 points)
If you are logged into a UNIX/LINUX host, how to find:
1. your login name?
2. the host name?
3. the IP address?
4. The OS version?
5. where you are in the unix directory?
6. Where Dr. wahab’s solution of A1 this semester?
Question 2: (40 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 and 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 <ChatServer HostName> <ChatServer Port> <MulticastIP> <MulticastPort>.
Write the following 4 functions in any programming language (e.g., C, Python).
These 4 functions will be integrated into ChatClient program (e.g., using fork, select or threads).
1. CreateT:
Create a tcp socket T and connect it to the ChatServer.
2. CreateM:
Create a multicast socket M to receive multicast messages.
3. TtoM:
Receive messages from T and send it to M.
4. MtoT:
Receive messages from M and send it to T.
1. CreateT
2. CreateM
3. ToM
4. MtoT
Question 3: 20 points
Consider the following executions of Server and Client:
Srv.c:
char *UnicastIPaddress;
int UDPport;
char *MulticastIPAddress="224.0.0.1";
main (int argc, char *argv[])
{
struct sockaddr_in LocalHost, from;
int UDPsocket;
int rc;
char buf[100];
int fromlen = sizeof (struct sockaddr_in);
UDPsocket = socket (AF_INET, SOCK_DGRAM, 0);
reusePort (UDPsocket);
bzero(&LocalHost, sizeof(LocalHost));
if (strcmp (argv[2], "a") == 0) {
printf ("Using INADDR_ANY\n");
LocalHost.sin_addr.s_addr = htonl (INADDR_ANY);
}
else if (strcmp (argv[2], "m") == 0) {
printf ("Using MCAST ADDR: %s\n", MulticastIPAddress);
LocalHost.sin_addr.s_addr = inet_addr (MulticastIPAddress);
joinGroup (UDPsocket, MulticastIPAddress);
}
else if (strcmp (argv[2], "l") == 0) {
printf ("Using LOCAL ADDR: %s\n", "127.0.0.1");
LocalHost.sin_addr.s_addr = inet_addr ("127.0.0.1");
}
else if (strcmp (argv[2], "u") == 0) {
getIPaddress ();
printf ("Using UNICAST ADDR: %s\n", UnicastIPaddress);
LocalHost.sin_addr.s_addr = inet_addr (UnicastIPaddress);
}
UDPport = htons (atoi (argv[1]));
LocalHost.sin_family = AF_INET;
LocalHost.sin_port = UDPport;
bind (UDPsocket, (SA *) & LocalHost, sizeof (LocalHost));
for (;;) {
rc = recvfrom(UDPsocket, buf, sizeof (buf), 0, (SA *) & from, &fromlen) ;
buf[rc] = '\0';
printf("Received: %s\n", buf);
sendto(UDPsocket, buf, rc, 0, (SA *) & from, fromlen);
}
}
Clt.c:
main(int argc, char *argv[])
{
int
sd;
struct sockaddr_in dest;
char
buf[512];
int
rc;
char *msg=getlogin();
dest.sin_family = AF_INET;
dest.sin_port = ntohs(atoi(argv[1]));
dest.sin_addr.s_addr = inet_addr(argv[2]);
sd = socket(PF_INET, SOCK_DGRAM, 0);
sendto(sd, msg, strlen(msg), 0, (SA *) & dest, sizeof(dest));
rc = recv(sd, buf, sizeof(buf), 0);
buf[rc] = '\0';
printf("Received Back: %s\n", buf);
}
Assume YOU ran Srv simultaneously on 4 different windows (W1, W2, W3 and W4) on
LINUX host (e.g., sirius, 128.82.4.244 ) as follows in that order:
W1: Srv
10234 l
W2: Srv
10234 u
W3: Srv
10234 m
W4: Srv
10234 a
Following that YOU ran the Clt on the SAME host on 3 different windows (W5, W6 and W7) as follows:
W5: Clt 10234 127.0.0.1
W6: Clt 10234 128.82.4.244
W7: Clt 10234 224.0.0.1
Show what is displayed on the seven windows.
Question 4:20 points
A. What is the output of the executing the following program? Explain.
struct sockaddr_in Server;
main(int argc, char *argv[])
{
int
sd, psd, length;
struct hostent *hp, *gethostbyname();
Server.sin_family = AF_INET;
hp = gethostbyname("localhost");
bcopy(hp->h_addr, &(Server.sin_addr.s_addr), hp->h_length);
Server.sin_port = htons(0);
sd = socket(PF_INET, SOCK_STREAM, 0);
bind(sd, (SA *) & Server, sizeof(Server));
length = sizeof(Server);
getsockname(sd, (SA *) & Server, &length);
if (fork() == 0)
Clt();
listen(sd, 0);
psd = accept(sd, 0, 0);
printf(“accepted”);
pause();
}
Clt()
{
int
csd;
csd = socket(AF_INET, SOCK_STREAM, 0);
sleep(1);
if ( connect(csd, (SA *) & Server, sizeof(Server))< 0)
printf(“connection error”);
pause();
}
B. What is the output if we remove the statement
getsockname(sd, (SA *) & Server, &length);
Explain.
Download