我想使用命名管道編寫echo客戶端服務器應用程序(我使用套接字完成了工作,但現在我想要 對命名管道執行相同操作。)服務器必須是多線程的。C和C++中的簡單回聲客戶端服務器
我以爲我的程序在我第一次運行時就工作了,這一切都很好。連接到服務器的每個客戶端 都收到來自它的消息。
但是現在,當我試圖運行第二次節目,我仍然得到這個消息:
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
如何解決它的工作,因爲我想要它?製作一個真正的多線程回顯服務器客戶端應用程序?
繼承人我的代碼:
echoserver.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
echoclient.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
不要將已存在的文件視爲錯誤。 –
客戶端只使用「無編號」的fifos。這預計如何工作? – alk
@alk你知道如何給客戶提供相同的號碼嗎?我不知道如何使它 –