Sockets , Client - Server 8 :
Consider the server program given below:
/* assume include files:
*/
void * handle_request (void * a)
{ int connection;
time_t nticks;
char buf [512];
nticks = time (NULL);
connection = *(int *)a;
sprintf (buf, "%.24s", ctime (&nticks));
write (connection, buf, strlen (buf) + 1);
close (connection);
pthread_exit (NULL);
}
int main (int argc, char * argv [])
{ int sockfd, newsockfd, clilen, childpid, STCP_PORT, threaded = 0;
time_t ticks; char cmd [255], buf [512];
struct sockaddr_in server1, client1;
if (argc != 2) exit (1);
threaded = atoi (argv [1]);
STCP_PORT = 6300; /* port servers listens to */
bzero ((char *)&server1, sizeof (server1));
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
perror ("Socket fails");
server1.sin_family = AF_INET;
server1.sin_addr.s_addr = htonl (INADDR_ANY);
server1.sin_port = htons (STCP_PORT);
if (bind (sockfd, (struct sockaddr *)& server1, sizeof (server1)) < 0)
{ perror("Server socket bind failed"); exit (1); }
listen (sockfd, 1);
for (;;)
{ int i, fd, len; time_t nticks; pthread_t servt;
nticks = time (NULL); clilen = sizeof (client1); strcpy (cmd, "");
newsockfd = accept (sockfd, (struct sockaddr *) & client1, &clilen);
if (newsockfd < 0) {
close (sockfd);
fprintf (stderr, "%s\n", "Server accept failed");
exit (1);
}
if (threaded == 0) {
sprintf (buf, "%.24s", ctime (&nticks));
write (newsockfd, buf, strlen (buf) + 1); close (newsockfd);
} else {
pthread_create (&servt, NULL, handle_request, (void *)&newsockfd);
}
}
}
A.
Draw two client-server architecture diagrams for the communication channels
used by this server assuming there are two clients and
command line argument was 0.
Clearly indicate ports common across two diagrams.
Consider Fig. 12.2 and Fig. 12.3 (pp. 434) from the Textbook for notation.
B.
Draw a client-server diagram assume that the command line argument is 1.
C.
Discuss the pros and cons of threaded implementation of this "time" server.