I've been trying to get a C++ client to send a message to a C# server and I'm having difficulty on the C++ client side. I'm using the sample C++ client shown here with no changes at all, 100% copy/pasted right now. The problem seems to be in the line where I'm trying to set the Read Mode with SetNamedPipeHandleState() to PIPE_READMODE_MESSAGE. When I have the client try to connect to the server it stops and prints "SetNamedPipeHandleState failed. GLE=87".
I heard that setting the read mode to Message can fail if the pipe is set to Byte mode on the server end but pipes are set to Message mode by default in C# anyway. I manually set the read mode of the C# server anyway with "pipeServer.ReadMode = PipeTransmissionMode.Message;" just incase and that caused the server to crash when the client attempted to connect.
The closest I've gotten to making this work is setting the Readmode on the C++ client end to Byte with "dwMode = PIPE_READMODE_BYTE;" which allows the client to successfully send a message to the server, albeit a bit messed up. The text I send in Byte mode has spaces between every letter which is obviously not what I want.
The server code I'm using is this:
using System;
using System.IO;
using System.IO.Pipes;
class PipeServer
{
static void Main()
{
ListenServer();
}
static void ListenServer()
{
bool keepRunning = true;
while (keepRunning)
{
using (NamedPipeServerStream pipeServer =
new NamedPipeServerStream("testpipe", PipeDirection.InOut))
{
//Console.WriteLine("NamedPipeServerStream object created.");
// Wait for a client to connect
Console.WriteLine("Pipe Server");
Console.WriteLine("Waiting for client connection...");
pipeServer.WaitForConnection();
//pipeServer.ReadMode = PipeTransmissionMode.Message;
Console.WriteLine("Client connected.");
try
{
using (StreamReader sr = new StreamReader(pipeServer))
{
// Display the read text to the console
string temp;
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("Received from client: {0}", temp);
}
}
}
// Catch the IOException that is raised if the pipe is
// broken or disconnected.
catch (IOException e)
{
Console.WriteLine("ERROR: {0}", e.Message);
}
}
}
}
}
Thanks for reading if you've made it this far, the help is very much appreciated!