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:
- client.Connect(); causes 2 bytes (not null terminated) to be written to the pipe. this explains the "?????..."
- "Messege0" gets written correctly from the client side and the server reads it correctly also, but:
- 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.
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.