c - 为什么 FIFO 在客户端-服务器模型上不起作用?
问题描述
我正在使用 FIFO 在 C 中创建客户端-服务器模型,即使我看到所有代码都是正确的,我也没有得到预期的结果。该模型的工作原理如下:客户端写入一个文件名,该文件名写入 FIFO 供服务器读取(来自服务器的预期答案是文件的第一行,如果文件不存在,则为空字符串发回给客户)。随之,客户端发送一个特定 FIFO 的名称,在第二个 FIFO 中,服务器将写入该行。一个唯一的 FIFO 用于将文件名从各个客户端发送到服务器,但每个客户端都有自己的 FIFO 来接收数据。
这是客户端的代码:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define BUFSIZE 512
struct sent {
char *name;
char *fifo;
};
int main()
{
char name[BUFSIZE];
char recieved[BUFSIZE];
int client_server_fifo;
char *cs_fifo = "cs_fifo";
int server_client_fifo;
char *sc_fifo;
sprintf(sc_fifo, "sc_fifo_%d", getpid());
struct sent *sent = malloc(sizeof(struct sent));
mkfifo(sc_fifo, 0777);
while(1) {
printf("Write the name of the file: ");
scanf("%s", name);
printf("1111\n");
client_server_fifo = open(cs_fifo, O_WRONLY);
printf("2222\n");
printf("%s", name);
printf("%s", cs_fifo);
sent->name = name;
sent->fifo = cs_fifo;
printf("%s", name);
printf("%s", cs_fifo);
write(client_server_fifo, sent, sizeof(*sent));
server_client_fifo = open(sc_fifo, O_RDONLY);
if (read(server_client_fifo, recieved, sizeof(recieved)) == -1) {
printf("An error ocurred.\n");
} else {
printf("First line of the file: \n%s\n", recieved);
close(client_server_fifo);
close(server_client_fifo);
}
memset(recieved, 0, sizeof(recieved));
}
return 0;
}
这是服务器:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define BUFSIZE 512
struct sent {
char *name;
char *fifo;
};
int main()
{
int client_server_fifo;
char *cs_fifo = "cs_fifo";
int server_client_fifo;
char *sc_fifo;
struct sent *sent = malloc(sizeof(struct sent));
char *name;
char *line;
FILE *file;
printf("Server running...\n");
mkfifo(cs_fifo, 0777);
while (1)
{
client_server_fifo = open(cs_fifo, O_RDONLY);
printf("1111\n");
read(client_server_fifo, sent, sizeof(*sent));
printf("2222\n");
name = sent->name;
printf("%s\n", name);
sc_fifo = sent->fifo;
printf("%s\n", sc_fifo);
printf("%s", name);
printf("%s", sc_fifo);
server_client_fifo = open(sc_fifo, O_WRONLY);
if (strcmp(izena, "exit") == 0) {
printf("Closing session...");
break;
} else {
if((file = fopen(name, "r")) != NULL) {
fgets(line, BUFSIZE, file);
}
}
printf("Reached this point\n");
write(server_client_fifo, line, strlen(line));
memset(name, 0, sizeof(name));
memset(line, 0, sizeof(line));
}
close(client_server_fifo);
close(server_client_fifo);
unlink(cs_fifo);
unlink(sc_fifo);
return 0;
}
我已经将代码从巴斯克语翻译成英文,所以可能会有一些错误。
此外,正如您所看到的,我放了一些 printfs 来查看我的代码到达的位置,但在客户端的情况下,它打印 1111 的,似乎打开了 fifo 并打印了 2222 并在这里挂起。在服务器上发生同样的情况,打印 1111 和 2222 但仅此而已,然后是分段错误。
知道为什么会这样吗?
我需要在服务器中使用 fork() 吗?或者在无限循环中做所有事情也有效?
谢谢。