title: How to work with named pipes (C++ server , C# client)
body: <p>I am trying to get started with working with named pipes as I will need to use them for a project of mine in the future. </p>
  
  <p>At the moment I have a C++ server which waits until a client connects and sends over a test message. I roughly followed this <a href="http://code.msdn.microsoft.com/windowsdesktop/CppNamedPipeServer-d1778534" rel="nofollow">tutorial</a> to get started. The relevant code is below:</p>
  
  <pre><code>    #define MESSAGE L"TestMessage"
  
  HANDLE hnamedPipe = INVALID_HANDLE_VALUE;
  
  hnamedPipe = CreateNamedPipe(
      L"\\\\.\\pipe\\testpipe",
      PIPE_ACCESS_DUPLEX,
      PIPE_TYPE_MESSAGE|
      PIPE_READMODE_MESSAGE|
      PIPE_WAIT,
      PIPE_UNLIMITED_INSTANCES,
      1024,
      1024,
      NMPWAIT_USE_DEFAULT_WAIT,
      NULL);
  
  if(hnamedPipe == INVALID_HANDLE_VALUE)
  {
          cout &lt;&lt; "Failed" &lt;&lt; endl;
  }
  
  while(true)
  {
      cout&lt;&lt; "Waiting for client"&lt;&lt; endl;
  
      if(!ConnectNamedPipe(hnamedPipe,NULL))
      {
          if(ERROR_PIPE_CONNECTED != GetLastError())
          {
          cout &lt;&lt; "FAIL"&lt;&lt; endl;
          }
      }
  
      cout&lt;&lt;"Connected!"&lt;&lt;endl;
  
      //Send over the message
      wchar_t chResponse[] = MESSAGE;
      DWORD cbResponse,cbWritten;
      cbResponse = sizeof(chResponse);
  
      if(!WriteFile(
      hnamedPipe,
      chResponse,
      cbResponse,
      &amp;cbWritten,
      NULL))
      {
          wprintf(L"failiure w/err 0x%08lx\n",GetLastError);
      }
      cout&lt;&lt;"Sent bytes :)" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>The client code (C#) is below:</p>
  
  <pre><code>        using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut))
          {
              while (true)
              {
                  Console.WriteLine("Connecting to server...");
                  pipeClient.Connect();
  
                  Console.WriteLine("Connected :)");
                  Console.WriteLine(pipeClient.ReadByte());
                  pipeClient.Close();
                  Console.WriteLine("Closed");
              }
  
          }
  </code></pre>
  
  <p>At the moment I have got the client to connect successfully to the server and it prints out the first byte. I want to know how to do 2 things:</p>
  
  <ol>
  <li><p>Read the entire message - I tried using StreamReader over the pipeClient to read the message but it hangs on ReadLine() indefinitely.</p></li>
  <li><p>Continuously send over messages - I want the server to send message after message to the client which will read them in one at a time and print them out. I am a bit clueless about IPC so at first I tried to make the client disconnect and reconnect to the server in the while(true) loop whilst the server is in a while true loop which at the top always waits for a new client connection before sending another message. My attempt at this is in the code above.</p></li>
  </ol>
  
  <p>Any help with this would be greatly appreciated. Eventually the aim is to be sending over images from the server to the client. The client would then print them out to the screen in real-time. I wanted to get this working with simple string messages before I tried the image data.</p>
  
  <p>EDIT:</p>
  
  <p>Eventually I want to be able to send a message from the client to the server indicating it wants to get the latest image frame, the server will then send over the latest frame which the client will then display on screen. So the flow is:</p>
  
  <ol>
  <li>Client -> Server : indicator that client wants the latest frame info. (Something simple, maybe an unsigned int with the value 1)</li>
  <li>Server -> Client : Latest frame info. (640x480 image stored in a byte array with RGB byte values)</li>
  <li>Client : Display the frame on the display.</li>
  </ol>