Two way C++ to C# communication using named pipes

后端 未结 2 931
耶瑟儿~
耶瑟儿~ 2020-12-19 05:03

I am trying to have a 2-way communication between a VC++ 6 app and a C# app. I am using named pipes. In my C++ code I can read the message from the C# client but then the se

2条回答
  •  一向
    一向 (楼主)
    2020-12-19 05:13

    Disposing of a StreamWriter or StreamReader will close the underlying stream.

    Your using statements therefore will be causing the stream to close.

        public void ThreadStartClient(object obj)
        {
                // Ensure that we only start the client after the server has created the pipe
                ManualResetEvent SyncClientServer = (ManualResetEvent)obj;
    
                // Only continue after the server was created -- otherwise we just fail badly
                // SyncClientServer.WaitOne();
    
                using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", "SAPipe"))
                {
                    // The connect function will indefinately wait for the pipe to become available
                    // If that is not acceptable specify a maximum waiting time (in ms)
                    pipeStream.Connect();
    
    
                    //Write from client to server
                    StreamWriter sw = new StreamWriter(pipeStream))
                    sw.WriteLine("What's your status?");
    
                    //Read server reply
                    StreamReader sr = new StreamReader(pipeStream)
                    string temp = "";
                    temp = sr.ReadLine();   //Pipe is already closed here ... why?
    
                    MessageBox.Show(temp);
                }
        }
    

    It should also be noted that because you wrap your stream in a using statement, the commented out pipeStream.Close() function isn't needed.

提交回复
热议问题