IPWorks IPC - How to create and use named pipes
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
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.
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.
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 article please contact our support team at support@nsoftware.com.