简体   繁体   中英

simple client/server program using named pipes in linux

I am trying to write a program that has two separate process that talk via named pipes. The client which sends a message to a server, and the server which needs to broadcast that message to all clients attached to it. So far, I can get a connection between the two, but I cannot get more than one message to work no matter what I have tried. Below is the code I have written that will allow a connection and transmission of a single message.

server.cpp:

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

#define FIFO_FILE_1  "/tmp/client_to_server_fifo"
#define FIFO_FILE_2  "/tmp/server_to_client_fifo"

int main()
{
    int client_to_server;
    int server_to_client;
    char buf[BUFSIZ];

    /* create the FIFO (named pipe) */
    mkfifo(FIFO_FILE_1, 0666);
    mkfifo(FIFO_FILE_2, 0666);

    printf("Server ON.\n");

    while (1)
    {
    /* open, read, and display the message from the FIFO */
        client_to_server = open(FIFO_FILE_1, O_RDONLY);
        server_to_client = open(FIFO_FILE_2, O_WRONLY);

        read(client_to_server, buf, BUFSIZ);

        if (strcmp("exit",buf)==0)
        {
            printf("Server OFF.\n");
            break;
        }

        else if (strcmp("",buf)!=0)
        {
            printf("Received: %s\n", buf);
            printf("Sending back...\n");
            write(server_to_client,buf,BUFSIZ);
        }

        /* clean buf from any data */
        memset(buf, 0, sizeof(buf));

        close(client_to_server);
        close(server_to_client);
    }

    close(client_to_server);
    close(server_to_client);

    unlink(FIFO_FILE_1);
   unlink(FIFO_FILE_2);
    return 0;
}

client.cpp:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>
#include <string.h>

#define FIFO_FILE_1  "/tmp/client_to_server_fifo"
#define FIFO_FILE_2  "/tmp/server_to_client_fifo"

int main()
{
    system("clear");
    int client_to_server;
    int server_to_client;

    char str[140];

    printf("Input message to server: ");
    scanf("%139[^\r\n]", str);

    /* write str to the FIFO */
    client_to_server = open(FIFO_FILE_1, O_WRONLY);
    server_to_client = open(FIFO_FILE_2, O_RDONLY);

    if(write(client_to_server, str, sizeof(str)) < 0){
        perror("Write:");//print error
        exit(-1);
    }
    if(read(server_to_client,str,sizeof(str)) < 0){
        perror("Read:"); //error check
        exit(-1);
    }
    printf("\n...received from the server: %s\n\n\n",str);

    close(client_to_server);
    close(server_to_client);

   /* remove the FIFO */

   return 0;
}

close(client_to_server); close(server_to_client);

Remove these lines from while loop because when server has done its work for the first time it will close the pipe and you cant be able to proceed further in pipes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM