当前位置:网站首页>Pipe pipe --namedpipe and anonymouspipe
Pipe pipe --namedpipe and anonymouspipe
2022-06-30 09:24:00 【LongtengGensSupreme】
Pipes are divided into named pipes and anonymous pipes
1. Introduction to named pipes
" name pipes " or " Name the pipeline "(Named Pipes) It's a simple interprocess communication (I P C) Mechanism ,Microsoft Windows NT,Windows 2000,Windows 95 as well as Windows 98 Both provide support for it ( But does not include Windows CE). Named pipes can be between different processes on the same computer , Or between different processes on different computers across a network , Support reliable , One way or two-way data communication . Designing applications with named pipes is actually quite simple , There is no need to master the basic network transmission protocol in advance ( Such as T C P / I P or I P X) Knowledge . This is because the named pipeline takes advantage of the Microsoft Network Provider (M S N P) Redirector , Through a network , Establish communication between processes . thus , Applications don't have to care about the details of network protocols . The reason why we use named pipes as our own network communication scheme , One important reason is that they make full use of Windows NT And Windows 2000 Built in security features .
2. Named pipe function
Here is an example of an acceptable command pipeline . Suppose we want to develop a data management system , Only one specified user group is allowed to operate . Imagine being in your office , There is a computer , It contains the company's secrets . We require only the management of the company , To access and deal with these secrets . Suppose you are on your workstation machine , Every employee in the company can see this computer on the network . However , We do not expect ordinary employees to gain access to confidential materials . under these circumstances , Named pipes work well , Because we can develop a server application , Make it subject to requests from the client , Operate the company's secrets safely . The server can restrict customer access to administrators , use Windows NT Or a new version Windows 2000 Built in security mechanism , This can be done very easily . One important thing to remember here is , When using named pipes as a network programming solution , It actually builds a simple client / Server data communication system , In which data can be reliably transmitted .
3. Named pipe benefits
Easy to use , And you don't need to declare a port number , There is no need to care about permissions in the program .
4. Named pipe restrictions ( I personally think )
The pipeline can only communicate one-to-one .
5. The implementation of named pipeline communication and anonymous pipeline are as follows :
#region Anonymouspipe
static string txID = null;
static string rxID = null;
public static void AnonymousPipeServer()
{
string clientExe = @"F:\Person\Longteng\LongtengSln\ConsoleAppTestAnonymousPipe\bin\Debug\ConsoleAppTestAnonymousPipe.exe";
HandleInheritability inherit = HandleInheritability.Inheritable;
using (var tx = new AnonymousPipeServerStream(PipeDirection.Out, inherit))
using (var rx = new AnonymousPipeServerStream(PipeDirection.In, inherit))
{
txID = tx.GetClientHandleAsString();
rxID = rx.GetClientHandleAsString();
var startInfo = new ProcessStartInfo(clientExe, txID + " " + rxID);
startInfo.UseShellExecute = false; // Required for child process
Process p = Process.Start(startInfo);
tx.DisposeLocalCopyOfClientHandle(); // Release unmanaged
rx.DisposeLocalCopyOfClientHandle(); // handle resources.
//tx.WriteByte(100);
//Console.WriteLine("Server received: " + rx.ReadByte());
//p.WaitForExit();
while (true)
{
tx.WriteByte(100);
Console.WriteLine("Server received: " + rx.ReadByte());
}
}
}
//ClientDemo.exe The content of
public static void AnonymousPipeClient()
{
//Thread.Sleep(TimeSpan.FromSeconds(1));
//string rxID = args[0]; // Note we're reversing the
//string txID = args[1]; // receive and transmit roles.
using (var rx = new AnonymousPipeClientStream(PipeDirection.In, rxID))
using (var tx = new AnonymousPipeClientStream(PipeDirection.Out, txID))
{
//Console.WriteLine("Client received: " + rx.ReadByte());
//tx.WriteByte(200);
while (true)
{
Console.WriteLine("Client received: " + rx.ReadByte());
tx.WriteByte(200);
}
}
}
//public static void AnonymousPipeServer1()
//{
// string clientExe = @"d:\PipeDemo\ClientDemo.exe";
// HandleInheritability inherit = HandleInheritability.Inheritable;
// using (var tx = new AnonymousPipeServerStream(PipeDirection.Out, inherit))
// using (var rx = new AnonymousPipeServerStream(PipeDirection.In, inherit))
// {
// string txID = tx.GetClientHandleAsString();
// string rxID = rx.GetClientHandleAsString();
// var startInfo = new ProcessStartInfo(clientExe, txID + " " + rxID);
// startInfo.UseShellExecute = false; // Required for child process
// Process p = Process.Start(startInfo);
// tx.DisposeLocalCopyOfClientHandle(); // Release unmanaged
// rx.DisposeLocalCopyOfClientHandle(); // handle resources.
// tx.WriteByte(100);
// Console.WriteLine("Server received: " + rx.ReadByte());
// p.WaitForExit();
// }
//}
ClientDemo.exe The content of
//public static void AnonymousPipeClient1()
//{
// string rxID = args[0]; // Note we're reversing the
// string txID = args[1]; // receive and transmit roles.
// using (var rx = new AnonymousPipeClientStream(PipeDirection.In, rxID))
// using (var tx = new AnonymousPipeClientStream(PipeDirection.Out, txID))
// {
// Console.WriteLine("Client received: " + rx.ReadByte());
// tx.WriteByte(200);
// }
//}
#endregion
#region Namepipe
public static void PipeServer()
{
var s = new System.IO.Pipes.NamedPipeServerStream("pipedream");
s.WaitForConnection();
while (true)
{
s.WriteByte(100);
Console.WriteLine($"PipeServer received client data :{s.ReadByte()}");
}
}
public static void PipeClient()
{
var s = new System.IO.Pipes.NamedPipeClientStream("pipedream");
s.Connect();
while (true)
{
Console.WriteLine($"PipeClient Receive server data :{s.ReadByte()}");
Thread.Sleep(TimeSpan.FromSeconds(2));
s.WriteByte(200); // Send the value 200 back.
}
}
public static void PipeServerMessage()
{
var s = new System.IO.Pipes.NamedPipeServerStream("pipedream", PipeDirection.InOut, 1, PipeTransmissionMode.Message);
s.WaitForConnection();
while (true)
{
byte[] msg = Encoding.UTF8.GetBytes("Hello");
s.Write(msg, 0, msg.Length);
Console.WriteLine($"PipeServer Server side data :{Encoding.UTF8.GetString(ReadMessage(s))}");
}
}
public static void PipeClientMessage()
{
//var s = new NamedPipeClientStream("pipedream");
var s = new NamedPipeClientStream("192.168.1.199", "pipedream");
s.Connect();
s.ReadMode = PipeTransmissionMode.Message;
while (true)
{
Console.WriteLine($"PipeClient Receive the data :{Encoding.UTF8.GetString(ReadMessage(s))}");
Thread.Sleep(TimeSpan.FromSeconds(2));
byte[] msg = Encoding.UTF8.GetBytes("Hello right back!1111");
s.Write(msg, 0, msg.Length);
Console.WriteLine($"PipeClient send data :Hello right back!111");
//Thread.Sleep(TimeSpan.FromSeconds(2));
//s.WriteByte(200); // Send the value 200 back.
}
}
static byte[] ReadMessage(PipeStream s)
{
MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[0x1000]; // Read in 4 KB blocks
do
{
ms.Write(buffer, 0, s.Read(buffer, 0, buffer.Length));
}
while (!s.IsMessageComplete);
return ms.ToArray();
}
#endregion
Reference link :https://www.cnblogs.com/shanranlei/p/3629901.html
边栏推荐
- Deep Learning with Pytorch- A 60 Minute Blitz
- Differences between the notify(), notifyall(), notifydatasetchanged(), notifydatasetinvalidated() methods in the adapter
- Flink SQL custom connector
- Sort (simple description)
- List set export excel table
- 9.JNI_ Necessary optimization design
- Raspberry pie 4B no screen installation system and networking using VNC wireless projection function
- asdsadadsad
- Treatment process record of Union Medical College Hospital (Dongdan hospital area)
- Dart asynchronous task
猜你喜欢
Talk about the job experience of kotlin cooperation process
4. use ibinder interface flexibly for short-range communication
Express get request
Introduction to the runner of mmcv
Metasploit practice - SSH brute force cracking process
7. know JNI and NDK
Talk about the kotlin cooperation process and the difference between job and supervisorjob
Baidu map JS browsing terminal
Express file download
Bottomsheetbehavior principle of realizing the home page effect of Gaode map
随机推荐
Pytorch BERT
Esp32 things (x): other functions
Script summary
Using appbarlayout to realize secondary ceiling function
Use Huawei performance management service to configure the sampling rate on demand
Couldn't load this key (openssh ssh-2 private key (old PEM format))
Metasploit practice - SSH brute force cracking process
Maxiouassigner of mmdet line by line interpretation
[protobuf] protobuf generates cc/h file through proto file
Icon resources
127.0.0.1, 0.0.0.0 and localhost
Esp32 things (3): overview of the overall system design
快应用中实现自定义抽屉组件
Installation, use and explanation of vulnerability scanning tool OpenVAS
So the toolbar can still be used like this? The toolbar uses the most complete parsing. Netizen: finally, you don't have to always customize the title bar!
Get to know handler again
Concatapter tutorial
Rew acoustic test (VI): signal and measurement
Dart basic notes
Deeply understand the working principle of kotlin collaboration suspend (beginners can also understand it)