C# 命名管道通信


先記錄一下代碼:

 

1. 創建NamedPipeServer類,作為服務端的使用,里面包含創建實例,收發的方法:

public class NamedPipeServer
    {
        public NamedPipeServerStream pipeServer;
        //StreamReader sr;
        //StreamWriter sw;

        /// <summary>
        /// 創建
        /// </summary>
        /// <param name="pipename"></param>
        /// <returns></returns>
        public NamedPipeServerStream CreatPipeServer(string pipename)
        {
            pipeServer = new NamedPipeServerStream(pipename, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.None);
            //sr = new StreamReader(pipeServer);
            //sw = new StreamWriter(pipeServer);
            return pipeServer;
        }


        /// <summary>
        /// 接收
        /// </summary>
        /// <returns></returns>
        public string Receive()
        {
            string str="";
            try
            {
                str = pipeServer.ReadByte().ToString();//讀取單字節
                
                //str = sr.ReadLine();                 //讀取字符串
                //byte[] buffff = new byte[8];
                //pipeServer.Read(buffff, 0, 8);       //讀取指定長度的字節數據
                //str = buffff[0].ToString ();

            }
            catch (Exception ex)
            {

            }
            return str;
        }


        /// <summary>
        /// 發送
        /// </summary>
        /// <param name="command"></param>
        public void Send(byte command)
        {
            try
            {
                pipeServer.WriteByte(command);   //發送單字節
                pipeServer.Flush();

                //sw.WriteLine(command);              //發送字符串
                //sw.Flush();

                //byte[] buffff = new byte[2];
                //buffff[0] = command;
                //pipeServer.Write(buffff, 0, 2);   //發送指定長度的字節數據
                Console.WriteLine("Send: " + command);
            }
            catch (Exception ex)
            {

            }
        }

    }
View Code

可以直接用NamedPipeServerStream的實例化的類pipeServer進行收發:

(1) 收發單字節:pipeServer.WriteByte();     pipeServer.ReadByte();

(2) 收發字節數組:byte[] buff = new byte[8];

                               buff [0] = 0x01;

                              pipeServer.Write(buff, 0, 8);  //讀取指定起點與長度的字節數組       pipeServer.Read(buff, 0, 8);       //讀取指定長度的字節數據數組

(3) 收發字符串:這里要用到 StreamWriter 和StreamRead,      sr = new StreamReader(pipeServer);   sw = new StreamWriter(pipeServer);

                            sw.WriteLine();       sr.ReadLine(); 

(4) 收發字符:     sw.Write();              sr.Read();

 

2. 創建NamedPipeClient類,作為客戶端的使用,里面包含創建實例,收發的方法:

public class NamedPipeClient
    {
        public NamedPipeClientStream pipeClient;
        //StreamReader sr;
        //StreamWriter sw;

        /// <summary>
        /// 創建
        /// </summary>
        /// <param name="pipename"></param>
        /// <returns></returns>
        public NamedPipeClientStream CreatPipeClient(string pipename)
        {
            pipeClient = new NamedPipeClientStream(".", pipename);
            //sr = new StreamReader(pipeClient);
            //sw = new StreamWriter(pipeClient);
            return pipeClient;
        }


        /// <summary>
        /// 接收
        /// </summary>
        /// <returns></returns>
        public string Receive()
        {
            string str = "";
            try
            {
                str = pipeClient.ReadByte().ToString();

                //str = sr.ReadLine();

                //byte[] buffff = new byte[8];
                //pipeClient.Read(buffff, 0, 8);
                //str = buffff[0].ToString();
            }
            catch (Exception ex)
            {

            }
            return str;
        }


        /// <summary>
        /// 發送
        /// </summary>
        /// <param name="command"></param>
        public void Send(byte command)
        {
            try
            {
                pipeClient.WriteByte(command);
                pipeClient.Flush();

                //sw.WriteLine(command);
                //sw.Flush();

                //byte[] buffff = new byte[2];
                //buffff[0] = command;
                //pipeClient.Write(buffff, 0, 2);
            }
            catch (Exception ex)
            {

            }
        }
    }
View Code

接收的方式與服務端類似,對應着改改就行 

 

3.總結:

總結幾個使用過程中遇到的問題:

(1) 實例化 NamedPipeServerStream 時,PipeTransmissionMode只是數據流傳輸的方式,發送的是字符串還是字節,由收發字節的方式決定。

(2) 字符串發送時,需要用到Flush();將數據寫入基礎流,才能把字符串傳輸出去。

(3) 和C語言或者C++寫成的命名管道通訊時,注意限制字符串的長度,否則會收到一大串字符串亂碼,掩蓋了真正需要收到的字符串。另外C語言需要在字符串的末尾添加"\n".

