title: c# named pipe two way communcation
body: <p>I have two programs written in c++ and c#.I want to establish a two way communication using named-pipe between them. The C# client program can be connected to the named-pipe created by c++ server program.But nothing received in both ends.</p>
  
  <p>Here is the c++ part (Server):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;windows.h&gt;
  #include &lt;stdlib.h&gt;
  #define UNICODE
  using namespace std;
  HANDLE hnamedPipe = INVALID_HANDLE_VALUE;
  BOOL Finished =false;
  HANDLE hThread = NULL;
  unsigned long __stdcall CS_RcvThr(void * pParam) ;
  int main(int argc, char **argv)
  {
      hnamedPipe = CreateNamedPipe(
          "\\\\.\\pipe\\vikeyP",
          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;
              }
          }
          else
          {
              cout&lt;&lt;"Connected!"&lt;&lt;endl;
              hThread = CreateThread( NULL, 0, &amp;CS_RcvThr, NULL, 0, NULL);
              if(hThread) cout&lt;&lt;"read thread created"&lt;&lt;endl; else cout&lt;&lt;"cant crat rd thed\n";
              break;
          }
      }
  
      while(1)
      {
          cout&lt;&lt;"lst loop"&lt;&lt;endl;
          //Send over the message
          char chResponse[] = "hello\n";
          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;
          Sleep(10);
      }
  }
  
  unsigned long __stdcall CS_RcvThr(void * pParam) {
      BOOL fSuccess; 
      char chBuf[100];
      DWORD dwBytesToWrite = (DWORD)strlen(chBuf);
      DWORD cbRead;
      int i;
  
      while (1)
      {
          fSuccess =ReadFile( hnamedPipe,chBuf,dwBytesToWrite,&amp;cbRead, NULL); 
          if (fSuccess)
          {
              printf("C++ App: Received %d Bytes : ",cbRead);
              for(i=0;i&lt;cbRead;i++)
                  printf("%c",chBuf[i]);
              printf("\n");
          }
          if (! fSuccess &amp;&amp; GetLastError() != ERROR_MORE_DATA) 
          {
              printf("Can't Read\n");
              if(Finished)
                  break;
          }
      }
  }
  </code></pre>
  
  <p>Here is the C# part (Client):</p>
  
  <pre><code>    private Thread vikeyClientThread;
      public void ThreadStartClient()
      {
          Console.WriteLine("Thread client started ID ={0} name = {1} " ,
          Thread.CurrentThread.ManagedThreadId,Thread.CurrentThread.Name);
          using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", "vikeyP"))
          {
              // The connect function will indefinately wait for the pipe to become available
              // If that is not acceptable specify a maximum waiting time (in ms)
              Console.WriteLine("Connecting to ViKEY server...");
              pipeStream.Connect();
              Console.WriteLine("Connected :)");
  
              //Write from client to server
              StreamWriter sw = new StreamWriter(pipeStream);
              while (true)
              {
                  //Read server reply
                  StreamReader sr = new StreamReader(pipeStream);
                  string temp = "";
                  sw.WriteLine(System.DateTime.Now);
                  byte[] c = new byte[200];
                  temp = sr.ReadLine();  
                  pipeStream.Read(c, 0, c.Length);
                  Console.WriteLine("RX =:{0}", Encoding.UTF8.GetString(c, 0, c.Length));
                  Thread.Sleep(500);
              }
          }
          Console.WriteLine("Vikey pipe Closed");
          Console.WriteLine("Thread with ID ={0} name = {1} is closed.",
          Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.Name);
      }
  </code></pre>