Andrei Karcheuski
Andrei Karcheuski

Reputation: 3306

System.IO.Exception: Pipe is broken on client side

I have two windows applications and using named pipes to send and receive lists of data between them. With serialization/deserialization mechanism using BinaryFormatter class.

Server:

  static void StartServer()
    {
        var server = new NamedPipeServerStream("PipesEnroll", PipeDirection.InOut);

            while (true)
            {
                StreamReader reader = new StreamReader(server);
                StreamWriter writer = new StreamWriter(server);
                string terminalTemplate;
                string matcherTemplate;
                int mathVersionNumber = 9;
                int numberFingers;
                BinaryFormatter formatterDeserialize = new BinaryFormatter();
                List<byte[]> retrievedList = (List<byte[]>)formatterDeserialize.Deserialize(reader.BaseStream);
                bool isOk = Enroll.EnrollWithoutWCF(retrievedList, mathVersionNumber, out terminalTemplate, out matcherTemplate, out numberFingers);
                List<String> sendList = new List<string>();
                sendList.Add(isOk.ToString());
                sendList.Add(terminalTemplate);
                sendList.Add(matcherTemplate);
                sendList.Add(numberFingers.ToString());
                BinaryFormatter formatterSerialize = new BinaryFormatter();
                formatterSerialize.Serialize(writer.BaseStream, sendList);
                server.Disconnect();
       }

Client:

                    using (var client = new NamedPipeClientStream(".", "PipesEnroll", PipeDirection.InOut))
                        {
                            client.Connect();
                            StreamReader reader = new StreamReader(client);
                            StreamWriter writer = new StreamWriter(client);

                                BinaryFormatter formatterSerialize = new BinaryFormatter();
                                formatterSerialize.Serialize(writer.BaseStream, images);
                                // writer.Write(stream);
                                // writer.Flush();
                                BinaryFormatter formatterDeserialize = new BinaryFormatter();
                                List<String> retrievedList = (List<String>)formatterDeserialize.Deserialize(reader.BaseStream);
                                bool isOK = Convert.ToBoolean(retrievedList[0]);
                                string terminalTemplate = retrievedList[1];
                                string matcherTemplate = retrievedList[2];
                                int numberFingers = Convert.ToInt32(retrievedList[3]);

}

But exception "System.IO.Exception: Pipe is broken" appears on client side when executing formatterSerialize.Serialize(writer.BaseStream, images); in debug mode.

Does anybody have any suggestion on how to avoid this issue?

Upvotes: 0

Views: 5335

Answers (1)

Yonathan Druck
Yonathan Druck

Reputation: 21

StreamWriter belongs to NamedPipeClientStream. If NamedPipeClientStream is disposed part of its process is disposing StreamWriter as well. If for any reason StreamWriter is Closed/Disposed in any other part of the code the result will be pipe is broken.

1) Check that formatterSerialize.Serialize is not closing the StreamWriter.

2) Also make sure to flush StreamWriter before exiting Using of NamedPipeClientStream.

Upvotes: 1

Related Questions