正确的FIFO客户端-服务器连接

7

我正在尝试编写简单的客户端和服务器C程序,它们在单独的终端中相互通信。

服务器必须创建一个公共FIFO并等待客户端。同时,客户端正在创建自己的FIFO,通过该FIFO,将会收到来自服务器的响应。客户端的任务是向服务器发送通过队列创建的名称,并获得ls命令的结果。

我搜索了答案,例如:fifo-server-programexample-of-using-named-pipes-in-linux-bashhow-to-send-a-simple-string-between-two-programs-using-pipes。我从第三个链接的代码开始,然后慢慢修改。

现在我做到了,客户端接收用户输入,将其发送到服务器并将其返回。但是它只能工作一次。我不知道为什么。主函数的主体如下。我将非常感激任何帮助。

编辑: 我让它工作了!:D 以下是代码,也许会对某人有所帮助。

服务器代码:

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

int main(int argc, char* argv[])
{
    int fds[2];
    char tab[BUFSIZ];
    int fd, n;

    char *myfifo = "/tmp/serwer";
    char *myfifo2 = "/tmp/client";

    pipe(fds);
    mkfifo(myfifo,0666);

    while(1)
    {
        fds[0]=open(myfifo2,O_RDONLY);
        fds[1]=open(myfifo,O_WRONLY);

        read(fds[0],tab,BUFSIZ);

        if (strcmp("klient",tab)==0) {
            printf("Od klienta: %s\n",tab);
            fd=open(tab,O_WRONLY);

            if(fork()==0)
            {
                dup2(fds[1],1);
                close(fds[1]);
                execlp("ls","ls","-l",NULL);
                close(fds[0]);
                close(fds[1]);
            }
            else
            {
                dup2(fds[0],0);
                n = read(fds[0],tab,BUFSIZ);
                write(fd,tab,n);
                close(fds[0]);
                close(fds[1]);
            }
        }
        memset(tab, 0, sizeof(tab));
        close(fd);
        close(fds[0]);
        close(fds[1]);
    }

    unlink(myfifo);
    return 0;
}

客户端代码client.c:
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main(int argc, char* argv[])
{
    int fds[2];
    char *myfifo = "/tmp/serwer";
    char *myfifo2 = "/tmp/client";

    mkfifo(myfifo2,0666);
    fds[0]=open(myfifo,O_RDONLY);
    fds[1]=open(myfifo2,O_WRONLY);

    char tab[BUFSIZ];
    memset(tab, 0, sizeof(tab));

    write(fds[1],"klient",6);
    perror("Write:"); //Very crude error check
    read(fds[0],tab,sizeof(tab));
    perror("Read:"); // Very crude error check

    printf("Odebrano od serwera: %s\n",tab);

    close(fds[0]);
    close(fds[1]);
    unlink(myfifo2);
    return 0;
}

1
从这段代码中看不出来。你已经让ls输出正常工作了还是只发送小消息?在这种情况下通常会陷入死锁,即服务器正在等待输入,而客户端也在等待输入。他们永远等待,因为没有人发送任何东西。 - Duck
不,ls命令还没有起作用。这些程序只能工作一次——服务器等待消息,将其返回给客户端,客户端可以关闭。当我想发送另一条消息时,客户端和服务器都变得无响应。 - uluroki
2个回答

6
为什么你不在服务器上管理两个FIFO呢?只需更改代码即可使其正常工作。
如果您真的想要建立客户端-服务器关系,并且服务器为多个不同的客户端提供服务,则套接字可能是更好的选择。
client.cpp
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
   int client_to_server;
   char *myfifo = "/tmp/client_to_server_fifo";

   int server_to_client;
   char *myfifo2 = "/tmp/server_to_client_fifo";

   char str[BUFSIZ];
   printf("Input message to serwer: ");
   scanf("%s", str);


   /* write str to the FIFO */
   client_to_server = open(myfifo, O_WRONLY);
   server_to_client = open(myfifo2, O_RDONLY);
   write(client_to_server, str, sizeof(str));

   perror("Write:"); //Very crude error check

   read(server_to_client,str,sizeof(str));

   perror("Read:"); // Very crude error check

   printf("...received from the server: %s\n",str);
   close(client_to_server);
   close(server_to_client);

   /* remove the FIFO */

   return 0;
}

server.cpp

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

int main()
{
   int client_to_server;
   char *myfifo = "/tmp/client_to_server_fifo";

   int server_to_client;
   char *myfifo2 = "/tmp/server_to_client_fifo";

   char buf[BUFSIZ];

   /* create the FIFO (named pipe) */
   mkfifo(myfifo, 0666);
   mkfifo(myfifo2, 0666);

   /* open, read, and display the message from the FIFO */
   client_to_server = open(myfifo, O_RDONLY);
   server_to_client = open(myfifo2, O_WRONLY);

   printf("Server ON.\n");

   while (1)
   {
      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);

   unlink(myfifo);
   unlink(myfifo2);
   return 0;
}

4
由于命名管道的工作方式,它只能运行一次。每次你使用open读取一个命名管道时,你都会被阻塞,直到另一个进程打开它进行写操作。然后你们就配对了,文件描述符连接了你们的进程。一旦任何一端关闭了该连接,那么这个管道的生命周期就结束了。为了让你的服务器“接受另一个连接”,它需要将管道的openclose移动到主循环中,以便可以重复地进行配对。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接