C#: Asynchronous NamedPipeServerStream understanding

后端 未结 1 1745
忘掉有多难
忘掉有多难 2021-01-19 03:46

I was trying to find any good and clear example of asynchronous NamedPipeServerStream and couldn\'t find any suitable for me.

I want to have NamedPipe Server which i

1条回答
  •  余生分开走
    2021-01-19 04:26

    • You tinker with the PipeSecurity to restrict access to the pipe, allowing only blessed programs to connect to your service. Put that on the back-burner until you've got this working and have performed a security analysis that shows that this kind of restriction is warranted.

    • The "some strange thing" is simply an arbitrary object that you can pass to the callback method. You don't often need it but it can be helpful if you write your callback so it serves multiple connections. In which case you need to know more about the specific pipe that got connected, the state argument allows you to pass that info. In your callback method, the result.AsyncState property gives you the reference back to that object. Only worry about that later, you'll find a use for it when you need it. Just pass null until then.

    • That's a bug. You must call EndWaitForConnection() first, before doing anything else with the pipe. Simply move it to the top of the method. You typically want to write it inside a try/catch so you can catch ObjectDisposedException, the exception that will be raised when you close the pipe before exiting your program.

    0 讨论(0)
提交回复
热议问题