开发者

How to use named pipes over network?

开发者 https://www.devze.com 2023-03-08 09:27 出处:网络
I\'m trying to create a connection over network via named pipes. I\'m doing it as it says inmsdn. I create pipes server side with function.

I'm trying to create a connection over network via named pipes. I'm doing it as it says in msdn. I create pipes server side with function.

CreateNamedPipe(
                     "\\\\.\\pipe\\myNamedPipe",
           开发者_如何学C          DUPLEX | FILE_FLAG_OVERLAPPED,
                     0,
                     255,
                     BUFFER_SIZE,
                     BUFFER_SIZE,
                     0,
                     IntPtr.Zero);

and trying to connect via CreateFile() function

CreateFile(
                  "\\\\10.0.0.29\\pipe\\myNamedPipe",
                  GENERIC_READ | GENERIC_WRITE,
                  0,
                  IntPtr.Zero,
                  OPEN_EXISTING,
                  FILE_FLAG_OVERLAPPED,
                  IntPtr.Zero);

10.0.0.29 is server machines ip. If I'm trying to run client side program on server machine with pipe name "\\.\pipe\myNamedPipe" or "\\10.0.0.29\pipe\myNamedPipe" (10.0.0.29 is servers ip) or "\\localhost\pipe\myNamedPipe" it works fine.

So how to use named pipes over network?


Starting with version 3.5, named pipes are supported natively in the .NET Framework, you don't have to use tedious interop p/invoke code. See this introduction article here: .NET 3.5 Adds Named Pipes Support for a sample.

Using this constructor overload, NamedPipeClientStream Constructor (String, String), you can pass a server name argument.


I think the question is answered above, but keep in mind there is a big problem using named pipes that are opened to a remote machine. When you call the windows API function WaitNamedPipe with a timeout greater than 0, the calling thread will use a whole CPU until the named pipe either connects or times out. With a timeout of 0, it doesn't have enough time to make a remote connection, so you are basically forced to use all of a CPU each time you try to connect, and all you can do is time out, and try again later.

I encountered this problem and found I wasn't alone: http://social.msdn.microsoft.com/Forums/sv-SE/netfxnetcom/thread/7bbf5a0b-3c22-4836-b271-999e514c321b

Until Microsoft provides a more performance friendly way of connecting to remote named pipes, I'm going to stop using them entirely, and I suggest you do the same. If for any reason you're unable to connect almost immediately, you end up with a livelock.


Pipe Server must have a name you need to specify a name for the server and not IP address.

See this tutorial.

However named pipes are convenient for local connections, because on the Network you get the overhead of TCP encapsulating making using named pipes inconvenient.

Using named pipes for local connection improve speed but over the network doesn't have much sense ... Use Socket ...

0

精彩评论

暂无评论...
验证码 换一张
取 消