https://github.com/gustavohennig/pipepoc
Examples o IPC using named pipes with .Net
https://github.com/gustavohennig/pipepoc
csharp named-pipes pipe
Last synced: 7 months ago
JSON representation
Examples o IPC using named pipes with .Net
- Host: GitHub
- URL: https://github.com/gustavohennig/pipepoc
- Owner: GustavoHennig
- License: mit
- Created: 2018-10-22T12:28:23.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-22T16:41:30.000Z (almost 7 years ago)
- Last Synced: 2025-01-18T03:24:35.898Z (9 months ago)
- Topics: csharp, named-pipes, pipe
- Language: C#
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### 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
```c#
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();
```
https://github.com/GustavoHennig/PipePoc/blob/9dcb2e19fb2737d4ecadbab46a0e544dae3ed69d/PipePocBinClient/Program.cs#L23-L58#### The Server
```c#
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();
```https://github.com/GustavoHennig/PipePoc/blob/9dcb2e19fb2737d4ecadbab46a0e544dae3ed69d/PipePocBinServer/Program.cs#L31-L62