Top: Streams: namedpipe

>     #include <pstreams.h>

>     class namedpipe: instm, outstm {
>         namedpipe( [ string pipename ] );
>         string get/set_pipename(string);
>     }

The namedpipe class implements full-duplex streaming communication between processes, typically on one computer. It is built on top of Windows named pipes and local UNIX sockets. Namedpipe incorporates iobase, instm and outstm interfaces and adds one more property: the pipe name to connect to. PTypes named pipes are session-oriented.

Naming. Local sockets on UNIX can be created anywhere in the file system and generally behave like ordinary files, except that the input and output streams for a socket file is handled by the server process that created the socket. In contrast to UNIX, Windows allocates named pipes in a special directory (actually a share name) \\.\pipe, which is not visible to the end-user.

PTypes can accept both a file name and an absolute path name when creating a named pipe object, however, absolute paths have effect only on UNIX. If you specify a single file name on UNIX, PTypes will place the socket file in /tmp. That is, if you are writing a portable application, and you want the socket file to be placed in a directory other than /tmp on UNIX (e.g. some daemons running as root place their sockets in /var/run), you can specify the full path in both Windows and UNIX versions of your program: the library will ignore the path on Windows, since it can not place it other than in \\.\pipe, and will use it on UNIX.

Usage. Named pipes provide efficient means of interprocess communication. Many networking applications, typically SQL servers, offer both network sockets and local sockets as alternate ways of connecting to the service. For security reasons, a system administrator may choose to configure the service such that it will use only the local socket, or the service can be open to the network too if it provides strong authentication at application level.

Namedpipe and ipstream classes are compatible: both are full-duplex streams and are derived from instm and outstm simultaneously. Thus, PTypes allows you to easily combine both methods of communication in one application. Each thread serving connections on server-side of your application can accept two parameters of basic types instm and outsm; you can then pass either an ipstream or a namedpipe object (or any other compatible stream object) to the newly instantiated thread to serve the request coming from the client. Note that it is sometimes necessary to provide explicit typecast when assigning namedpipe or ipstream to outstm or instm.

Windows security note. Named pipes on Windows are open to the network, i.e. any computer can connect to a pipe through \\computer-name\pipe\..., where computer-name can be a NetBIOS name or even an IP address. Even though PTypes' interfaces do not allow you to connect to a remote named pipe for the sake of compatibility with UNIX, still, you should consider a situation when someone knowing the pipe name and the protocol you use, writes his own program to 'illegally' access your service on a Windows machine from a remote computer. Hence, for better security, your service should provide user authentication at application level (of course, unless it's a public service and is open anyway). Aside from security, network named pipes are much slower than any other networking protocol, such like TCP/IP, so we do not encourage using named pipes remotely in any case.

Unlike Windows, UNIX local sockets can never be accessed from a remote computer even through a NFS-mounted directory. Note that remote access to named pipes on Windows can be disabled by stopping all Windows networking services and leaving only the transport-level protocol stacks.

Local unnamed pipes for exchanging data within one process can be created using infile::pipe().

Interface. Namedpipe is compatible with iobase, instm and outstm and in addition, defines the following:

namedpipe::namedpipe( [ string pipename ] ) creates a named pipe object and optionally assigns the pipe name. When creating a namedpipe object that will be passed to npserver::serve(), it is not necessary to assign a pipe name.

string namedpipe::get/set_pipename(string) gets or sets the pipe name. When assigning a new pipe name, set_pipename() first closes the stream.

See also: npserver, iobase, instm, outstm, Networking examples

PTypes home