Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Now suppose that the same server will act both as the time server in Problem

ID: 3629250 • Letter: 3

Question

3. Now suppose that the same server will act both as the time server in Problem
1 and the DNS server in Problem 2. Thus, some clients will request over the
UDP socket for name-to-IP conversion, and some will connect over a TCP
socket for the time. Thus, the server now needs to open both a TCP socket
and a UDP socket, and accept request from any one (using the accept() +
read()/send() call for TCP, and recvfrom() call for UDP), whichever
comes first. Use the select() call to make the server wait for any one of
the two connections, and handle whichever comes first. All handlings are
iterative.

Explanation / Answer

#include #include #include #include #include #include #include #include #include #include #include #include #include #define MYPORT 53028 /* the port users will be sending to */ #define BACKLOG 10 /* number of pending connections */ #define MAXBUFLEN 100 #define MAXDATASIZE 100 typedef char * string; main() { int sockfd; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int addr_len, numbytes; char buf[MAXBUFLEN]; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(my_addr.sin_zero), 8); if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } //TCP code starts here. // int sockfd_tcp, new_fd; /* listen on sock_fd, new connection on new_fd */ // char buf[MAXDATASIZE]; struct sockaddr_in my_addr_tcp; /* my address information */ struct sockaddr_in their_addr_tcp; /* client's address info */ int sin_size; if ((sockfd_tcp = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } my_addr_tcp.sin_family = AF_INET; my_addr_tcp.sin_port = htons(MYPORT); my_addr_tcp.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */ bzero(&(my_addr_tcp.sin_zero), 8); /* zero the rest */ if (bind(sockfd_tcp,(struct sockaddr *)&my_addr_tcp, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } if (listen(sockfd_tcp, BACKLOG) == -1) { perror("listen"); exit(1); } struct timeval tv; fd_set readfds; tv.tv_sec = 12; tv.tv_usec = 500000; FD_ZERO(&readfds); FD_SET(sockfd, &readfds); FD_SET(sockfd_tcp, &readfds); int select_no = select(sizeof(readfds)*8, &readfds, NULL, NULL, &tv); if(select_no==-1){ printf("select error "); return -1; } if(select_no>0) { if (FD_ISSET(sockfd, &readfds)) { addr_len = sizeof(struct sockaddr); if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); } printf("got packet from %s ",inet_ntoa(their_addr.sin_addr)); printf("packet is %d bytes long ",numbytes); buf[numbytes] = ''; char url[MAXBUFLEN]; while(1) { if((numbytes=recvfrom(sockfd, url, MAXBUFLEN, 0, (struct sockaddr *)&their_addr, &addr_len))==-1) { perror("receive error"); } else { printf("Client Requested DNS: "%s" ",url); int i; struct hostent *he; struct in_addr **addr_list; string ip_list[MAXBUFLEN]; if ((he = gethostbyname(url)) == NULL) { herror("gethostbyname"); sendto(sockfd, "Unknown host..!!", MAXBUFLEN, 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)); sendto(sockfd, "break", MAXBUFLEN, 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)); continue; } sendto(sockfd, he->h_name, MAXBUFLEN, 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)); addr_list = (struct in_addr **)he->h_addr_list; for(i = 0; addr_list[i] != NULL; i++) { ip_list[i] = inet_ntoa(*addr_list[i]); sendto(sockfd, ip_list[i], MAXBUFLEN, 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)); } sendto(sockfd, "break", MAXBUFLEN, 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)); printf(" "); } } } if(FD_ISSET(sockfd_tcp, &readfds)) { while(1) /* main accept() loop */ { sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd_tcp,(struct sockaddr *)&their_addr_tcp,&sin_size)) ==-1) { perror("accept"); continue; } printf("server: got connection from %s ", inet_ntoa(their_addr_tcp.sin_addr)); if (send(new_fd, "Hello, world! ", 14, 0) == -1) perror("send"); while(1) { char str[MAXDATASIZE]; recv(new_fd,str,MAXDATASIZE,0); printf("Client Asked For Time And Date "); char fmt[64], buf[64]; struct timeval tv; struct tm *tm; gettimeofday(&tv, NULL); if((tm = localtime(&tv.tv_sec)) != NULL) { strftime(fmt, sizeof fmt, "Date: %Y-%m-%d Time: %H:%M:%S", tm); //%%06u %z snprintf(buf, sizeof buf, fmt, tv.tv_usec); } send(new_fd,&buf,MAXDATASIZE,0); } close(new_fd); exit(0); } } } //close(sockfd); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote