当前位置:网站首页>C development - interprocess communication - named pipeline

C development - interprocess communication - named pipeline

2022-07-07 22:23:00 Hey, hey, hey, hey, hey

Simple named pipes Demo

client : Input 1+1 Click Send , take 1+1 To the server

Server side : After the operation, the result is returned to the client

Here is the core code , complete Demo Click here

client:

private void sendMsgToServer()
        {
            string _name = "sendMsgToServer";
            using (var client = new NamedPipeClientStream(".", m_sPipeName, PipeDirection.InOut))
            {
                try
                {
                    client.Connect(2000);
                    Helper.LogHelper.Log_Info(m_sClassName + _name, " Successfully connected to the server , Pipe name :" + m_sPipeName);
                    using (var pipe = new Helper.PipeStream(client))
                    {
                        Helper.LogHelper.Log_Info(m_sClassName + _name, " Start sending and receiving messages circularly , Pipe name :" + m_sPipeName);
                        while (!m_bClosePipe)
                        {
                            if (m_bSend)
                            {
                                m_bSend = !m_bSend;
                                m_bReceive = !m_bReceive;
                                this.Dispatcher.Invoke(delegate ()
                                {
                                    Helper.MsgModel msg = GetMsg();
                                    if (msg != null)
                                    {
                                        string sendMsg = JsonConvert.SerializeObject(msg);
                                        pipe.Send(sendMsg);
                                        Helper.LogHelper.Log_Info(m_sClassName + _name, " Send a message to the server , Pipe name :" + m_sPipeName);
                                    }
                                });
                            }
                            if (m_bReceive)
                            {
                                m_bReceive = !m_bReceive;
                                string receive = pipe.Receive();
                                if (!string.IsNullOrEmpty(receive))
                                {
                                    MessageBox.Show(receive, " Received a message from the server ");
                                    Helper.LogHelper.Log_Info(m_sClassName + _name, " Received a message from the server , Pipe name :" + m_sPipeName);
                                }
                            }
                        }
                        string closeMsg = JsonConvert.SerializeObject(null);
                        pipe.Send(closeMsg);
                        Helper.LogHelper.Log_Info(m_sClassName + _name, " Send a close pipeline message to the server , Pipe name :" + m_sPipeName);
                    }
                    client.Close();
                    Helper.LogHelper.Log_Info(m_sClassName + _name, " Client pipeline closed , Pipe name :" + m_sPipeName);
                    MessageBox.Show(" The client's current named pipe is closed ", " client ");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString(), " client ");
                    Helper.LogHelper.Log_Error(m_sClassName + _name, " error :" + ex.Message.ToString());
                }
            }
        }

server:

private void listenClient()
        {
            while (!m_bClose)
            {
                using (m_PipeServer = new NamedPipeServerStream(m_sPipeName, PipeDirection.InOut, m_iMaxNumber))
                {
                    try
                    {
                        m_PipeServer.WaitForConnection();
                        using (m_pipeStream = new Helper.PipeStream(m_PipeServer))
                        {
                            while (true)
                            {
                                string msgJson = m_pipeStream.Receive();
                                if (msgJson != "null")
                                {
                                    Helper.MsgModel msg = JsonConvert.DeserializeObject<Helper.MsgModel>(msgJson);
                                    double number1 = Convert.ToDouble(msg.number1);
                                    double number2 = Convert.ToDouble(msg.number2);
                                    switch (msg.type)
                                    {
                                        case "+":
                                            DoMain_A(number1, number2);
                                            break;
                                        case "-":
                                            DoMain_B(number1, number2);
                                            break;
                                        case "*":
                                            DoMain_C(number1, number2);
                                            break;
                                        case "/":
                                            DoMain_D(number1, number2);
                                            break;
                                    }
                                    m_PipeServer.RunAsClient(SendToClent);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        m_PipeServer.Close();
                        MessageBox.Show(" The current named pipeline of the server is closed "," The server ");
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString(), " The server ");
                    }
                }
            }
        }

原网站

版权声明
本文为[Hey, hey, hey, hey, hey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130606206556.html