C++ linux中使用命名管道的简单客户机/服务器程序
我正在尝试编写一个程序,它有两个单独的进程,通过命名管道进行通信。将消息发送到服务器的客户端,以及需要将该消息广播到连接到服务器的所有客户端的服务器。到目前为止,我可以在两者之间建立联系,但无论我做了什么尝试,我都无法获得多条信息。下面是我编写的代码,它允许连接和传输单个消息 server.cpp:C++ linux中使用命名管道的简单客户机/服务器程序,c++,linux,client-server,named-pipes,C++,Linux,Client Server,Named Pipes,我正在尝试编写一个程序,它有两个单独的进程,通过命名管道进行通信。将消息发送到服务器的客户端,以及需要将该消息广播到连接到服务器的所有客户端的服务器。到目前为止,我可以在两者之间建立联系,但无论我做了什么尝试,我都无法获得多条信息。下面是我编写的代码,它允许连接和传输单个消息 server.cpp: #include <fcntl.h> #include <stdio.h> #include <sys/stat.h> #include <unistd.h
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#define FIFO_FILE_1 "/tmp/client_to_server_fifo"
#define FIFO_FILE_2 "/tmp/server_to_client_fifo"
int main()
{
int client_to_server;
int server_to_client;
char buf[BUFSIZ];
/* create the FIFO (named pipe) */
mkfifo(FIFO_FILE_1, 0666);
mkfifo(FIFO_FILE_2, 0666);
printf("Server ON.\n");
while (1)
{
/* open, read, and display the message from the FIFO */
client_to_server = open(FIFO_FILE_1, O_RDONLY);
server_to_client = open(FIFO_FILE_2, O_WRONLY);
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);
}
close(client_to_server);
close(server_to_client);
unlink(FIFO_FILE_1);
unlink(FIFO_FILE_2);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义FIFO文件\u 1“/tmp/客户端\u到\u服务器\u FIFO”
#定义FIFO_文件_2“/tmp/server_to_client_FIFO”
int main()
{
int客户端到服务器;
int服务器到客户端;
char buf[BUFSIZ];
/*创建FIFO(命名管道)*/
mkfifo(FIFO_文件_10666);
mkfifo(FIFO_文件_20666);
printf(“服务器打开。\n”);
而(1)
{
/*打开、读取并显示来自FIFO的信息*/
客户端到服务器=打开(仅限FIFO文件1);
服务器到客户端=打开(FIFO文件2,仅限O);
读取(客户端到服务器、buf、BUFSIZ);
如果(strcmp(“退出”,buf)==0)
{
printf(“服务器关闭。\n”);
打破
}
否则如果(strcmp(“,buf)!=0)
{
printf(“收到:%s\n”,buf);
printf(“发送回…\n”);
写入(服务器到客户端、buf、BUFSIZ);
}
/*清除buf中的任何数据*/
memset(buf,0,sizeof(buf));
关闭(客户端到服务器);
关闭(服务器到客户端);
}
关闭(客户端到服务器);
关闭(服务器到客户端);
取消链接(FIFO文件1);
取消链接(FIFO_文件_2);
返回0;
}
client.cpp:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wait.h>
#include <string.h>
#define FIFO_FILE_1 "/tmp/client_to_server_fifo"
#define FIFO_FILE_2 "/tmp/server_to_client_fifo"
int main()
{
system("clear");
int client_to_server;
int server_to_client;
char str[140];
printf("Input message to server: ");
scanf("%139[^\r\n]", str);
/* write str to the FIFO */
client_to_server = open(FIFO_FILE_1, O_WRONLY);
server_to_client = open(FIFO_FILE_2, O_RDONLY);
if(write(client_to_server, str, sizeof(str)) < 0){
perror("Write:");//print error
exit(-1);
}
if(read(server_to_client,str,sizeof(str)) < 0){
perror("Read:"); //error check
exit(-1);
}
printf("\n...received from the server: %s\n\n\n",str);
close(client_to_server);
close(server_to_client);
/* remove the FIFO */
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义FIFO文件\u 1“/tmp/客户端\u到\u服务器\u FIFO”
#定义FIFO_文件_2“/tmp/server_to_client_FIFO”
int main()
{
系统(“清除”);
int客户端到服务器;
int服务器到客户端;
char-str[140];
printf(“向服务器输入消息:”);
scanf(“%139[^\r\n]”,str);
/*将str写入FIFO*/
客户端到服务器=打开(仅限FIFO文件1、O);
服务器到客户端=打开(仅限FIFO文件2);
if(写入(客户端到服务器,str,sizeof(str))<0){
perror(“写入:”;//打印错误
出口(-1);
}
if(读取(服务器到客户端,str,sizeof(str))<0){
perror(“读取:”;//错误检查
出口(-1);
}
printf(“\n…从服务器接收:%s\n\n\n”,str);
关闭(客户端到服务器);
关闭(服务器到客户端);
/*卸下FIFO*/
返回0;
}
关闭(客户端到服务器);
关闭(服务器到客户端)
从while循环中删除这些行,因为当服务器第一次完成其工作时,它将关闭管道,并且您无法在管道中继续 停止关闭循环中的FIFO。完成后只需将其关闭。您不会检查
open
和close
的返回值!不糟糕!