Named Pipes cross-process communication instance 2

Source: Internet
Author: User

Original: http://www.cnblogs.com/jonneydong/archive/2012/03/02/2418743.html

Service-Side code:

//Create a service-side, pipeline instanceNamedpipeserverstream Serverstream =NewNamedpipeserverstream ("Test2pipe", Pipedirection.inout,1, Pipetransmissionmode.message, pipeoptions.asynchronous);Private voidForm1_Load (Objectsender, EventArgs e) {    //enable thread to receive messagesThreadPool.QueueUserWorkItem (Delegate{serverstream.beginwaitforconnection (o)={namedpipeserverstream server=(Namedpipeserverstream) o.asyncstate; Server.            Endwaitforconnection (o); StreamReader SR=NewStreamReader (server); StreamWriter SW=NewStreamWriter (server); stringresult =NULL; stringClientName =server.            Getimpersonationusername ();  while(true) {result=Sr.                ReadLine (); if(Result = =NULL|| result = ="Bye")                     Break;  This. Invoke ((MethodInvoker)Delegate{listBox1.Items.Add (clientName+":"+result);            });    }}, Serverstream); });}

Client code:

        //Create a Client connection instanceNamedpipeclientstream Clientstream =NewNamedpipeclientstream ("127.0.0.1",            "Test2pipe", Pipedirection.inout, pipeoptions.asynchronous, System.Security.Principal.TokenImpersonationLev El.        None); StreamWriter SW=NULL; Private voidForm1_Load (Objectsender, EventArgs e) {            //Connecting PipesClientstream.connect (); SW=NewStreamWriter (Clientstream); Sw. AutoFlush=true; }        Private voidButton1_Click (Objectsender, EventArgs e) {            //writing content to a pipelineSW.        WriteLine (richTextBox1.Text); }

Show Results:

By discovery, named Pipes are actually connected based on TCP/IP. And the port is 445

Of course, I'm just transmitting a string as information only. You can still transfer your own defined objects and so on. (Remember to serialize yo ...)

Named Pipes cross-process Communication Instance 2 (RPM)

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.