FIFO服務器程序以及客戶端程序

服務器程序:

#include "client.h"
#include <ctype.h>
#include <fcntl.h>
int main()
{
    int server_fifo_fd, client_fifo_fd,server_fifo_fd_write;
    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 | O_NONBLOCK);
    if (server_fifo_fd == -1) {
        fprintf(stderr, "Server fifo open read only failure\n");
        exit(EXIT_FAILURE);
    }
    printf("open read only the server fifo :%d  \n",server_fifo_fd);
     /*服務器以寫阻塞模式打開,因讀非阻塞模式打開會成功返回,故寫阻塞模式也會成功打開*/
    server_fifo_fd_write = open(SERVER_FIFO_NAME, O_WRONLY);
    if (server_fifo_fd_write == -1) {
        fprintf(stderr, "Server fifo open write only failure\n");
        exit(EXIT_FAILURE);
    }
     /*關閉讀打開的阻塞模式,使read調用阻塞*/
    printf("open write only the server fifo :%d \n",server_fifo_fd_write);
	int		val;

	if ((val = fcntl(server_fifo_fd, F_GETFL, 0)) < 0)
	{
		printf("fcntl F_GETFL error");
		exit(EXIT_FAILURE);
	}


	val &= ~O_NONBLOCK;		/* turn flags off */

	if (fcntl(server_fifo_fd, F_SETFL, val) < 0)
	{
		printf("fcntl F_GETFL error");
		exit(EXIT_FAILURE);
	}
    
    printf(" set the read fifo block \n");

    do {
        read_res = read(server_fifo_fd, &my_data, sizeof(my_data));
	printf("read from server_fifo :%d \n",read_res);
        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));
                sleep(2);
                close(client_fifo_fd);
            }
        }
    } while (read_res > 0);
    close(server_fifo_fd);
    unlink(SERVER_FIFO_NAME);
    exit(EXIT_SUCCESS);
}


客戶端程序:

// Here's the client, client.c. The first part of this program opens the server FIFO,
// if it already exists, as a file. It then gets its own process ID, which forms some
// of the data that will be sent to the server. The client FIFO is created, ready for
// the next section.

#include "client.h"
#include <ctype.h>

int main()
{
    int server_fifo_fd, client_fifo_fd;
    struct data_to_pass_st my_data;
    int times_to_send;
    char client_fifo[256];

    server_fifo_fd = open(SERVER_FIFO_NAME, O_WRONLY);
    if (server_fifo_fd == -1) {
        fprintf(stderr, "Sorry, no server\n");
        exit(EXIT_FAILURE);
    }

    my_data.client_pid = getpid();
    sprintf(client_fifo, CLIENT_FIFO_NAME, my_data.client_pid);
    if (mkfifo(client_fifo, 0777) == -1) {
        fprintf(stderr, "Sorry, can't make %s\n", client_fifo);
        exit(EXIT_FAILURE);
    }

// For each of the five loops, the client data is sent to the server.
// Then the client FIFO is opened (read-only, blocking mode) and the data read back.
// Finally, the server FIFO is closed and the client FIFO removed from memory.

    for (times_to_send = 0; times_to_send < 5; times_to_send++) {
        sprintf(my_data.some_data, "Hello from %d", my_data.client_pid); 
        printf("%d sent %s, ", my_data.client_pid, my_data.some_data);
        write(server_fifo_fd, &my_data, sizeof(my_data));
        client_fifo_fd = open(client_fifo, O_RDONLY);
        if (client_fifo_fd != -1) {
            if (read(client_fifo_fd, &my_data, sizeof(my_data)) > 0) {
                printf("received: %s\n", my_data.some_data);
            }
            close(client_fifo_fd);
        }
    }
    close(server_fifo_fd);
    unlink(client_fifo);
    exit(EXIT_SUCCESS);
}


執行結果

open read only the server fifo :3  
open write only the server fifo :4 
read from server_fifo :24 
read from server_fifo :24 
read from server_fifo :24 
read from server_fifo :24 
read from server_fifo :24 
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章