Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么read()没有阻塞,或者为什么write()发送空白?_C++_Named Pipes_Blocking - Fatal编程技术网

C++ 为什么read()没有阻塞,或者为什么write()发送空白?

C++ 为什么read()没有阻塞,或者为什么write()发送空白?,c++,named-pipes,blocking,C++,Named Pipes,Blocking,这是给HW的关于我项目的一点背景资料。我正在编写一个客户机/服务器程序。我的客户机将生成我的服务器进程,然后通过命名管道向服务器发送文件名和搜索项。我的服务器将在文件中搜索搜索词的实例,并返回找到它的信息。在我的最后两行之前,它一直运行良好。在搜索整个文件后,服务器将告诉客户端搜索词的总发现次数。然后,客户机将以特殊格式的行输出此信息。为了告诉我的客户,是时候使用这个特殊的“总计”行了,我让我的服务器发送“最后一行”。客户机应该从管道中检索,进入一个条件语句来处理特殊的行,然后从管道中从服务器获

这是给HW的关于我项目的一点背景资料。我正在编写一个客户机/服务器程序。我的客户机将生成我的服务器进程,然后通过命名管道向服务器发送文件名和搜索项。我的服务器将在文件中搜索搜索词的实例,并返回找到它的信息。在我的最后两行之前,它一直运行良好。在搜索整个文件后,服务器将告诉客户端搜索词的总发现次数。然后,客户机将以特殊格式的行输出此信息。为了告诉我的客户,是时候使用这个特殊的“总计”行了,我让我的服务器发送“最后一行”。客户机应该从管道中检索,进入一个条件语句来处理特殊的行,然后从管道中从服务器获取总的总数

这是我的问题:我的客户机正在识别“最后一行”输入并进入正确的条件语句,但是当它从管道中获取总的总数时,它总是返回空白。如果管道里什么都没有,它不应该堵塞吗?如果这不是问题,为什么/如何将坯料放入管道?我有一份声明证明,总的总数正在进入管道

客户:

...
char endMessage[] ="Server-EOF";
        read(fd2, buffer, maxBufferSize);


        while(strcmp(buffer,endMessage) != 0)
        {       

                if(strcmp(buffer, "final line") ==0)
                {
                        read(fd2, buffer, maxBufferSize);
                        cout <<"final line buffer is:<< buffer << endl;
                        cout <<"Client : PID " <<clientPID << "- Target >>"<< requestedTarget
                                <<"<< in File >>" <<requestedFileName<< "<< " << buffer <<endl;
                }
                else{

                cout << "Client :PID " << clientPID <<" - Target >>" <<requestedTarget
                        <<"<< " << buffer << endl;
                }

                read(fd2, buffer, maxBufferSize);
        }
。。。
char endMessage[]=“服务器EOF”;
读取(fd2、缓冲区、maxBufferSize);
while(strcmp(缓冲区,结束消息)!=0)
{       
if(strcmp(缓冲区,“最终行”)==0)
{
读取(fd2、缓冲区、maxBufferSize);

cout“你不检查
read()
调用的返回值,这对你的错误检测代码来说是个坏兆头。如果你不知道
read()
是否有效,如果有效,它得到了多少数据,你就不能做很多。与
write()一样
调用。您有很多错误…例如,在读取之后,您不能只使用
strcmp()
,因为您读取数据的缓冲区可能不会以NUL结尾(即使您安排它以NUL开头,前面较长的行也可能会覆盖它们)。此外,当您发出多个写操作时,它们可能会被一条客户端读取指令读取…您不能假设每次读取都会给您一条离散的逻辑“消息”。此外,您可以测试
eof()
,但不测试getline是否成功,因此您可能会成功放弃一条后跟eof的行读取。
 size_t found;
    getline(rf,tempLine);
    while (!rf.eof())
    {
        currentLineNumber++;

            found= tempLine.find(requestedTarget);
            while(found != string::npos)
            {
                    totalTimesFoundInFile++;
                    totalTimesFoundOnLine++;
                    found=tempLine.find(requestedTarget, found+1);
            }



            if(totalTimesFoundOnLine != 0)
            {
                    int n = sprintf(stringToSend, "Appeared on Line %d,     %d times",currentLineNumber,
                                    totalTimesFoundOnLine);
                    write(fd2, stringToSend, sizeof(stringToSend));

            }

            getline(rf,tempLine);
            totalTimesFoundOnLine = 0;

    }
    int t = sprintf(stringToSend, "Appeared a Total of %d Times",totalTimesFoundInFile);
    cout <<"Final string to send: " << stringToSend <<endl;
    char finalLine[] = "final line";
    write(fd2, finalLine, sizeof(finalLine));
    cout <<"Just put 'finalLine' into pipe" <<endl;
    write(fd2, stringToSend, sizeof(stringToSend));
    cout <<"Just put final string into pipe" <<endl;



    char endingMessage[]="Server-EOF";
    write(fd2, endingMessage, sizeof(endingMessage));
Final string to send: Appeared a Total of 4 Times
Just put 'finalLine' into pipe
Just put final string into pipe
Client :PID 12684 - Target >>apple<< Appeared on Line 1,        1 times
Client :PID 12684 - Target >>apple<< Appeared on Line 2,        1 times
Client :PID 12684 - Target >>apple<< Appeared on Line 4,        2 times
final line Buffer is:
Client : PID 12684- Target >>apple<< in File >>pipedTest.txt<<