2

I have a C++ server application and C# client application, and I want them to communicate with each other using named pipes mechanism.

I want to use a multithreded server (each tread will service a client), I found a working code here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365588(v=vs.85).aspx

It works great with the C++ client from here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365592(v=vs.85).aspx

My goal is to make this C++ server to work with C# client, so I wrote this simple program for the client side:

public static void Main(string[] Args)
{
    //Client
    var client = new NamedPipeClientStream(".", "mynamedpipe", PipeDirection.InOut);
    client.Connect();

    StreamReader reader = new StreamReader(client, Encoding.Unicode);
    StreamWriter writer = new StreamWriter(client, Encoding.Unicode);
    int i = 0;
    while (true)
    {
        writer.WriteLine("Message" + i.ToString() + Char.MinValue);
        writer.Flush();
        i++;

        System.Console.WriteLine(reader.ReadLine());

        Console.ReadKey();
    }
}

The behavior I expect is :

Client->Server: "Message1"
Server->Client: "default answer from server"
Client->Server: "Message2"
Server->Client: "default answer from server"
Client->Server: "Message3"
Server->Client: "default answer from server"
Client->Server: "Message4"
Server->Client: "default answer from server"

And so on...

Instead, this is the output I get:

Pipe Server: Main thread awaiting client connection on \.\pipe\mynamedpipe Client connected, creating a processing thread.

Pipe Server: Main thread awaiting client connection on \.\pipe\mynamedpipe InstanceThread created, receiving and processing messages.

Client Request String:"????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????"

Client Request String:"Message0"

After some debugging here are few weird things I noticed:

  1. client.Connect(); causes 2 bytes (not null terminated) to be written to the pipe. this explains the "?????..."
  2. "Messege0" gets written correctly from the client side and the server reads it correctly also, but:
  3. Client side does not seem to read the reply from the server ("default answer from server") correctly - it is stuck on the blocking "reader.ReadLine()" line. I do not think it is because of a bug on the server as the same server seems to work fine with the C++ client.
  4. After closing the server, the message gets to the client. I added

    FlushFileBuffers(hPipe);

after

WriteFile()  

in the server - it did not help.

  • Encoding seems to be correct on both sides - Unicode, Little Endien.

  • Both server and client running on the same Windows machine.

The issue in item number 3, is the most disturbing to me, and I cannot find an explanation.

Yan4321
  • 329
  • 1
  • 5
  • 17
  • Are both client and server running on the same windows machine? – nakiya Sep 05 '14 at 03:17
  • Yes, they are. Added that fact to my original question. – Yan4321 Sep 05 '14 at 03:25
  • You can try and read bytes instead of strings and check what you actually receive. – nakiya Sep 05 '14 at 03:28
  • The issue is that I receive what I expect, but only when I close the server (and the pipe gets disconnected). until then - excecution is stuck on reader.ReadLine() command which (I think) means client does not see anything in the pipe. You would think that this is a problem on the server (not flushing or something along theses lines) but this exact server works fantastically with the C++ client I linked. – Yan4321 Sep 05 '14 at 03:33
  • The ReadLine() call completes when StreamReader reads a line terminator ("\r\n", just "\n" in C code) or the stream is closed. The C++ code doesn't send "\n". – Hans Passant Sep 05 '14 at 03:50
  • The "????" junk still bothers me. The issue is that after client first flush (even without writing anything to the pipe), when the server reads the pipe - it reads a sequence of ? signs. I can ignore the first message on the server side, but it would be nice to know why that happens. – Yan4321 Sep 05 '14 at 20:13

0 Answers0