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)