server
unknown
c_cpp
22 days ago
1.9 kB
4
Indexable
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #define PIPE_NAME "/tmp/encrypted_chat_pipe" #define MAX_MESSAGE_LENGTH 256 void reverse_encrypt(char *str) { int len = strlen(str); for (int i = 0; i < len / 2; i++) { char temp = str[i]; str[i] = str[len - i - 1]; str[len - i - 1] = temp; } } int main() { char message[MAX_MESSAGE_LENGTH]; int pipe_fd; // Създаване на наименувана тръба if (mkfifo(PIPE_NAME, 0666) == -1) { perror("mkfifo failed"); exit(1); } // Отваряме тръбата за четене pipe_fd = open(PIPE_NAME, O_RDONLY); if (pipe_fd == -1) { perror("open failed"); exit(1); } while (1) { // Четене на съобщение от клиента int bytes_read = read(pipe_fd, message, sizeof(message) - 1); if (bytes_read > 0) { message[bytes_read] = '\0'; // Добавяне на нулев терминиращ символ printf("Received message: %s\n", message); // Прилагане на Reverse Encryption reverse_encrypt(message); printf("Encrypted message: %s\n", message); // Отваряме същата тръба за запис, за да изпратим обратно int pipe_fd_write = open(PIPE_NAME, O_WRONLY); if (pipe_fd_write == -1) { perror("open failed for writing"); exit(1); } write(pipe_fd_write, message, strlen(message) + 1); // Записваме обратно съобщението close(pipe_fd_write); } } close(pipe_fd); return 0; }
Editor is loading...
Leave a Comment