C #: understanding asynchronous NamedPipeServerStream

I tried to find a good and clear example of an asynchronous NamedPipeServerStream and could not find a suitable one for me.

I want to have a NamedPipe server that asynchronously receives messages from clients. The client is simple and suitable for me. But I cannot find server examples or cannot understand how this works.

Now, as I understand it, I need to create a NamedPipeServerStream object. Let me do this:

namedPipeServerStream = new NamedPipeServerStream(PIPENAME, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, BUFFERSIZE, BUFFERSIZE); 

It seems to need work. But I don’t know, do I need to use PipeSecurity or PipeAccessRule at all? I AM? My server will run as a Windows service on the local system.

What's next? I think I need to use BeginWaitForConnection for asynchronous connections. Let's get a look:

 namedPipeServerStream.BeginWaitForConnection(WaitForConnectionAsyncCallback, <some strange thing>); 

Question 1 : What is this “weird thing”? How to use it?

Question 2 : should I do

 while(true) { namedPipeServerStream.BeginWaitForConnection(WaitForConnectionAsyncCallback, <some strange thing>); } 

So that my server always waits for connections? Or do I need to do it somehow else?

And then ... Let me take a look at the WaitForConnectionAsyncCallback function:

 private void WaitForConnectionAsyncCallback(IAsyncResult result) { Console.WriteLine("Client connected."); byte[] buff = new byte[BUFFERSIZE]; namedPipeServerStream.Read(buff, 0, namedPipeServerStream.InBufferSize); string recStr = General.Iso88591Encoding.GetString(buff, 0, namedPipeServerStream.InBufferSize); Console.WriteLine(" " + recStr); namedPipeServerStream.EndWaitForConnection(result); } 

.. This, of course, does not work. Since , I do not know how to get the string from the stream exactly. How? Now it throws an InvalidOperationException:

The pipe is not yet connected.

So how to organize asynchronous work with NamedPipeServerStream?

+4
source share
1 answer
  • You are trying to use PipeSecurity to restrict access to the channel, allowing only blissful programs to connect to your service. Put it on the back-burner until you get this work, and have performed a security analysis that shows that this restriction is justified.

  • “Some strange thing” is just an arbitrary object that you can pass to the callback method. You don’t often need this, but it can be useful if you write a callback so that it serves several connections. In this case, you need to know more about the specific connected channel, the status argument allows you to pass this information. In your callback method, the result.AsyncState property returns a reference to this object. Only worry about it later, you can use it when you need it. Just go null for now.

  • This is mistake. You must first call EndWaitForConnection () before doing anything else with the pipe. Just move it to the top of the method. Usually you want to write it inside try / catch so that you can catch an ObjectDisposedException, an exception that will occur when you close the channel before exiting your program.

+7
source

All Articles