server.c

来自「linux的程序设计第二版 是学习LINUX下程序设计的好教程哦」· C语言 代码 · 共 51 行

C
51
字号
#include "client.h"#include <ctype.h>int main(){    int server_fifo_fd, client_fifo_fd;    struct data_to_pass_st my_data;    int read_res;    char client_fifo[256];    char *tmp_char_ptr;    mkfifo(SERVER_FIFO_NAME, 0777);    server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY);    if (server_fifo_fd == -1) {        fprintf(stderr, "Server fifo failure\n");        exit(EXIT_FAILURE);    }    sleep(10); /* lets clients queue for demo purposes */    do {        read_res = read(server_fifo_fd, &my_data, sizeof(my_data));        if (read_res > 0) {// In this next stage, we perform some processing on the data just read from the client.// We convert all the characters in some_data to uppercase and combine the CLIENT_FIFO_NAME// with the received client_pid.            tmp_char_ptr = my_data.some_data;            while (*tmp_char_ptr) {                *tmp_char_ptr = toupper(*tmp_char_ptr);                tmp_char_ptr++;            }            sprintf(client_fifo, CLIENT_FIFO_NAME, my_data.client_pid);// Then we send the processed data back, opening the client pipe in write-only, blocking mode.// Finally, we shut down the server FIFO by closing the file and then unlinking the FIFO.            client_fifo_fd = open(client_fifo, O_WRONLY);            if (client_fifo_fd != -1) {                write(client_fifo_fd, &my_data, sizeof(my_data));                close(client_fifo_fd);            }        }    } while (read_res > 0);    close(server_fifo_fd);    unlink(SERVER_FIFO_NAME);    exit(EXIT_SUCCESS);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?