views:

612

answers:

3

Hi, I am currently learning about named pipes in Windows using ASP.NET 3.5 and C#. I wrote a small server program which creates a named pipe:

using (NamedPipeServerStream pipeStream = new NamedPipeServerStream(pipeName))
{
  pipeStream.WaitForConnection();
  // do sth.
}

and a client application opening the pipe like this:

using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(pipeName))
{ 
  pipeStream.Connect();
  // do sth.
}

This works great as long as only one client connects to the pipe. It can both read and write. If I try to connect a second client, the code never exceeds the line

pipeStream.Connect();

Both the server and all clients are running on the same machine. Any ideas?

Thank you very much in advance!

+2  A: 

you can look up some of the info here: http://stackoverflow.com/questions/1266849/number-of-clients-that-can-connect-to-a-named-pipe

and here in MSDN: http://msdn.microsoft.com/en-us/library/aa365594%28VS.85%29.aspx

from what i understand, you should create a multi-threaded application. main thread should be responsible for the future connections, and each new connection should be launched in new thread.

ifesdjeen
A: 

A real pipe (that one from the hardwre store) has two endings. So I'd expect that the named pipe also has exactly two endpoints, one for the server process and one for the client process.

However, no matter what's the final answer, I'd suggest to have a look at WCF - it supports net pipe binding (and lots of others like binary TCP, SOAP with or without WS-* specs just by changing the config) and handles the server activation and multi threading for you. .net 3.5 is available, so I'd definitely opt for it.

Marc Wittke
A: 

Thanks for the fast help.

I already handled the real processing in separate threads but forgot to mention that. A co-worker found the problem though:

I had a StreamReader enclosed by another using block in the posted NamedPipeServerStream-using block on the server side.

When this block finished closing the StreamReader, it also disconnected the NamedPipeServerStream for some reason. Also I wasn't enclosing the pipeStream.WaitForConnection() in a loop.

SimonW
so, you may close the question))
ifesdjeen
I can accept my answer tomorrow ;)
SimonW