C # pipeline communication-named pipeline (2)

Source: Internet
Author: User

Summary of connections to named MPs Queues:

The connection to the named MPs queue is blocked twice. One is that when waitforconnection () is used, [the same reason for client connection] is blocked, and the other is when readstring () [read MPs queue information] is used.

After creating a named pipe for the first time on the server end, wait for the connection. After the client connection is successful, the named pipe on the server end is used for communication purposes. If you need to wait for the connection again, the server needs to open the named pipe (create an instance named pipe) again and wait for the connection.

Each time the client opens a named pipe, it establishes a connection with the server, and then it can use the Named Pipe for communication. If you need to establish a second connection, you need to open the pipe again and establish a connection again.

When creating a named MPs queue, you must specify a host name and MPs queue name. The MPs queue name is unique on the same host. Once a named MPs queue is created, MPs Queues with the same name cannot be created again.

Note that closing a media transcoding queue and closing a connection are different. You can establish connections on the same media transcoding queue and reduce the system load. If the maximum number of pipelines is specified, the new pipeline cannot be opened if the old one is not closed after the maximum number of opened pipelines is reached. The client cannot close the connection, but can only directly call close () to close the pipeline. If the client calls close () to close the MPs queue, you cannot connect to the original MPs queue on the server again. The MPs queue on the server also needs to be reinitialized and wait for the connection.

About bidirectional communication:

As mentioned in the previous article, two-way communication can be performed between pipelines. Two-way communication is actually a process in which the server simulates the client's sending and receiving, and the client primarily calls pipeserver. runasclient (startlistening), startlistening is a callback method, which can write back messages. The client can perform read. The specific code is:

Main client code:

NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", _pipeName, PipeDirection.InOut, PipeOptions.None, TokenImpersonationLevel.Impersonation);pipeClient.Connect();while (true){//Writestring sRequest = "request";StreamString ss = new StreamString(pipeClient);ss.WriteString(sRequest);//Readstring sResult = ss.ReadString();if (sResult != ""){//}}

Main server code:

NamedPipeServerStream pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.InOut, 1);pipeServer.WaitForConnection();try{_ss = new StreamString(pipeServer);string sResult = _ss.ReadString();if ( sResult != ""){pipeServer.RunAsClient(StartListening);}}catch (IOException ex){}private void StartListening(){//Return result messagestring sResult = "request";_ss.WriteString(sResult);}

Because my program contains some complicated logic processing, I only paste some of the main code, and do not directly run it on behalf of me. I hope Boyou can practice it after understanding the meaning.

Related Article

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.