Question: The following C program named server.c is to run a server-side application that can connect and receive data from a specific client application. (1)________________(comment about

  1. The following C program named server.c is to run a server-side application that can connect and receive data from a specific client application.

  1. (1)________________(comment about lines 2 to 6)
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. (2)_________________(comment about lines 9 to 10)
  8. #define SERVER_PORT 3490
  9. #define MAX_BUF_LEN 100
  10. int main(void)
  11. {
  12. int sockfd; (3)________________(comment about line 14)
  13. (4)________________(comment about lines 17 to 18)
  14. struct sockaddr_in srv_info;
  15. struct sockaddr_in clnt_info;
  16. int addr_len, numbytes; (5)________________(comment about line 20)
  17. char buf[MAX_BUF_LEN]; (6)________________(comment about line 21)
  18. (7)________________(comment about lines 24 to 28)
  19. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  20. {
  21. perror("Socket creation error.");
  22. exit(1);
  23. }
  24. (8)________________(comment about lines 31 to 34)
  25. srv_info.sin_family = AF_INET;
  26. srv_info.sin_addr.s_addr = INADDR_ANY;
  27. srv_info.sin_port = htons(SERVER_PORT);
  28. memset(&srv_info, 0, sizeof(srv_info));
  29. (9)________________(comment about lines 37 to 41)
  30. if(bind(sockfd,(struct sockaddr *)&srv_info, sizeof(struct sockaddr)) == -1)
  31. {
  32. perror("Bind error.");
  33. exit(1);
  34. }
  35. while (1)
  36. {
  37. addr_len = sizeof(struct sockaddr);
  38. (10)________________(comment about lines 48 to 53)
  39. if ((numbytes=recvfrom(sockfd, buf, MAX_BUF_LEN-1, 0, (struct sockaddr *)&clnt_info, &addr_len)) == -1)
  40. {
  41. perror("Receiving error.");
  42. exit(1);
  43. }
  44. buf[numbytes] = '\0';
  45. (11)________________(comment about lines 56 to 60)
  46. if ((numbytes=sendto(sockfd, "Your message is received ", 26, 0, (struct sockaddr *)&clnt_info, sizeof(struct sockaddr))) == -1)
  47. {
  48. perror("Sending error.");
  49. exit(1);
  50. }
  51. printf("Type ctrl-C to exit server ");
  52. }
  53. close(sockfd); (12)________________(comment about line 65)
  54. return 0;
  55. }

Please read the program code and try to explain the program by writing down appropriate comments to describe each different part of the program code (highlighted in yellow color above).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!