title: C++ Socket Pair Not Reading/Writing Parent/Child
body: <p>I've been assigned a project in which I need to use <strong><em>Unix Domain Sockets</em></strong> to communicate two-way between a parent and child process.  My original approach was to create a child and server, but I was getting some serious issues with connectivity and paths.  If anyone would prefer to see that code, I'm willing to provide it.</p>
  
  <p>Anyway, I'm reaching the line in my program where I print out "parent has written", although I'm not entirely convinced that it has because no subsequent reading occurs.  I've worked with closing the pipes in different places and changing how I'm reading/writing.  </p>
  
  <p>I am now using <strong><em>socketpair()</em></strong> to set up a pair of sockets.  I cannot use any other type of IPC other than sockets.  This information is brand new to me, so forgive the sloppy code.  </p>
  
  <p>Please ignore long #include list, will clean up ASAP.</p>
  
  <p>Again, my current output stops at "Parent wrote to child" and then it keeps running, does not close out, so I think it's the child read waiting to receive something.</p>
  
  <p>My program needs to be able to read and write incredibly long files, so you'll see serialization and parsing work throughout the code.  The serialization and parsing works perfectly on a different part of the project, so I know that's not the issue.  It's the darn sockets!</p>
  
  <p>Anyway, code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;vector&gt;
  #include &lt;fstream&gt;
  
  int main() {
      int sockets[2];
  
      std::cout &lt;&lt; "Please enter name of text file." &lt;&lt; std::endl;
      std::string entered_file;
      std::cin &gt;&gt; entered_file;
      std::string string_to_find;
      std::cout &lt;&lt; "Please enter the string you'd like to search for." &lt;&lt; std::endl;
      std::cin &gt;&gt; string_to_find;
  
      int rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
      if (rc &lt; 0) {
          perror("socketpair");
          exit(1);
      }
  
      pid_t p;
      p = fork();
  
      if (p == 0)  //child
      {
          char ch;
          std::string the_file_as_string = "";
          std::vector&lt;std::string&gt; lines_with_string;
  
          int r;
          while ((r = read(sockets[0], &amp;ch, 1)) &gt; 0) {
              the_file_as_string.push_back(ch);
          }
  
          std::cout &lt;&lt; "Child has read from parent..." &lt;&lt; std::endl;
  
          size_t pos = 0;
          while(the_file_as_string.find(string_to_find, pos) != std::string::npos) {
              pos = the_file_as_string.find(string_to_find, pos+string_to_find.size());
              std::string sub = the_file_as_string.substr(0,pos);
              int occurrences = 0;
              size_t pos2 = 0;
              while (sub.find("/0", pos2 ) != std::string::npos) {
                  pos2 = sub.find("/0", pos2+2);
                  ++ occurrences;
                  pos += string_to_find.length();
              }
              occurrences = occurrences;
              std::string occurrences_string = std::to_string(occurrences);
              lines_with_string.push_back(occurrences_string);
          }
          std::cout &lt;&lt; "child has parsed" &lt;&lt; std::endl;
          std::string lines_with_string_as_string = "";
          for(unsigned int i=0; i&lt;lines_with_string.size(); i++) {
               lines_with_string_as_string = lines_with_string_as_string + lines_with_string.at(i) + "/0";
           }
          int file_size = lines_with_string_as_string.size();
  
          write(sockets[0], lines_with_string_as_string.c_str(), file_size+1);
          close(sockets[0]);
          std::cout &lt;&lt; "child has sent back to parent..." &lt;&lt; std::endl;
      }
      else  //parent
      {
          close(sockets[0]);
          std::ifstream myfile;
  
          //open file
          myfile.open(entered_file.c_str());
  
          if (!myfile) {
              std::cerr &lt;&lt; "Unable to open file datafile.txt" &lt;&lt; std::endl;
              return 1;   // call system to stop
          }
  
  
          //here, pass lines to child one by one and return true or false based on if it finds the string
          char lines_with_string[100];
          char ch;
          std::vector&lt;std::string&gt; the_file;
          std::vector&lt;int&gt; final_line_numbers;
          std::vector&lt;std::string&gt; final_lines;
          std::string str;
           while (std::getline(myfile, str))
           {
               the_file.push_back(str);
           }
           std::string the_file_as_string = "";
  
           for(unsigned int i=0; i&lt;the_file.size(); i++) {
               the_file_as_string = the_file_as_string + the_file.at(i) + "/0";
           }
  
           int big_size = the_file_as_string.size();
           write(sockets[1], the_file_as_string.c_str(), big_size+1);
  
           std::cout &lt;&lt; "Parent wrote to the child..." &lt;&lt; std::endl;
  
           wait(NULL);
  
          std::string lines_with_string_as_string = "";
          int r;
          while ((r = read(sockets[1], &amp;ch, 1)) &gt; 0) {
              lines_with_string_as_string.push_back(ch);
          }
          close(sockets[1]);
          std::cout &lt;&lt; "Parent has read from child..." &lt;&lt; std::endl;
  
          std::string delimit = "/0";
          size_t pos3 = 0;
          while ((pos3 = lines_with_string_as_string.find(delimit)) != std::string::npos) {
              std::string token = lines_with_string_as_string.substr(0, pos3);
              int token2 = std::stoi(token);
              final_line_numbers.push_back(token2);
              lines_with_string_as_string.erase(0, pos3 + delimit.length());
          }
          //match line numbers to array of lines (original)
          for(unsigned int i=0; i &lt; final_line_numbers.size(); i++) {
              int find_int = final_line_numbers.at(i);
              std::string find_string = the_file.at(find_int-1);
              std::cout &lt;&lt; find_string &lt;&lt; std::endl;
              final_lines.push_back(find_string);
          }
  
          std::sort(final_lines.begin(), final_lines.end());
          std::cout &lt;&lt; "The final outcome with lines containing " &lt;&lt; "'" &lt;&lt; string_to_find &lt;&lt; "' are:" &lt;&lt; std::endl;
          for(unsigned int i=0; i&lt;final_lines.size(); i++) {
              std::cout &lt;&lt; final_lines.at(i) &lt;&lt; std::endl;
          }
           myfile.close();
           std::cout &lt;&lt; "Program has exited completely." &lt;&lt; std::endl;
      }
  }
  </code></pre>
  
  <p>MINI VERSION (same issue, says "Parent has written to child..." and then doesn't do anything further, and also doesn't exit:  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;vector&gt;
  #include &lt;fstream&gt;
  
  int main() {
  
      int sockets[2];
  
      pid_t p;
  
  
      socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
  
      p=fork();
  
      if (p &lt; 0) {
          perror("forking");
      }
      else if(p &gt; 0) {                                                //parent
          std::string long_string_to_send = "ladjfldjsfljasdfj adjlkjadlsjf a fljasdfladj ljdl aljfajdfljadfadsflajd  ajfdlkjslfjadj faldjsljalsdfljdfljadsl jladsfjasflajdflkajfl dasl fjalfjldjsfladsflajdlsfald fjljdfljadfjadfjl djfljadlfj aldjl hello";
          int big_size = long_string_to_send.size();
  
          write(sockets[1], long_string_to_send.c_str(), big_size+1);
          std::cout &lt;&lt; "Parent has written to child..." &lt;&lt; std::endl;
  
          wait(NULL);
  
          std::string lines_with_string_as_string = "";
          int r;
          char ch;
          while ((read(sockets[1], &amp;ch, 1)) &gt; 0) {
              lines_with_string_as_string.push_back(ch);
          }
          close(sockets[1]);
          std::cout &lt;&lt; "Parent has received from child..." &lt;&lt; std::endl;
          std::cout &lt;&lt; "Parent has read the following... " &lt;&lt; lines_with_string_as_string &lt;&lt; std::endl;
          std::cout &lt;&lt; "Program has exited completely" &lt;&lt; std::endl;
      }
      else {                                                          //child
          char ch;
          std::string the_file_as_string = "";
  
          int r;
          while ((read(sockets[0], &amp;ch, 1)) &gt; 0) {
              the_file_as_string.push_back(ch);
          }
  
          std::cout &lt;&lt; "Child has read from parent..." &lt;&lt; std::endl;
          std::cout &lt;&lt; "Child has read the following... " &lt;&lt; the_file_as_string &lt;&lt; std::endl;
          int file_size = the_file_as_string.size();
  
          write(sockets[0], the_file_as_string.c_str(), file_size+1);
          close(sockets[0]);
      }
  }
  </code></pre>