Reputation: 133
I have two executables: client.exe and server.exe.
They communicate with each other by using named pipes. They work fine when the server only listens client requests and sends response to the client.
But now the requirement is both exes contain client pipe and server pipe. So each exe contains two methods: serverpipe()
and clientpipe()
. Those methods are in a thread and do not communicate to each other.
First client and server communicate properly.
Client.EXE:
public static void Main(string[] Args)
{
PipeClient obj=new PipeClient();
obj.client();
Thread startserver = new Thread(new ThreadStart(obj.server));
startserver.Start();
}
public void client()
{
int cnt =0;
while (true)
{
System.IO.Pipes.NamedPipeClientStream pipeClient = new System.IO.Pipes.NamedPipeClientStream(".", "\\\\.\\pipe\\SamplePipe1", System.IO.Pipes.PipeDirection.InOut, System.IO.Pipes.PipeOptions.None);
if (pipeClient.IsConnected != true) { pipeClient.Connect(); }
StreamReader sr = new StreamReader(pipeClient);
StreamWriter sw = new StreamWriter(pipeClient);
string temp;
temp = sr.ReadLine();
if (temp == "Waiting")
{
try
{
cnt++;
sw.WriteLine("clientSide Message " + cnt);
sw.Flush();
Thread.Sleep(10000);
}
catch (Exception ex) { throw ex; }
}
else
{
}
pipeClient.Close();
}
}
public void server()
{
StreamWriter sw1 = new StreamWriter("C:\\ServerEXE\\serverdataFile0.txt", true);
System.IO.Pipes.NamedPipeServerStream pipeServer = new System.IO.Pipes.NamedPipeServerStream("\\\\.\\pipe\\SamplePipeSend1", System.IO.Pipes.PipeDirection.InOut, 4);
sw1 = new StreamWriter("C:\\ServerEXE\\serverdataFile2.txt", true);
sw1.Close();
StreamReader sr = new StreamReader(pipeServer);
StreamWriter sw = new StreamWriter(pipeServer);
do
{
try
{
pipeServer.WaitForConnection();
string test;
sw.WriteLine("Waiting");
sw.Flush();
pipeServer.WaitForPipeDrain();
test = sr.ReadLine();
sw1 = new StreamWriter("C:\\ServerEXE\\serverdataFile2.txt",true);
sw1.WriteLine(test);
sw1.Close();
}
catch (Exception ex)
{ throw ex; }
finally
{
pipeServer.WaitForPipeDrain();
if (pipeServer.IsConnected) { pipeServer.Disconnect(); }
}
} while (true);
}
Server.EXE:
public static void Main()
{
PipeServer obj = new PipeServer();
obj.server();
Thread startserver = new Thread(new ThreadStart(obj.client));
startserver.Start();
}
public void server()
{
System.IO.Pipes.NamedPipeServerStream pipeServer = new System.IO.Pipes.NamedPipeServerStream("\\\\.\\pipe\\SamplePipe1", System.IO.Pipes.PipeDirection.InOut, 4);
StreamReader sr = new StreamReader(pipeServer);
StreamWriter sw = new StreamWriter(pipeServer);
do
{
try
{
pipeServer.WaitForConnection();
string test;
sw.WriteLine("Waiting");
sw.Flush();
pipeServer.WaitForPipeDrain();
test = sr.ReadLine();
Console.WriteLine(test);
}
catch (Exception ex)
{ throw ex; }
finally
{
pipeServer.WaitForPipeDrain();
if (pipeServer.IsConnected) { pipeServer.Disconnect(); }
}
} while (true);
}
public void client()
{
int cnt = 0;
while (true)
{
System.IO.Pipes.NamedPipeClientStream pipeClient = new System.IO.Pipes.NamedPipeClientStream(".", "\\\\.\\pipe\\SamplePipeSend1", System.IO.Pipes.PipeDirection.InOut, System.IO.Pipes.PipeOptions.None);
if (pipeClient.IsConnected != true) { pipeClient.Connect(); }
StreamReader sr = new StreamReader(pipeClient);
StreamWriter sw = new StreamWriter(pipeClient);
string temp;
temp = sr.ReadLine();
if (temp == "Waiting")
{
try
{
cnt++;
sw.WriteLine("clientSide Message " + cnt);
sw.Flush();
Thread.Sleep(10000);
}
catch (Exception ex) { throw ex; }
}
else
{
pipeClient.Close();
}
}
}
Upvotes: 0
Views: 5546
Reputation: 9249
See inline comments:
public static void Main ( string[] Args )
{
PipeClient obj = new PipeClient ();
obj.client (); // <-- This has an endless loop and won't return
// So the following lines will never be executed!
Thread startserver = new Thread ( new ThreadStart ( obj.server ) );
startserver.Start ();
}
Just start the thread before entering the endless loop:
public static void Main ( string[] Args )
{
PipeClient obj = new PipeClient ();
Thread startserver = new Thread ( new ThreadStart ( obj.server ) );
startserver.Start ();
obj.client (); // Thread already running, so it's ok if this is endless
}
Of course, this needs to be done in server as well in client exe-
Upvotes: 5
Reputation: 45272
This sounds like a permissions issue.
To create a globally accessible named pipe, the application needs the SeCreateGlobalPrivilege
permission.
If Windows7 has UAC turned on, and the client is not running in elevated mode, then it's attempt to create a global named pipe will (silently) fail.
Some reading:
Minimum OS Permissions required to create named pipe (WCF)
The examples focus on WCF, but in this case WCF is just a layer on top of the named pipe communication, and the source of the problems are the same.
Upvotes: 0