Advertisement
-fury

UDP_datetime:

May 2nd, 2024
1,368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.63 KB | None | 0 0
  1. //UDP_SERVER_datetime:
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <arpa/inet.h>
  9. #include <netinet/in.h>
  10. #include <time.h>
  11.  
  12. #define PORT     8080
  13. #define MAXLINE 1024
  14.  
  15. int main() {
  16.     int sockfd;
  17.     char buffer[MAXLINE];
  18.     char *hello = "Hello from server";
  19.     struct sockaddr_in servaddr, cliaddr;
  20.  
  21.     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  22.         perror("socket creation failed");
  23.         exit(EXIT_FAILURE);
  24.     }
  25.  
  26.     memset(&servaddr, 0, sizeof(servaddr));
  27.     memset(&cliaddr, 0, sizeof(cliaddr));
  28.  
  29.     servaddr.sin_family = AF_INET;
  30.     servaddr.sin_addr.s_addr = INADDR_ANY;
  31.     servaddr.sin_port = htons(PORT);
  32.  
  33.     if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
  34.         perror("bind failed");
  35.         exit(EXIT_FAILURE);
  36.     }
  37.  
  38.     int len, n;
  39.  
  40.     len = sizeof(cliaddr);
  41.  
  42.     n = recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, (struct sockaddr *)&cliaddr, &len);
  43.     buffer[n] = '\0';
  44.     printf("Client : %s\n", buffer);
  45.  
  46.     time_t rawtime;
  47.     struct tm *timeinfo;
  48.     time(&rawtime);
  49.     timeinfo = localtime(&rawtime);
  50.     char datetime[MAXLINE];
  51.     strftime(datetime, MAXLINE, "%Y-%m-%d %H:%M:%S", timeinfo);
  52.  
  53.     size_t response_length = strlen(hello) + strlen(datetime) + 4;
  54.     if (response_length >= MAXLINE) {
  55.         fprintf(stderr, "Response length exceeds buffer size\n");
  56.         exit(EXIT_FAILURE);
  57.     }
  58.  
  59.     char response[MAXLINE];
  60.     if (snprintf(response, MAXLINE, "%s - %s", hello, datetime) >= MAXLINE) {
  61.         fprintf(stderr, "Response truncated due to buffer size\n");
  62.         exit(EXIT_FAILURE);
  63.     }
  64.  
  65.     sendto(sockfd, (const char *)response, strlen(response), MSG_CONFIRM, (const struct sockaddr *)&cliaddr, len);
  66.     printf("Hello message sent.\n");
  67.  
  68.     return 0;
  69. }
  70.  
  71.  
  72. //UDP_CLIENT_datetime:
  73.  
  74.  
  75.  
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <unistd.h>
  79. #include <string.h>
  80. #include <sys/types.h>
  81. #include <sys/socket.h>
  82. #include <arpa/inet.h>
  83. #include <netinet/in.h>
  84. #include <time.h>
  85.  
  86. #define PORT     8080
  87. #define MAXLINE 1024
  88.  
  89. int main() {
  90.     int sockfd;
  91.     char buffer[MAXLINE];
  92.     char *hello = "Hello from client";
  93.     struct sockaddr_in servaddr;
  94.  
  95.     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  96.         perror("socket creation failed");
  97.         exit(EXIT_FAILURE);
  98.     }
  99.  
  100.     memset(&servaddr, 0, sizeof(servaddr));
  101.  
  102.     servaddr.sin_family = AF_INET;
  103.     servaddr.sin_port = htons(PORT);
  104.     servaddr.sin_addr.s_addr = INADDR_ANY;
  105.  
  106.     int n, len;
  107.  
  108.     time_t rawtime;
  109.     struct tm *timeinfo;
  110.     time(&rawtime);
  111.     timeinfo = localtime(&rawtime);
  112.     char datetime[MAXLINE];
  113.     strftime(datetime, MAXLINE, "%Y-%m-%d %H:%M:%S", timeinfo);
  114.  
  115.     size_t message_length = strlen(hello) + strlen(datetime) + 4;
  116.     if (message_length > MAXLINE) {
  117.         fprintf(stderr, "Message length exceeds buffer size\n");
  118.         exit(EXIT_FAILURE);
  119.     }
  120.  
  121.     char message[MAXLINE];
  122.     if (snprintf(message, MAXLINE, "%s - %s", hello, datetime) >= MAXLINE) {
  123.         fprintf(stderr, "Message truncated due to buffer size\n");
  124.         exit(EXIT_FAILURE);
  125.     }
  126.  
  127.     sendto(sockfd, (const char *)message, strlen(message),
  128.         MSG_CONFIRM, (const struct sockaddr *)&servaddr,
  129.             sizeof(servaddr));
  130.     printf("Hello message sent.\n");
  131.  
  132.     n = recvfrom(sockfd, (char *)buffer, MAXLINE,
  133.                 MSG_WAITALL, (struct sockaddr *)&servaddr,
  134.                 &len);
  135.     buffer[n] = '\0';
  136.     printf("Server : %s\n", buffer);
  137.  
  138.     close(sockfd);
  139.     return 0;
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement