我想使用命名管道编写一个回显客户端-服务器应用程序(我用套接字做了它,它工作,但现在我想用命名管道做同样的事情。)服务器必须是多线程的。
当我第一次运行它们时,我认为我的程序可以正常工作,一切都很好。每个连接到服务器的客户端都从它那里收到一条消息。
但是现在,当我第二次尝试运行程序时,我仍然收到以下消息:
mkfifo client2server: File exists
(许多这样的消息)。这是我尝试运行 ./echoserver 时的输出:
$ ./echoserver
Server is working ...
mkfifo client2server: File exists
mkfifo client2server: File exists
mkfifo client2server: File exists
mkfifo client2server: File exists
mkfifo client2server: File exists
mkfifo client2server: File exists
mkfifo client2server: File exists
mkfifo client2server: File exists
mkfifo client2server: File exists
mkfifo client2server: File exists
mkfifo client2server: File exists
mkfifo client2server: File exists
如何修复它以使其按我的意愿工作?制作一个真正的多线程回显服务器-客户端应用程序?
这是我的代码:
回声服务器.cpp
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <sstream>
using namespace std;
string toString(int d)
{
ostringstream ss;
ss << d;
return ss.str();
}
void* clients_service(void * arg)
{
int client_id = *((int*)&arg), server2client = -1, buffsize = 255, bytes_read = -1, client2server = -1;
client_id++;
char buffer[255];
string fifoname = "/tmp/server2client";
fifoname += toString(client_id);
string fifoclient = "/tmp/client2server";
fifoclient += toString(client_id);
if(mkfifo(fifoclient.c_str(), 0666) == -1)
{
perror("mkfifo client2server");
fprintf(stderr, "%s\n", strerror(errno));
exit(1);
}
if((client2server = open(fifoclient.c_str(), O_RDONLY)) == -1)
{
perror("open client2server");
exit(1);
}
if(mkfifo(fifoname.c_str(), 0666) == -1)
{
perror("mkfifo server2client");
exit(1);
}
if((server2client = open(fifoname.c_str(), O_WRONLY)) == -1)
{
perror("open server2client");
exit(1);
}
for(;;)
{
if ((bytes_read = read(client2server, buffer, buffsize))== -1)
{
perror("read");
exit(1);
}
else if (bytes_read == 0)
{
fprintf(stdout, "Connection closed\n");
break;
}
buffer[bytes_read] = '\0';
if (strcmp("\\q",buffer)==0)
{
fprintf(stdout, "Server OFF.\n");
break;
}
fprintf(stdout, "From client: %s\n", buffer);
if ((write(server2client,buffer, strlen(buffer)))== -1)
{
perror("write");
break;
}
fprintf(stdout, "Send to client: %s\n", buffer);
// clean buffer from any data
memset(buffer, 0, sizeof(buffer));
}
close(client2server);
close(server2client);
unlink(fifoname.c_str());
unlink(fifoclient.c_str());
}
int main(int argc, char **argv)
{
int clientsCounter = 0;
fprintf(stdout, "Server is working ...\n");
while (1)
{
pthread_t thread;
pthread_create(&thread, NULL, clients_service, (void*)&clientsCounter);
}
return 0;
}
像这样编译它:g++ -Wall -pthread echoserver.cpp -o echoserver
回声客户端.c
#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/client2server";
int server_to_client;
char *myfifo2 = "/tmp/server2client";
char str[255];
/* write str to the FIFO */
client_to_server = open(myfifo, O_WRONLY);
server_to_client = open(myfifo2, O_RDONLY);
while(1)
{
printf("\n> ");
scanf("%s", str);
write(client_to_server, str, sizeof(str));
read(server_to_client,str,sizeof(str));
printf("From server: %s\n",str);
}
close(client_to_server);
close(server_to_client);
/* remove the FIFO */
unlink("/tmp/server2client");
unlink("/tmp/client2server");
return 0;
}
像这样编译它:gcc echoclient.c -o echoclient