I'm trying to establish a named pipe (2 way) between a python 2.7 and a C# application. It works fine for byte mode, but when I'm change to message mode in the python server side the C# client claims it's still byte mode.
Here is my python code:
import win32pipe, win32file
p = win32pipe.CreateNamedPipe("\\\\.\\pipe\\test_pipe",
win32pipe.PIPE_ACCESS_DUPLEX,
win32pipe.PIPE_TYPE_MESSAGE|win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT,
1, 65536, 65536,300,None)
print("Waiting for connection");
win32pipe.ConnectNamedPipe(p, None)
print("Client has connected")
win32file.WriteFile(p, "TestMessage".encode('utf-8'))
p.close()
My C# -Code:
class Program
{
static void Main(string[] args)
{
using (NamedPipeClientStream clientStream = new NamedPipeClientStream(".", "test_pipe", PipeDirection.InOut))
{
clientStream.Connect();
Console.WriteLine("Connected");
string inMessage = ProcessSingleReceivedMessage(clientStream);
Console.WriteLine("in Message " + inMessage);
}
Console.ReadLine();
}
private static string ProcessSingleReceivedMessage(NamedPipeClientStream namedPipeClient)
{
Console.WriteLine("Transmission mode: " + namedPipeClient.TransmissionMode);
StringBuilder messageBuilder = new StringBuilder();
string messageChunk = string.Empty;
byte[] messageBuffer = new byte[5];
do
{
namedPipeClient.Read(messageBuffer, 0, messageBuffer.Length);
messageChunk = Encoding.UTF8.GetString(messageBuffer);
messageBuilder.Append(messageChunk);
messageBuffer = new byte[messageBuffer.Length];
}
while (!namedPipeClient.IsMessageComplete);
return messageBuilder.ToString();
}}
For simplification I've only posted the transmission from python to C#, because here the error appears here. The c# client for some reason thinks the transmission is Byte, hence an InvalidOperationException occurs saying "ReadMode is not PipeTransmissionMode.Message." What did I do wrong.