Code Monkey home page Code Monkey logo

pipepoc's Introduction

3 Examples of IPC using named pipes

PipePocMsExample

This project is an example using the official Microsoft documentation.

https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-use-named-pipes-for-network-interprocess-communication

PipePocBinClient (Client and Server)

It uses PipeTransmissionMode.Message, communication occurs by buffered read/write (bytes);

PipePocStreamReader (Client and Server)

This project is a copy of PipePocBin but it uses StreamReader and StreamWriter helpers.

My findings...

This was the example that best fit the needs of my project, and was quite simple:

The Client

  NamedPipeClientStream pipeClient = 
                        new NamedPipeClientStream(".", "testpipe",
                            PipeDirection.InOut, PipeOptions.None,
                            TokenImpersonationLevel.Impersonation);

      pipeClient.Connect();
      pipeClient.ReadMode = PipeTransmissionMode.Message;

      while (pipeClient.IsConnected)
      {
          Console.Write("Ready: ");
          string readline = Console.ReadLine();

          byte[] resp = UnicodeEncoding.Default.GetBytes(readline);

          pipeClient.Write(resp, 0, resp.Length);

          StringBuilder sb = new StringBuilder();

          do
          {
              var buffer = new byte[10];
              var readBytes = pipeClient.Read(buffer, 0, buffer.Length);
              sb.Append(UnicodeEncoding.Default.GetString(buffer));
          }
          while (!pipeClient.IsMessageComplete);

          Console.WriteLine(sb.ToString().TrimEnd('\0'));
      }
  pipeClient.Close();

NamedPipeClientStream pipeClient =
new NamedPipeClientStream(".", "testpipe",
PipeDirection.InOut, PipeOptions.None,
TokenImpersonationLevel.Impersonation);
pipeClient.Connect();
pipeClient.ReadMode = PipeTransmissionMode.Message;
while (pipeClient.IsConnected)
{
Console.Write("Ready: ");
string readline = Console.ReadLine();
byte[] resp = UnicodeEncoding.Default.GetBytes(readline);
pipeClient.Write(resp, 0, resp.Length);
StringBuilder sb = new StringBuilder();
do
{
var buffer = new byte[10];
var readBytes = pipeClient.Read(buffer, 0, buffer.Length);
sb.Append(UnicodeEncoding.Default.GetString(buffer));
}
while (!pipeClient.IsMessageComplete);
Console.WriteLine(sb.ToString().TrimEnd('\0'));
}
pipeClient.Close();

The Server

  NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message);

  int threadId = Thread.CurrentThread.ManagedThreadId;

  // Wait for a client to connect
  pipeServer.WaitForConnection();

  while (pipeServer.IsConnected)
  {
      StringBuilder sb = new StringBuilder();

      do
      {
          var buffer = new byte[10];

          AutoResetEvent manualResetEvent = new AutoResetEvent(false);

          pipeServer.BeginRead(buffer, 0, buffer.Length, (st) => {
              pipeServer.EndRead(st);
              manualResetEvent.Set();
          }, null);
          manualResetEvent.WaitOne();

          sb.Append(UnicodeEncoding.Default.GetString(buffer));
      }
      while (!pipeServer.IsMessageComplete);

      Console.WriteLine(sb.ToString());

      Console.Write("Ready: ");
      string readline;
      do
      {
          readline = Console.ReadLine();
      } while (string.IsNullOrEmpty(readline));

      byte[] resp = UnicodeEncoding.Default.GetBytes(readline);

      pipeServer.Write(resp, 0, resp.Length);
  }
  pipeServer.Close();

StringBuilder sb = new StringBuilder();
do
{
var buffer = new byte[10];
AutoResetEvent manualResetEvent = new AutoResetEvent(false);
pipeServer.BeginRead(buffer, 0, buffer.Length, (st) => {
pipeServer.EndRead(st);
manualResetEvent.Set();
}, null);
manualResetEvent.WaitOne();
sb.Append(UnicodeEncoding.Default.GetString(buffer));
}
while (!pipeServer.IsMessageComplete);
Console.WriteLine(sb.ToString());
Console.Write("Ready: ");
string readline;
do
{
readline = Console.ReadLine();
} while (string.IsNullOrEmpty(readline));
byte[] resp = UnicodeEncoding.Default.GetBytes(readline);
pipeServer.Write(resp, 0, resp.Length);
}
pipeServer.Close();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.