我有一台服务器和两个不同的客户端。服务器和客户端将通过FIFO在它们之间进行通信。每个客户端首先向服务器发送一个包含clientpid和数字的struct变量。数字用于区分客户。然后,服务器为每个客户端提供adeguate fifo。建立此服务器后,服务器会向每个客户端发送不同的消息。当我运行服务器和客户端时,客户端没有收到消息,他们会继续阅读。它为什么会发生,我该怎么做才能解决它? (对于这个例子,我简化了客户端,它们几乎是相同的,除了它们传递给服务器以区分自己的数量。在原始程序中,两个客户端将执行不同的操作,并且它们将从服务器,这就是我需要区分它们的原因。)
server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <stdint.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define ec_neg1(s,m) if((s) == -1) {perror(m); exit(errno);}
#define SERVER_FIFO_NAME "fifo_server"
#define PERM_FILE 0664
struct acknow_
{
pid_t clientpid;
int num;
};
int make_fifo_name(pid_t pid, char *name, size_t name_max)
{
snprintf(name, name_max, "fifo%ld", (long)pid);
return 0;
}
int main()
{
int fd_server;
int fd_send;
int fd_recv;
ssize_t nread;
struct acknow_ acknow;
char fifo_send[100];
char fifo_recv[100];
char msg1[100] = "SENDER GOT THE MESSAGE\n";
char msg2[100] = "RECEIVER GOT THE MESSAGE\n";
printf("server is started\n");
if(mkfifo(SERVER_FIFO_NAME, PERM_FILE) == -1 && errno != EEXIST)
{perror("can't make fifo"); exit(errno); }
ec_neg1( fd_server = open(SERVER_FIFO_NAME, O_RDWR), "cant open fd_server" )
while(1)
{
ec_neg1( nread = read(fd_server, &acknow, sizeof(acknow)), "can't read from fd_server")
if (nread == 0)
{
errno = ENETDOWN;
perror("nread == 0"); exit(errno);
}
if(acknow.num == 1) /* sender's pid arrived first */
{
/*MAKE FIFO OF THE SENDER*/
make_fifo_name(acknow.clientpid, fifo_send, sizeof(fifo_send));
ec_neg1(fd_send = open(fifo_send, O_WRONLY), "can't open fifo_send" )
/*GET PID OF THE RECEIVER*/
ec_neg1( nread = read(fd_server, &acknow, sizeof(acknow)), "can't read from fd_server")
if (nread == 0)
{
errno = ENETDOWN;
perror("nread == 0"); exit(errno);
}
/*MAKE FIFO OF THE RECEIVER*/
make_fifo_name(acknow.clientpid, fifo_recv, sizeof(fifo_recv));
ec_neg1( fd_recv = open(fifo_recv, O_WRONLY), "can't open fifo_recv" )
}
else /* receiver's pid arrived first */
{
/*MAKE FIFO OF THE SENDER*/
make_fifo_name(acknow.clientpid, fifo_recv, sizeof(fifo_recv));
ec_neg1(fd_recv = open(fifo_recv, O_WRONLY), "can't open fifo_recv" )
/*GET PID OF THE RECEIVER*/
ec_neg1( nread = read(fd_server, &acknow, sizeof(acknow)), "can't read from fd_server")
if (nread == 0)
{
errno = ENETDOWN;
perror("nread == 0"); exit(errno);
}
/*MAKE FIFO OF THE RECEIVER*/
make_fifo_name(acknow.clientpid, fifo_send, sizeof(fifo_send));
ec_neg1( fd_send = open(fifo_send, O_WRONLY), "can't open fifo_name" )
}
printf("sending message to sender\n");
ec_neg1( write(fd_send, &msg1, sizeof(msg1)), "can't write to fd_send" )
printf("sending message to receiver\n");
ec_neg1( write(fd_recv, &msg2, sizeof(msg2)), "can't write to fd_recv")
ec_neg1( close(fd_send), "can't close fd_send" )
ec_neg1( close(fd_recv), "can't close fd_recv" )
printf("done\n");
}
ec_neg1( close(fd_server), "can't close fd_server" )
return 0;
}
Client1.c(这里我只发布Client1,Client2只是相同的代码,只需将&#34; acknow.num&#34;更改为&#34; 2&#34;如果你想运行它们。)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <stdint.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define ec_neg1(s,m) if((s) == -1) {perror(m); exit(errno);}
#define SERVER_FIFO_NAME "fifo_server"
#define PERM_FILE 0664
struct acknow_
{
pid_t clientpid;
int num;
};
int make_fifo_name(pid_t pid, char *name, size_t name_max)
{
snprintf(name, name_max, "fifo%ld", (long)pid);
return 0;
}
int main()
{
int fd_server, fd_client;
ssize_t nread;
char fifo_name[100];
char msg[100];
struct acknow_ acknow;
printf("client1 started\n");
acknow.clientpid = getpid();
acknow.num = 1;
make_fifo_name(acknow.clientpid, fifo_name, sizeof(fifo_name));
if (mkfifo(fifo_name, PERM_FILE) == -1 && errno != EEXIST)
{perror("can't make fifo"); exit(errno); }
ec_neg1( fd_server = open(SERVER_FIFO_NAME, O_WRONLY), "can't open fd_server" )
ec_neg1( write(fd_server, &acknow, sizeof(acknow)),"can't write to fd_server" )
ec_neg1( fd_client = open(fifo_name, O_RDWR), "can't open fifo_name" )
while((nread = read(0, &msg, sizeof(msg))))
{
write(1, msg, nread);
}
if (nread == 0)
{
errno = ENETDOWN;
perror("nread == 0"); exit(errno);
}
if (nread == -1)
perror("can't read"); exit(errno);
ec_neg1( close(fd_server), "can't close fd_server" )
ec_neg1( close(fd_client), "can't close fd_client" )
ec_neg1( unlink(fifo_name), "can't unlink fifo_name" )
return 0;
}
答案 0 :(得分:0)
首先,在服务器中,您将打开读取FIFO O_WRONLY
。这应该是O_RDONLY
。
其次,在客户端中,您将打开读取FIFO O_RDWR
。我相信这是POSIX下的未定义行为(你不提操作系统),应该是O_RDONLY
。这已经足够了。