当前位置:网站首页>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();
}
#endregionReference link :https://www.cnblogs.com/shanranlei/p/3629901.html
边栏推荐
- 2020-11-02
- Talk about the job experience of kotlin cooperation process
- C#訪問SQL Server數據庫兩種方式的比較(SqlDataReader vs SqlDataAdapter)
- Interviewer: do you understand the principle of recyclerview layout animation?
- Pit encountered by fastjason
- Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)
- Is it safe to open an account? How can anyone say that it is not reliable.
- Deep Learning with Pytorch- A 60 Minute Blitz
- Application of hongruan face recognition
- 5. Messager framework and imessager interface
猜你喜欢

Interpretation of orientedrcnn papers

Opencv learning notes -day4 image pixel reading and writing operations (array traversal and pointer traversal implementation, uchar vec3b data type and mat class functions mat:: at(), mat:: ptr())

5. Messager framework and imessager interface

C accesses mongodb and performs CRUD operations

What kind of experience is it to develop a "grandson" who will call himself "Grandpa"?

12. problem set: process, thread and JNI architecture

Small program learning path 1 - getting to know small programs

Talk about how the kotlin process started?

将线程绑定在某个具体的CPU逻辑内核上运行

I'm late for school
随机推荐
Detailed explanation of pipline of mmdetection
Research on lg1403 divisor
127.0.0.1, 0.0.0.0 and localhost
5. Messager framework and imessager interface
使用华为性能管理服务,按需配置采样率
Deep understanding of kotlin collaboration context coroutinecontext
Wechat development tool (applet)
[paid promotion] collection of frequently asked questions, FAQ of recommended list
桂林 稳健医疗收购桂林乳胶100%股权 填补乳胶产品线空白
Code management related issues
Talk about the job experience of kotlin cooperation process
C#访问MongoDB并执行CRUD操作
Bottomsheetbehavior principle of realizing the home page effect of Gaode map
Resnet50+fpn for mmdet line by line code interpretation
Deep Learning with Pytorch-Train A Classifier
Opencv learning notes -day 12 (ROI region extraction and inrange() function operation)
asdsadadsad
Raspberry pie 4B no screen installation system and networking using VNC wireless projection function
Mmdet line by line code interpretation of positive and negative sample sampler
关于Lombok的@Data注解