Named Pipes for C # (named pipe)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.