Getting Started with IPWorks IPC

Requirements: IPWorks IPC

Introduction

IPWorks IPC is a suite of components for inter-process communications (IPC) through Named Pipes. The component suite includes client, server, and remote execution components enabling straightforward peer-to-peer communication between related or unrelated processes.

This article goes through the basic functionality of each component included in the toolkit. Before continuing, it is recommended to download IPWorks IPC in order to follow along with this tutorial.

Contents

  1. Setting up a Pipe Server
  2. Connecting to the Pipe Server
  3. PipeExec

Setting up a Pipe Server

The PipeServer component can be used to create and listen on a new named pipe. PipeServer is the server complement of PipeClient (which is used to create client applications). They share a common design philosophy and interface.

PipeServer can start to listen on a pipe by setting the PipeName property and then setting Listening to true:

// Create a new named pipe pipeserver1.PipeName = "MyPipeServer"; pipeserver1.Listening = true;

When a client connects the Connected event fires, a ConnectionId is assigned, and communication can start. This Id is unique to each connection. PipeServer's events also have ConnectionId as a parameter to identify the connection to which they are related. Data is received via the DataIn event, and data can be sent using either the Send method or the DataToSend property.

// DataIn event handler delegate pipeserver1.OnDataIn += (o, e) => { Console.WriteLine("DataIn: [" + e.ConnectionId + "] - " + e.Text); }; // Send data to a connected client pipeserver1.Send(connectionId, "Data to send to client."); // or pipeserver1.DataToSend[connectionId] = "Data to send to client.";

Creating a PipeClient application.

The PipeClient component is a simple client for connecting and communicating over named pipes. To begin, first set PipeName to the name of the pipe to which the connection will be made and then call the Connect method to establish the connection. The Connected event will fire when the connection is complete.

// Connect to the existing pipe server pipeclient1.PipeName = "MyPipeServer"; pipeclient1.Connect();

After connecting set DataToSend, or call Send or SendFile to send data. Incoming data is received through the DataIn event. To disconnect call Disconnect or set Connected to False.

PipeExec

The PipeExec component provides an easy way to launch and communicate with a process over Stdin, Stdout, and Stderr.

To begin, set the ProcessFileName property to the path of the process on disk. Optionally set ProcessArgs and ProcessWorkingDir and call the StartProcess method to start the process.

// An event handler for the Stdout event static void exec_OnStdout(object sender, PipeexecStdoutEventArgs e) { Console.WriteLine(e.Text); } // Create an instance of the PipeExec component and start the cmd.exe process Pipeexec exec = new Pipeexec(); exec.OnStdout += exec_OnStdout; exec.ProcessFileName = @"C:\Windows\system32\cmd.exe"; exec.ProcessArgs = "/Q"; exec.StartProcess();

Next, set Stdin or call the Send method to send data to the process. Received data will be provided through the Stdout and Stderr events.

// Send the dir command to the process exec.Stdin = "dir" + System.Environment.NewLine; // or exec.Send("dir" + System.Environment.NewLine); while (true) // Wait for output exec.DoEvents();

To terminate the process call StopProcess.

We appreciate your feedback.  If you have any questions, comments, or suggestions about this article please contact our support team at kb@nsoftware.com.