C# 命名管道通訊
先記錄一下程式碼:
1. 建立NamedPipeServer類,作為服務端的使用,裡面包含建立例項,收發的方法:
public class NamedPipeServer { public NamedPipeServerStream pipeServer; //StreamReader sr; //StreamWriter sw; /// <summary> /// 建立 /// </summary> /// <param name="pipename"></param>View Code/// <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) { } } }
可以直接用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