【问题标题】:Why do I get the error message when reading from a FIFO?为什么从 FIFO 读取时会收到错误消息?
【发布时间】:2021-08-10 11:23:10
【问题描述】:

在操作系统简介课程中,我们被要求使用 FIFO 构建客户端-服务器模型。作为客户端,我们向服务器发送一个字符串,服务器获取该字符串,如果存在具有此名称的文件,它会发回该文件的第一行。如果文件不存在或存在但恰好为空,则返回一个空字符串。

问题是只工作一次,例如,我发送 file1,服务器发回第一行,当我再次发送 file1 或同一“会话”中的另一个文件名时,printf("First line of the file %s: \n%s\n", name, recived); 不会发生,它进入if (read(fifo_serv_client, recived, sizeof(recived)) == -1) { printf("An error occurred.\n"); }

知道为什么会这样吗?我用同一个文件尝试了两次,所以它存在 100%,但我仍然得到相同的结果。

非常感谢!

这是客户端的代码:

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

#define BUFSIZE 512

int main()
{
   int fifo_client_serv;
   char *fifo1 = "fifo_client_serv";

   int fifo_serv_client;
   char *fifo2 = "fifo_serv_client";

   char name[BUFSIZE];

   while(1) {
      printf("Write the file's name: ");
      scanf("%s", name);

      /* write str to the FIFO */
      fifo_client_serv = open(fifo1, O_WRONLY);
      fifo_serv_client = open(fifo2, O_RDONLY);
      write(fifo_client_serv, name, sizeof(name));

      char recived[BUFSIZE];

      if (read(fifo_serv_client, recived, sizeof(recived)) == -1) {
         printf("An error occurred.\n");
      } else {
         printf("First line of the file %s: \n%s\n", name, recived);
         close(fifo_client_serv);
         close(fifo_serv_client);

      }
   }

   return 0;
}

这是服务器的代码:

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

#define BUFSIZE 512

int main()
{
   int fifo_client_serv;
   char *fifo1 = "fifo_client_serv";

   int fifo_serv_client;
   char *fifo2 = "fifo_serv_client";

   char buf[BUFSIZE];
   char line[BUFSIZE];

   FILE *file;

   /* create the FIFO (named pipe) */
   mkfifo(fifo1, 0777);
   mkfifo(fifo2, 0777);
   
   printf("Server runnning...\n");

   while (1)
   {
      fifo_client_serv = open(fifo1, O_RDONLY);
      fifo_serv_client = open(fifo2, O_WRONLY);
      read(fifo_client_serv, buf, BUFSIZE); 
       
      if((file = fopen(buf, "r")) == NULL) {
         write(fifo_serv_client, "", BUFSIZE);
      } else {
         fgets(line, BUFSIZE, file);
         write(fifo_serv_client, line, BUFSIZE);
      }

      /* clear buffer and line */
      memset(buf, 0, sizeof(buf));
      memset(line, 0, sizeof(buf));

      close(fifo_client_serv);
      close(fifo_serv_client);

      unlink(fifo1);
      unlink(fifo2);
   }

   return 0;
}

更新我发现了为什么会发生这种情况,如果我在其中创建fifo,它可以正常工作!我只是暂时把mkfifo(fifo1, 0777); mkfifo(fifo2, 0777);放在第一位。我的问题是,每次从客户端发送文本时是否真的有必要创建 FIFO?我不能只创建一次 FIFO,从它进行通信并在完成后关闭吗?

【问题讨论】:

    标签: linux server client fifo


    【解决方案1】:

    你可以尝试在服务器文件中读/写后关闭文件吗?

    if((file = fopen(buf, "r")) == NULL) {
         write(fifo_serv_client, "", BUFSIZE);
         fclose(file);
    } else {     
         fgets(line, BUFSIZE, file);
         write(fifo_serv_client, line, BUFSIZE);
         fclose(file);
    }
    

    顺便说一句。值得对您的 writes 进行错误检查。

    【讨论】:

    • 仍然发生相同的情况,我错误检查了客户端和服务器中的每次写入,当我第二次输入名称时,write(fifo_client_serv, name, sizeof(name));
    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多