<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      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

       

      posted @ 2022-03-10 19:01  尋找迷途的羔羊  閱讀(1219)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 丝袜高潮流白浆潮喷在线播放| 九九综合va免费看| 亚洲成在人线AV品善网好看| 动漫AV纯肉无码AV电影网| 欧美性XXXX极品HD欧美风情| 国产精品毛片在线完整版| 中文字幕亚洲国产精品| 国产又黄又硬又粗| 国产精品日韩中文字幕| 亚洲国产大片永久免费看| 男女扒开双腿猛进入爽爽免费看 | 日本三级理论久久人妻电影| 韩国精品一区二区三区在线观看 | 色综合久久中文字幕综合网 | 蜜臀午夜一区二区在线播放| 国产精品日韩精品日韩| 日韩无专区精品中文字幕| 欧美视频免费一区二区三区| 亚洲精品一区二区口爆| 日韩大片看一区二区三区| 亚洲中文字幕人妻系列| 久热这里只有精品12| 国产SM重味一区二区三区| 综合色一色综合久久网| 少妇人妻精品无码专区视频 | 国产欧美日韩视频怡春院| 国产精品亚洲一区二区在| 亚洲国产成人无码AV在线影院L| 国产亚洲精品AA片在线播放天| 国产麻豆精品av在线观看| 汾西县| 免费人成在线视频无码| 成人啪啪高潮不断观看| 午夜福利yw在线观看2020| 色成人亚洲| 国产亚洲精品岁国产精品| 免费又大粗又爽又黄少妇毛片| 亚洲永久精品日韩成人av| 久久久精品人妻一区二区三区| 人妻无码ΑV中文字幕久久琪琪布 国产乱人伦AV在线麻豆A | 18禁在线永久免费观看|