System. Io series: multiple threads in the LAN use named pipes to communicate instances between processes

Source: Internet
Author: User

For the basic usage of pipelines, see system. Io. Use pipelines for inter-process communication (system. Io. Pipes ).

This section describes how to use named pipes. In this example, several clients obtain the newly generated int type ID through a server.

Server Function: when the client requests a new ID, it increases the existing ID by 1 and returns it to the client.

Server implementation: when the program starts, it starts n threads, declares a namedpipeserverstream instance in each thread, and cyclically waitforconnection (), write the new ID to the named pipe, and then disconnect. Release the namedpipeserverstream instance when the program exits.

The following code is used:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. io; using system. io. pipes; using system. threading; namespace idserver {class program {// <summary> // name of the named pipeline /// </Summary> private const string pipe_name = "testnetworkpipe "; // defines the number of threads, which is also the maximum number of instances allowed by namedpipeserverstream. Private const int max_threads_count = 3; Private Static volatile int _ runingthreadcount = 0; Private Static volatile int _ newid = 0; // instance array Private Static namedpipeserverstream [] _ serverstreams; static void main (string [] ARGs) {_ serverstreams = new namedpipeserverstream [max_threads_count]; // release all namedpipeserverstream instances appdomain when the process exits. currentdomain. processexit ++ = new eventhandler (currentdomain_processexit); // start the thread startservers (); console. read () ;}/// <summary> /// release the named pipe when the process exits /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> static void currentdomain_processexit (Object sender, eventargs e) {If (_ serverstreams! = NULL) {foreach (namedpipeserverstream item in _ serverstreams) {item. dispose () ;}}/// <summary> // start the server thread // </Summary> Private Static void startservers () {for (INT I = 0; I <max_threads_count; I ++) {thread = new thread (New threadstart (startnewidserver); thread. start () ;}/// <summary> // start a namedpipeserverstream instance // </Summary> Private Static void startnewidserver () {namedpipeserverstream = NULL; console. writeline ("start server in thread" + thread. currentthread. managedthreadid); stream = _ serverstreams [_ runingthreadcount] = new namedpipeserverstream (pipe_name, pipedirection. inout, max_threads_count, pipetransmissionmode. message, pipeoptions. none); int threadno = _ runingthreadcount; _ runingthreadcount + = 1; while (true) {stream. waitforconnection (); int newid = ++ _ newid; byte [] bytes = bitconverter. getbytes (newid); stream. write (bytes, 0, bytes. length); stream. flush (); console. write ("threadno:" + thread. currentthread. managedthreadid + "\ r"); stream. disconnect ();}}}}

The client function is to continuously send requests for new IDs, print new IDs, and configure the Server IP address on the client.

The following code:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading; namespace idclient {class program {private const string pipe_name = "testnetworkpipe"; static void main (string [] ARGs) {console. writeline ("enter any character and press enter to Start Program Execution .. "); console. read (); do {// intranet Server IP address, which must be LAN string servername = "127.0.0.1"; // declare namedpipeclientstream instance using (VAR clientstream = new syste M. io. pipes. namedpipeclientstream (servername, pipe_name) {// connect to the clientstream server. connect (1000); // set it to clientstream in message read mode. readmode = system. io. pipes. pipetransmissionmode. message; do {byte [] bytes = new byte [4]; clientstream. read (bytes, 0, 4); int val = bitconverter. toint32 (bytes, 0); console. write ("newid =" + Val + "\ r");} while (! Clientstream. ismessagecomplete);} thread. Sleep (1) ;}while (true );}}}

In SQL Server, a named pipe is used for Process Communication in the LAN.

When declaring a namedpipeserverstream instance, you can specify the number of its instances. If the number of instances exceeds this value, an IO exception "All pipeline samples are in use" will be thrown.

This example cannot be used in actual projects.

Download program source code

Related essays:
. Net: Windows File Operations in system. Io

. Net series: stream of system. Io

System. Io memory ing file shared memory

System. Io uses pipelines for inter-process communication (using system. Io. Pipes)

System. Io series: multiple threads in the LAN use named pipes to communicate instances between processes

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.