Named pipes are information transfers from one process to another using kernel objects. Unlike a typical pipeline, named pipes can be called by different processes in different ways (across permissions, across languages, across platforms). As long as the program knows the name of the named pipe, the information sent to the named pipe can be read by all the programs that have the specified authorization, but not with the authorization. A named pipe is a FIFO (first in, First-in, first-out) object.
We can use Named pipes to communicate in 2 different processes without the need to obtain information through a generic IO read-write file.
In C # It's easy to use this to receive messages
usingSystem.IO.Pipes;Private volatileNamedpipeserverstream _outputnamedpipe;
_outputnamedpipe = new Namedpipeserverstream (Pipename, pipedirection.inout, 254);Private voidBeginreceiveoutput () {Try{_outputnamedpipe.waitforconnection (); using(varReader =NewStreamReader (_outputnamedpipe)) {_outputmessage=Reader. ReadLine (); } } Catch { } }
In VBScript, you can send messages like this.
Dim FS, Pipe Set CreateObject ("scripting.filesystemobject") Set pipe = fs. OpenTextFile ("\\.\pipe\pipename"8False0) Pipe. WriteLine ("This wasa message from VBS") pipe. Close
A more rigorous notation
1 classServer2 {3 Static voidMain (string[] args)4 {5 varrunning =true;6 7 while(running)//loop only for 1 client8 {9 using(varServer =NewNamedpipeserverstream ("Pipe_name", pipedirection.inout))Ten { One server. Waitforconnection (); A -Console.WriteLine ("Client Connected"); - the using(varPipe =Newpipestream (server)) - { -Pipe. Send ("Handshake"); - + Console.WriteLine (pipe. Receive ()); - + server. Waitforpipedrain (); A server. Flush (); at } - } - } - -Console.WriteLine ("Server closed"); - Console.read (); in } - } to + classClient - { the Static voidMain (string[] args) * { $ using(varClient =NewNamedpipeclientstream (".","Pipe_name", pipedirection.inout))Panax Notoginseng { -Client. Connect ( -); the + using(varPipe =Newpipestream (client)) A { theConsole.WriteLine ("Message:"+Pipe. Receive ()); + -Pipe. Send ("Hello!!!"); $ } $ } - - Console.read (); the } - }Wuyi the Public classpipestream:idisposable - { Wu Private ReadOnlyStream _stream; - Private ReadOnlyReader _reader; About Private ReadOnlyWriter _writer; $ - PublicPipestream (Stream stream) - { -_stream =stream; A +_reader =NewReader (_stream); the_writer =NewWriter (_stream); - } $ the Public stringReceive () the { the return_reader. ReadLine (); the } - in Public voidSend (stringmessage) the { the _writer. WriteLine (message); About _writer. Flush (); the } the the Public voidDispose () + { - _reader. Dispose (); the _writer. Dispose ();Bayi the _stream. Dispose (); the } - - Private classReader:streamreader the { the PublicReader (Stream stream) the:Base(Stream) the { - the } the the protected Override voidDispose (BOOLdisposing)94 { the Base. Dispose (false); the } the }98 About Private classWriter:streamwriter - {101 PublicWriter (Stream stream)102:Base(Stream)103 {104 the }106 107 protected Override voidDispose (BOOLdisposing)108 {109 Base. Dispose (false); the }111 } the}
Named Pipes for C # (named pipe)