我正在尝试编写简单的客户端和服务器C程序,在不同的终端中相互通信.
服务器必须创建公共fifo并等待客户端.与此同时,客户端正在创建自己的fifo,服务器的响应将通过该fifo来实现.客户端的任务是向服务器发送队列创建的名称,并返回ls命令的结果.
我确实搜索了一个答案,例如:fifo-server-program,example-of-using-named-pipes-in-linux-bash,how-to-send-a-simple-string-between-two-programs-using-pipes.我从第三个链接的代码开始并慢慢修改它.
我现在得到的是一个客户端从用户那里获取输入,将其发送到服务器并将其接收回来.但它只能工作一次.我不知道为什么.主要功能的主体在下面.我将不胜感激任何帮助.
编辑:
我搞定了! :D代码在下面,也许它会帮助某人.
server.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 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],BUFSIZ); write(fd,n); close(fds[0]); close(fds[1]); } } memset(tab,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,sizeof(tab)); write(fds[1],"klient",6); perror("Write:"); //Very crude error check read(fds[0],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; }
解决方法
你为什么不管理服务器中的两个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,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,BUFSIZ); } /* clean buf from any data */ memset(buf,sizeof(buf)); } close(client_to_server); close(server_to_client); unlink(myfifo); unlink(myfifo2); return 0; }