(4) 命名管道屬於半雙工通信,接收數據的同時不能發送數據。

(5) Read()或者ReadLine() 會讓進程一直卡在這個地方,一直等到另一端發送一個數據過來,可以選擇使用asnyc/await避免卡頓問題,

     另外,如果在收到數據之前,想發送一個數據,發送也會卡在Flush();的這個位置,目前,暫未有好的解決辦法,希望有大神可以賜教。

 

4. 附上一段操作代碼

public class OperateProcess
    {
        NamedPipeServer namedPipeServer = new NamedPipeServer();
        Support support = Support.GetInstance();
        ComLog log = new ComLog();

        /// <summary>
        /// 創建流程
        /// </summary>
        /// <returns></returns>
        public bool CreateProcess()
        {
            try
            {
                namedPipeServer.pipeServer = namedPipeServer.CreatPipeServer("testpipe");
                namedPipeServer.pipeServer.WaitForConnection();
            }
            catch (Exception ex)
            {
                support.FunShow(ex);
                return false;
            }

            return true;
        }


        /// <summary>
        /// 結束流程
        /// </summary>
        public bool EndProcess()
        {
            try
            {
                if(!Handshake())
                {
                    return false;
                }

                Thread.Sleep(100);
                SendCommand(0x03);
                if (!ReceiveCommand(30, 0xFE))
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                support.FunShow(ex);
                return false;
            }


        }



        /// <summary>
        /// 開始流程
        /// </summary>
        public bool StartProcess()
        {
            try
            {
                if (!Handshake())
                {
                    return false;
                }

                Thread.Sleep(100);
                SendCommand(0x01);
                if (!ReceiveCommand(10, 0xFE))
                {
                    return false;
                }
                Thread.Sleep(100);
                if (!ReceiveCommand(10, 0x02))
                {
                    return false;
                }
                SendCommand(0xFF);
                return true;
            }
            catch (Exception ex)
            {
                support.FunShow(ex);
                return false;
            }
            
        }


        /// <summary>
        /// 握手
        /// </summary>
        /// <returns></returns>
        private bool Handshake()
        {
            try
            {
                SendCommand(0x00);
                if (!ReceiveCommand(30, 0xFE))
                {
                    return false;
                }
                SendCommand(0xFF);
                return true;
            }
            catch (Exception ex)
            {
                support.FunShow(ex);
                return false;
            }
        }


        /// <summary>
        /// 發送命令並等待
        /// </summary>
        /// <param name="com"></param>
        public bool SendCommand(byte com, int time)
        {
            namedPipeServer.Send(com);
            log.SendReceiveLog("發送:" + com.ToString(), 0);
            if (!ReceiveCommand(time, 0xFE))
            {
                return false;
            }
            return true;
        }


        /// <summary>
        /// 發送命令
        /// </summary>
        /// <param name="com"></param>
        public void SendCommand(byte com)
        {
            namedPipeServer.Send(com);
            log.SendReceiveLog("發送:" + com.ToString(), 0);
        }


        /// <summary>
        /// 循環發送
        /// </summary>
        /// <returns></returns>
        public bool CycleSend(byte com, int time)
        {
            int count = 0;
            while (!SendCommand(com,time))
            {
                if (count > 5)
                {
                    return false;
                }
                count++;
            }
            return true;
        }


        /// <summary>
        /// 接收命令
        /// </summary>
        public string ReceiveCommand()
        {
            try
            {
                string str= namedPipeServer.Receive();
                log.SendReceiveLog("接收:" + str, 1);
                if (!string.IsNullOrEmpty(str))
                {
                    return str;
                }
            }
            catch (Exception ex)
            {
                support.FunShow(ex);
            }
            return "";
        }

        /// <summary>
        /// 接收指定的命令
        /// </summary>
        /// <param name="time"></param>
        /// <param name="re"></param>
        /// <returns></returns>
        public bool ReceiveCommand(int time, byte re)
        {
            try
            {
                string str;
                int i = 0;
                while (i < time)
                {
                    str = namedPipeServer.Receive();
                    log.SendReceiveLog("接收:" + str, 1);
                    if (!string.IsNullOrEmpty(str))
                    {
                        if (str == Convert.ToInt32(re).ToString())
                        {
                            return true;
                        }
                    }
                    i++;
                    Thread.Sleep(1000);
                }
            }
            catch (Exception ex)
            {
                support.FunShow(ex);
            }
            return false;
        }


        /// <summary>
        /// 保存日志
        /// </summary>
        public void SaveLog()
        {
            log.SaveLog();
        }
    }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM