Why is read() not blocking or why is write() sending blanks?

**This is for HW** A little background on my project. I am writing a client/server program. My client is to spawn my server process and then send a filename and search term to the server through a named pipe. My server is to search the file for instances of the search term and send back that it was found. It is working fine until my last two lines. After searching the whole file, the server is to tell the client the total times the search term was found. The client will then output this information in a specially formatted line. In order to tell my client that it's time for that special "total" line, I am having my server send "last line". Client should retrieve that from the pipe, go into a conditional statement to handle the special line, and then grab from the pipe the overall total from the server.

Here is my issue: my Client is recognizing the "last line" input and going into the correct conditional statement, but when It goes to grab the overall total from the pipe it always comes back blank. If there was nothing in the pipe, shouldn't it block? If that isn't the problem, why/how are blanks getting into the pipe? I have a statement proving that the overall total is getting into the pipe

client:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    ...
    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);
            } 


server:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
     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)); 


output:

1
2
3
4
5
6
7
8
   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<< 


I tried to edit out things that were irrelevant, but if there is any clarification needed, I'll be watching this thread closely for a bit.

**EDIT** I did some checking and it looks like my server is getting all of the way through its code and returning. Could that be the issue? Does that close/clear the pipe? If so, is there a way to get my server to wait on my client before returning?
Topic archived. No new replies allowed.