IPWorks IPC - How to create and use named pipes

How to use IPWorks IPC to create and use named pipes.

Date Entered: 08/06/2015    Last Updated: 08/14/2015

IPWorks IPC provides a means of inter-process communications (IPC) through named pipes. This article will provide details about setting up a PipeServer, connecting to a PipeServer with a PipeClient, and using the PipeExec component to interact with a process.

Components

PipeServer

To begin using the PipeServer to listen for incoming connections, only a couple of steps are required:

  • Specify the desired name of the pipe in the PipeName property.
  • Set Listening to true
When a connection has successfully been established, the Connected event will fire with the ConnectionId used to refer to that connection. The ConnectionId will also be stored in the Connections property. In order to broadcast a message to all active connections, iterate through the Connections collection and set DataToSend for each connection. Incoming data will be provided through the DataIn event. If DefaultEOL is not set, the DataIn event will be fired as data is received, without waiting for EOL. The Disconnected event will fire with the ConnectionId for every client that disconnects. To close all active connections and stop accepting new connections, call the Shutdown method.

Pipeserver pipeserver; private void StartServer() { //Create a PipeServer and bind the events. pipeserver = new Pipeserver(); pipeserver.OnDataIn += pipeserver_OnDataIn; pipeserver.OnConnected += pipeserver_OnConnected; pipeserver.OnDisconnected += pipeserver_OnDisconnected; //Set the PipeName, DefaultEOL, and start the server. pipeserver.PipeName = "MyPipeServer"; pipeserver.DefaultEOL = "\r\n"; //optional pipeserver.Listening = true; } private void BroadcastMessage() { //To broadcast a message to all connected clients, iterate through the //Connections collection and set DataToSend for each connection. foreach (PipeConnection connection in pipeserver.Connections.Values) { connection.DataToSend = "Broadcast Data\r\n"; } } private void StopServer() { //Terminate existing connections, and stop accepting new connections. pipeserver.Shutdown(); } //Data is received through the DataIn event. private void pipeserver_OnDataIn(object sender, PipeserverDataInEventArgs e) { tbLog.AppendText("Echo '" + e.Text + "' from " + e.ConnectionId + ".\r\n"); } //The Connected event is fired for every connection that is established. private void pipeserver_OnConnected(object sender, PipeserverConnectedEventArgs e) { Console.WriteLine("Client " + e.ConnectionId + " connected."); } //The Disconnected event is fired for every connection that is terminated. private void pipeserver_OnDisconnected(object sender, PipeserverDisconnectedEventArgs e) { Console.WriteLine("Client " + e.ConnectionId + " disconnected."); }

PipeClient

Using the PipeClient component to connect to a PipeServer requires two basic steps:

  • Specify the name of the pipe to connect to in the PipeName property.
  • Call the Connect method.
If the connection was successfully established, the Connected event will fire. To send data to the server, set the DataToSend property, or use the Send method. Incoming data is provided through the DataIn event. If EOL is not set, the DataIn event will be fired as data is received, without waiting for EOL. To disconnect, simply call the Disconnect method, or set the Connected property to false.

Pipeclient pipeclient; private void ConnectClient() { //Create the PipeClient and bind the events. pipeclient = new Pipeclient(); pipeclient.OnDataIn += pipeclient_OnDataIn; pipeclient.OnConnected += pipeclient_OnConnected; pipeclient.OnDisconnected += pipeclient_OnDisconnected;. //Set the PipeName, EOL, and call Connect. pipeclient.PipeName = "MyPipeServer"; pipeclient.EOL = "\r\n"; //optional pipeclient.Connect(); } private void SendMessage() { //To send a message to the server, simply set DataToSend. pipeclient.DataToSend = "Hello World!\r\n"; } private void DisconnectClient() { //To disconnect from the server, simply call the Disconnect method. pipeclient.Disconnect(); } //Data is received through the DataIn event. private void pipeclient_OnDataIn(object sender, PipeclientDataInEventArgs e) { Console.WriteLine("Echo '" + e.Text + "'."); } //The Connected event is fired when a connection is established. private void pipeserver_OnConnected(object sender, PipeserverConnectedEventArgs e) { Console.WriteLine("Connected."); } //The Disconnected event is fired when the connection is terminated. private void pipeserver_OnDisconnected(object sender, PipeserverDisconnectedEventArgs e) { Console.WriteLine("Disconnected."); }

PipeExec

In order to use the PipeExec component to launch a process, the following two steps are required:

  • Set the ProcessFileName property to the path of the process on disk.
  • Call the StartProcess method.
The ProcessArgs property may be set to pass arguments to the process when it is started. The ProcessWorkingDir property can be set to specify the working directory in which the process will run. To send data to the process, set the Stdin property or call the Send method. Received data will be provided through the Stdout and Stderr events. To terminate the process, call the StopProcess method.

Pipexec pipeexec; private void Start() { //Create an instance of PipExec and bind the events. pipeexec = new Pipeexec(); pipeexec.OnStdout += pipeexec_OnStdout; pipeexec.OnStderr += pipeexec_OnStderr; //Pick a ProcessFileName, specify the ProcessArgs, and call StartProcess. pipeexec.ProcessFileName = @"C:\Windows\system32\cmd.exe"; pipeexec.ProcessArgs = "/Q"; pipeexec.StartProcess(); } private void SendData() { //To send data to the process, simply set the Stdin property. pipeexec.Stdin = "dir" + System.Environment.NewLine; } //Typical output data received from the process will be provided through the Stdout event. private void pipeexec_OnStdout(object sender, IPWorksIPC.PipeexecStdoutEventArgs e) { Console.WriteLine(e.Text); } //Error output received from the process will be provided through the Stderr event. private void pipeexec_OnStderr(object sender, IPWorksIPC.PipeexecStderrEventArgs e) { Console.WriteLine(e.Text); }


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