当前位置:网站首页>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
边栏推荐
- Talk about how the kotlin process started?
- Rew acoustic test (III): generate test signal
- ES6 learning path (II) let & const
- [cmake] make command cannot be executed normally
- Duplicate entry '2' for key 'primary appears in JPA‘
- Use of Baidu face recognition API
- Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)
- Dart basic notes
- Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)
- Opencv learning notes -day10 logical operation of image pixels (usage of rectangle function and rect function and bit related operation in openCV)
猜你喜欢

Deeply understand the working principle of kotlin collaboration suspend (beginners can also understand it)

Flink SQL custom connector

Express の Hello World

Challenge transform() 2D

ES6 learning path (II) let & const

Opencv learning notes -day 11 (split() channel separation function and merge() channel merge function)

Abstract factory pattern

Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)

Applet learning path 2 - event binding

Express file download
随机推荐
ES6 learning path (IV) operator extension
Talking about kotlin process exception handling mechanism
Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function
Treatment process record of Union Medical College Hospital (Dongdan hospital area)
Opencv learning notes -day2 (implemented by the color space conversion function cvtcolar(), and imwrite image saving function imwrite())
快应用中实现自定义抽屉组件
Express file upload
Express file download
12. problem set: process, thread and JNI architecture
JVM tuning related commands and explanations
Small program learning path 1 - getting to know small programs
Application of hongruan face recognition
5. Messager framework and imessager interface
Do you want the dialog box that pops up from the click?
Interpretation of source code demand:a rotation equivariant detector for aerial object detection
4. use ibinder interface flexibly for short-range communication
[JPEG] how to compile JPEG turbo library files on different platforms
Talk about writing
启动jar包报错UnsupportedClassVersionError,如何修复
Flink Exception -- No ExecutorFactory found to execute the application