System.IO.Exception: Pipe is broken

后端 未结 3 806
夕颜
夕颜 2021-02-02 11:20

I have two .NET applications that talk to each other over a named pipe. Everything is great the first time through, but after the first message is sent, and the server is going

3条回答
  •  一整个雨季
    2021-02-02 12:01

    I'll post my code that seems to work - I was curious since I never did anything with pipes. I didn't find the class you name for the server-side in the relevant namespace, so here's the code based on the NamedPipeServerStream. The callback stuff is just because I couldn't be bothered with two projects.

    NamedPipeServerStream s = new NamedPipeServerStream("p", PipeDirection.In);
    Action a = callBack;
    a.BeginInvoke(s, ar => { }, null);
    ...
    private void callBack(NamedPipeServerStream pipe)
    {
      while (true)
      {
        pipe.WaitForConnection();
        StreamReader sr = new StreamReader(pipe);
        Console.WriteLine(sr.ReadToEnd());
        pipe.Disconnect();
      }
    }
    

    And the client does this:

    using (var pipe = new NamedPipeClientStream(".", "p", PipeDirection.Out))
    using (var stream = new StreamWriter(pipe))
    {
      pipe.Connect();
      stream.Write("Hello");
    }
    

    I can repeat above block multiple times with the server running, no prob.

提交回复
热议问题