Advertisement
-fury

TCP_datetime

May 2nd, 2024
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. //TCP_SERVER_datetime:
  2. #include <stdio.h>
  3. #include <netdb.h>
  4. #include <netinet/in.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <time.h>
  11.  
  12. #define MAX 80
  13. #define PORT 8080
  14. #define SA struct sockaddr
  15.  
  16. void func(int connectID) {
  17.     char buffer[MAX];
  18.     int n;
  19.     char datetime[MAX];
  20.     time_t rawtime;
  21.     struct tm *timeinfo;
  22.  
  23.     for (;;) {
  24.         bzero(buffer, MAX);
  25.         read(connectID, buffer, sizeof(buffer));
  26.         time(&rawtime);
  27.         timeinfo = localtime(&rawtime);
  28.         strftime(datetime, MAX, "%Y-%m-%d %H:%M:%S", timeinfo);
  29.         printf("[%s] From the client: %s\t To the client : ", datetime, buffer);
  30.         bzero(buffer, MAX);
  31.         n = 0;
  32.         while ((buffer[n++] = getchar()) != '\n');
  33.         write(connectID, buffer, sizeof(buffer));
  34.         if (strncmp("exit", buffer, 4) == 0) {
  35.             printf("Closing Server\n");
  36.             break;
  37.         }
  38.     }
  39. }
  40.  
  41. int main() {
  42.     int sockfd, connectID, length;
  43.     struct sockaddr_in serverAddress, client;
  44.  
  45.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  46.     if (sockfd == -1) {
  47.         printf("Socket not created!\n");
  48.         exit(0);
  49.     } else {
  50.         printf("Socket successfully created!\n");
  51.     }
  52.     bzero(&serverAddress, sizeof(serverAddress));
  53.     serverAddress.sin_family = AF_INET;
  54.     serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
  55.     serverAddress.sin_port = htons(PORT);
  56.     if ((bind(sockfd, (SA *)&serverAddress, sizeof(serverAddress))) != 0) {
  57.         printf("Socket binding failed!\n");
  58.         exit(0);
  59.     } else {
  60.         printf("Socket binding done successfully!\n");
  61.     }
  62.     if ((listen(sockfd, 5)) != 0) {
  63.         printf("Listen failed!\n");
  64.         exit(0);
  65.     } else {
  66.         printf("Server listening!\n");
  67.     }
  68.     length = sizeof(client);
  69.     connectID = accept(sockfd, (SA *)&client, &length);
  70.     if (connectID < 0) {
  71.         printf("Server accept failed!\n");
  72.         exit(0);
  73.     } else {
  74.         printf("Server accepted the client!\n");
  75.     }
  76.     func(connectID);
  77.     close(sockfd);
  78. }
  79.  
  80.  
  81.  
  82. //TCP_CLIENT_datetime:
  83. #include <arpa/inet.h>
  84. #include <netdb.h>
  85. #include <stdio.h>
  86. #include <stdlib.h>
  87. #include <string.h>
  88. #include <strings.h>
  89. #include <sys/socket.h>
  90. #include <unistd.h>
  91. #include <time.h>
  92.  
  93. #define MAX 80
  94. #define PORT 8080
  95. #define SA struct sockaddr
  96.  
  97. void func(int sockfd) {
  98.     char buffer[MAX];
  99.     int n;
  100.     char datetime[MAX];
  101.     time_t rawtime;
  102.     struct tm *timeinfo;
  103.  
  104.     time(&rawtime);
  105.     timeinfo = localtime(&rawtime);
  106.     strftime(datetime, MAX, "%Y-%m-%d %H:%M:%S", timeinfo);
  107.  
  108.     for (;;) {
  109.         bzero(buffer, sizeof(buffer));
  110.         printf("Enter the string: ");
  111.         n = 0;
  112.         while ((buffer[n++] = getchar()) != '\n');
  113.         int message_length = strlen(buffer);
  114.         write(sockfd, datetime, strlen(datetime));
  115.         write(sockfd, buffer, message_length);
  116.         read(sockfd, buffer, MAX);
  117.         printf("[%s] From the server : %s", datetime, buffer);
  118.         if ((strncmp(buffer, "exit", 4)) == 0) {
  119.             printf("Closing Client\n");
  120.             break;
  121.         }
  122.     }
  123. }
  124.  
  125. int main() {
  126.     int sockfd, connfd;
  127.     struct sockaddr_in socketAddress, cli;
  128.  
  129.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  130.     if (sockfd == -1) {
  131.         printf("Socket not created!\n");
  132.         exit(0);
  133.     } else {
  134.         printf("Socket successfully created!\n");
  135.     }
  136.     bzero(&socketAddress, sizeof(socketAddress));
  137.     socketAddress.sin_family = AF_INET;
  138.     socketAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
  139.     socketAddress.sin_port = htons(PORT);
  140.     if (connect(sockfd, (SA *)&socketAddress, sizeof(socketAddress)) != 0) {
  141.         printf("Connection with the server failed!\n");
  142.         exit(0);
  143.     } else {
  144.         printf("Connected to the server!\n");
  145.     }
  146.     func(sockfd);
  147.     close(sockfd);
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement