Patrik
Patrik

Reputation: 1336

Reconnect to NamedPipeClientStream after connection lost (.NET)

I built two applications. One is Client and the other is a Server.

They are communicating through Named Pipes. The client creates the NamedPipeClientStream. Everything works fine except when the server closes the connection or if the server aplication terminates.

After termination the pipestream on the client part sets the IsConnected property to false. This is ok. But If I tried to reopen the NamedPipeClientStream it never succeedes. After restarting the server I expected from the client to open the connection but this never happens.

Every time it tries to reconnect the TimeOutException is raised. The only solution is to exit from the application and start it again. But this is not an option for me.

Anybody please help!

Upvotes: 3

Views: 5470

Answers (1)

Patrik
Patrik

Reputation: 1336

Never mind.

The problem was in not closing the read pipe on clinet side after disconnection from the server.

The solution was

try{
   while(true){
      ... Reading from the PIPE ...
   }
}
finally{
 pipeStream.Close();
 pipeStream.Dispose();
 IsRunning = false;
}

NEVER FORGET TO CLOSE AND DISPOSE THE STREAMS !!!

Upvotes: 5

Related Questions