Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception thrown by BeginWaitForConnection when reusing a NamedPipeServerStream in .NET 4 after client disconnects

I'm trying to use NamedPipeServerStream to create a named pipe server in .Net 4. I'm using BeginWaitForConnection to wait for the connection, so that I can abort the wait if the server is to be shut down.

Everything works well for the first client --- the connection is acknowledged, the data received, and the response sent OK. However, after the client disconnects everything breaks. I'm calling BeginWaitForConnection again to wait for a new connection, but this is throwing an IOException saying that the "pipe is broken".

How can I wait for a second client on the same pipe?

like image 445
Anthony Williams Avatar asked May 18 '11 13:05

Anthony Williams


1 Answers

Create a new instance of NamedPipeServerStream specifying the same pipe, and call BeginWaitForConnection on that.

i.e. Don't try to reuse a NamedPipeServerStream object for different clients: one instance should service one client connection/conversation, then be disposed.

See also Multithreaded NamePipeServer in C#

like image 150
Chris Dickson Avatar answered Sep 23 '22 15:09

Chris Dickson