为我令人困惑的描述道歉。这是sn-ps的代码。
服务器端:
#define FIFO_FILE "/tmp/fifo"
while (1)
{
char readbuf[25];
FILE *file = fopen(FIFO_FILE, "r");
fgets(readbuf, 25, file);
std::queue<std::string>().swap(msg);
msg.push(readbuf);
while (!feof(file))
{
fgets(readbuf, 25, file);
msg.push(readbuf);
}
std::cout << "message num: " << msg.size() << std::endl;
std::future<void> ret = std::async (std::launch::async, command_process, std::ref(msg), std::ref(binfo));
fclose(file);
std::cout << "messages are being processed!\n";
}
客户端:
int main(int argc, char *argv[])
{
FILE *fp;
if((fp = fopen(FIFO_FILE, "w")) == NULL) {
perror("fopen \n");
return -1;
}
fputs(argv[1], fp);
fclose(fp);
return 0;
}
服务器启动后,在 fgets 处等待。当客户端发送一个字符串到FIFO,服务端接收到这个字符串并存储在msg中,
然后传给线程command_process处理,服务器等待
在 fopen 获取更多信息。 但是如果客户端再次尝试发送消息,它将在客户端的 fopen 处被阻塞,直到服务器的线程完成处理,服务器继续接收客户端的消息。
我希望客户端不应该被阻止发送消息并且服务器应该立即接收消息,然后中止先前的消息以处理新消息。
启