Export (0) Print
Expand All

AnonymousPipeServerStream Class

Exposes a stream around an anonymous pipe, which supports both synchronous and asynchronous read and write operations.

Namespace:  System.IO.Pipes
Assembly:  System.Core (in System.Core.dll)

[HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
public sealed class AnonymousPipeServerStream : PipeStream

NoteNote:

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Anonymous pipes help provide safe and secure interprocess communication between child and parent processes. The AnonymousPipeServerStream class enables a parent process to send or receive information from a child process.

Anonymous pipes are unnamed, one-way pipes that typically transfer data between parent and child processes. Anonymous pipes are always local; they cannot be used over a network. A PipeDirection value of InOut is not supported because anonymous pipes are defined to be one-way.

Anonymous pipes do not support Message read modes.

The client side of an anonymous pipe must be created from a pipe handle provided by the server side by calling the GetClientHandleAsString method. The string is then passed as a parameter when creating the client process. From the client process, it is then passed to the AnonymousPipeClientStream constructor as the pipeHandleAsString parameter.

The AnonymousPipeServerStream object must dispose the client handle using the DisposeLocalCopyOfClientHandle method in order to be notified when the client exits.

Note   For Windows XP Professional and Windows 2000 Server, the maximum number of pipes that are permitted to simultaneously connect over the network is ten.

The following example demonstrates a way to send a string from a parent process to a child process using anonymous pipes. This example creates an AnonymousPipeServerStream object in a parent process with a PipeDirection value of Out. It also creates an AnonymousPipeClientStream object in a child process with a PipeDirection value of In. The parent process then sends a user-supplied string to the child process. The string is displayed to the console.

This example is for the server process, which uses the AnonymousPipeServerStream class. For the entire code example, including the code for both the pipe client and server, see How to: Use Anonymous Pipes to Communicate Between Local Processes.

using System;
using System.IO;
using System.IO.Pipes;
using System.Diagnostics;

class PipeServer
{
    static void Main()
    {
        Process pipeClient = new Process();

        pipeClient.StartInfo.FileName = "pipeClient.exe";

        using (AnonymousPipeServerStream pipeServer =
            new AnonymousPipeServerStream(PipeDirection.Out,
            HandleInheritability.Inheritable))
        {
            // Show that anonymous pipes do not support Message mode. 
            try
            {
                Console.WriteLine("[SERVER] Setting ReadMode to \"Message\".");
                pipeServer.ReadMode = PipeTransmissionMode.Message;
            }
            catch (NotSupportedException e)
            {
                Console.WriteLine("[SERVER] Exception:\n    {0}", e.Message);
            }

            Console.WriteLine("[SERVER] Current TransmissionMode: {0}.",
                pipeServer.TransmissionMode);

            // Pass the client process a handle to the server.
            pipeClient.StartInfo.Arguments =
                pipeServer.GetClientHandleAsString();
            pipeClient.StartInfo.UseShellExecute = false;
            pipeClient.Start();

            pipeServer.DisposeLocalCopyOfClientHandle();

            try
            {
                // Read user input and send that to the client process. 
                using (StreamWriter sw = new StreamWriter(pipeServer))
                {
                    sw.AutoFlush = true;
                    // Send a 'sync message' and wait for client to receive it.
                    sw.WriteLine("SYNC");
                    pipeServer.WaitForPipeDrain();
                    // Send the console input to the client process.
                    Console.Write("[SERVER] Enter text: ");
                    sw.WriteLine(Console.ReadLine());
                }
            }
            // Catch the IOException that is raised if the pipe is broken 
            // or disconnected. 
            catch (IOException e)
            {
                Console.WriteLine("[SERVER] Error: {0}", e.Message);
            }
        }

        pipeClient.WaitForExit();
        pipeClient.Close();
        Console.WriteLine("[SERVER] Client quit. Server terminating.");
    }
}

System.Object
  System.MarshalByRefObject
    System.IO.Stream
      System.IO.Pipes.PipeStream
        System.IO.Pipes.AnonymousPipeServerStream

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

Community Additions

ADD
Show:
© 2014 Microsoft