NamedPipeServerStream 类

定义

公开命名管道周围的 Stream,该管道既支持同步读写操作,也支持异步读写操作。Exposes a Stream around a named pipe, supporting both synchronous and asynchronous read and write operations.

public ref class NamedPipeServerStream sealed : System::IO::Pipes::PipeStream
public sealed class NamedPipeServerStream : System.IO.Pipes.PipeStream
type NamedPipeServerStream = class
    inherit PipeStream
Public NotInheritable Class NamedPipeServerStream
Inherits PipeStream
继承
NamedPipeServerStream

示例

下面的示例演示了使用命名管道将字符串从父进程发送到同一计算机上的子进程的方法。The following example demonstrates a way to send a string from a parent process to a child process on the same computer using named pipes. 此示例在具有 PipeDirectionOut的父进程中创建一个 NamedPipeServerStream 对象。This example creates a NamedPipeServerStream object in a parent process with a PipeDirection value of Out. 服务器随后将等待子进程中的 NamedPipeClientStream 对象连接到该对象。The server then waits for a NamedPipeClientStream object in a child process to connect to it. 在此示例中,两个进程都在同一台计算机上,并且 NamedPipeClientStream 对象的 PipeDirection 值为 InIn this example, both processes are on the same computer and the NamedPipeClientStream object has 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.

此示例适用于使用 NamedPipeServerStream 类的服务器进程。This example is for the server process, which uses the NamedPipeServerStream class. 有关完整的代码示例(包括管道客户端和服务器的代码),请参阅如何:使用命名管道进行网络进程间通信For the entire code example, including the code for both the pipe client and server, see How to: Use Named Pipes for Network Interprocess Communication.

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

class PipeServer
{
    static void Main()
    {
        using (NamedPipeServerStream pipeServer =
            new NamedPipeServerStream("testpipe", PipeDirection.Out))
        {
            Console.WriteLine("NamedPipeServerStream object created.");

            // Wait for a client to connect
            Console.Write("Waiting for client connection...");
            pipeServer.WaitForConnection();

            Console.WriteLine("Client connected.");
            try
            {
                // Read user input and send that to the client process.
                using (StreamWriter sw = new StreamWriter(pipeServer))
                {
                    sw.AutoFlush = true;
                    Console.Write("Enter text: ");
                    sw.WriteLine(Console.ReadLine());
                }
            }
            // Catch the IOException that is raised if the pipe is broken
            // or disconnected.
            catch (IOException e)
            {
                Console.WriteLine("ERROR: {0}", e.Message);
            }
        }
    }
}
Imports System.IO
Imports System.IO.Pipes

Class PipeServer

    Shared Sub Main()
        Dim pipeServer As New NamedPipeServerStream("testpipe", PipeDirection.Out)

        Console.WriteLine("NamedPipeServerStream object created.")

        ' Wait for a client to connect
        Console.Write("Waiting for a client connection...")
        pipeServer.WaitForConnection()

        Console.WriteLine("Client connected.")
        Try
            'Read user input and send that to the client process.
            Dim sw As New StreamWriter(pipeServer)
            sw.AutoFlush = True
            Console.Write("Enter Text: ")
            sw.WriteLine(Console.ReadLine())
        Catch ex As IOException
            ' Catch the IOException that is raised if the pipe is broken
            ' or disconnected
            Console.WriteLine("ERROR: {0}", ex.Message)
        End Try
    End Sub
End Class

注解

Named pipes 提供单向或双工管道,用于在管道服务器与一个或多个管道客户端之间进行通信。Named pipes provide one-way or duplex pipes for communication between a pipe server and one or more pipe clients. 命名管道可用于在本地或通过网络进行进程间通信。Named pipes can be used for interprocess communication locally or over a network. 单个管道名称可由多个 NamedPipeClientStream 对象共享。A single pipe name can be shared by multiple NamedPipeClientStream objects.

任何进程都可以充当命名管道服务器或客户端,也可以同时充当这两者。Any process can act as either a named pipe server or client, or both.

构造函数

NamedPipeServerStream(PipeDirection, Boolean, Boolean, SafePipeHandle)

从指定的管道句柄初始化 NamedPipeServerStream 类的一个新实例。Initializes a new instance of the NamedPipeServerStream class from the specified pipe handle.

NamedPipeServerStream(String)

用指定的管道名称初始化 NamedPipeServerStream 类的新实例。Initializes a new instance of the NamedPipeServerStream class with the specified pipe name.

NamedPipeServerStream(String, PipeDirection)

使用指定的管道名称和管道方向初始化 NamedPipeServerStream 类的新实例。Initializes a new instance of the NamedPipeServerStream class with the specified pipe name and pipe direction.

NamedPipeServerStream(String, PipeDirection, Int32)

使用指定的管道名称、管道方向和最大数量的服务器实例初始化 NamedPipeServerStream 类的一个新实例。Initializes a new instance of the NamedPipeServerStream class with the specified pipe name, pipe direction, and maximum number of server instances.

NamedPipeServerStream(String, PipeDirection, Int32, PipeTransmissionMode)

使用指定的管道名称、管道方向、最大数量的服务器实例和传输模式初始化 NamedPipeServerStream 类的新实例。Initializes a new instance of the NamedPipeServerStream class with the specified pipe name, pipe direction, maximum number of server instances, and transmission mode.

NamedPipeServerStream(String, PipeDirection, Int32, PipeTransmissionMode, PipeOptions)

使用指定的管道名称、管道方向、最大数量的服务器实例、传输模式和管道选项初始化 NamedPipeServerStream 类的新实例。Initializes a new instance of the NamedPipeServerStream class with the specified pipe name, pipe direction, maximum number of server instances, transmission mode, and pipe options.

NamedPipeServerStream(String, PipeDirection, Int32, PipeTransmissionMode, PipeOptions, Int32, Int32)

使用指定的管道名称、管道方向、最大数量的服务器实例、传输模式、管道选项和建议的输入输出缓冲区大小初始化 NamedPipeServerStream 类的新实例。Initializes a new instance of the NamedPipeServerStream class with the specified pipe name, pipe direction, maximum number of server instances, transmission mode, pipe options, and recommended in and out buffer sizes.

NamedPipeServerStream(String, PipeDirection, Int32, PipeTransmissionMode, PipeOptions, Int32, Int32, PipeSecurity)

使用指定的管道名称、管道方向、最大数量的服务器实例、传输模式、管道选项、建议的输入输出缓冲区大小和管道安全性初始化 NamedPipeServerStream 类的新实例。Initializes a new instance of the NamedPipeServerStream class with the specified pipe name, pipe direction, maximum number of server instances, transmission mode, pipe options, recommended in and out buffer sizes, and pipe security.

NamedPipeServerStream(String, PipeDirection, Int32, PipeTransmissionMode, PipeOptions, Int32, Int32, PipeSecurity, HandleInheritability)

使用指定的管道名称、管道方向、最大数量的服务器实例、传输模式、管道选项、建议的输入输出缓冲区大小、管道安全性和继承模式初始化 NamedPipeServerStream 类的新实例。Initializes a new instance of the NamedPipeServerStream class with the specified pipe name, pipe direction, maximum number of server instances, transmission mode, pipe options, recommended in and out buffer sizes, pipe security, and inheritability mode.

NamedPipeServerStream(String, PipeDirection, Int32, PipeTransmissionMode, PipeOptions, Int32, Int32, PipeSecurity, HandleInheritability, PipeAccessRights)

使用指定的管道名称、管道方向、最大数量的服务器实例、传输模式、管道选项、建议的输入输出缓冲区大小、管道安全性、继承模式和管道访问权限初始化 NamedPipeServerStream 类的新实例。Initializes a new instance of the NamedPipeServerStream class with the specified pipe name, pipe direction, maximum number of server instances, transmission mode, pipe options, recommended in and out buffer sizes, pipe security, inheritability mode, and pipe access rights.

字段

MaxAllowedServerInstances

表示系统资源允许的服务器实例的最大数量。Represents the maximum number of server instances that the system resources allow.

属性

CanRead

获取一个值,该值指示当前流是否支持读操作。Gets a value indicating whether the current stream supports read operations.

(继承自 PipeStream)
CanSeek

获取一个值,该值指示当前流是否支持查找操作。Gets a value indicating whether the current stream supports seek operations.

(继承自 PipeStream)
CanTimeout

获取一个值,该值确定当前流是否可以超时。Gets a value that determines whether the current stream can time out.

(继承自 Stream)
CanWrite

获取一个值,该值指示当前流是否支持写操作。Gets a value indicating whether the current stream supports write operations.

(继承自 PipeStream)
InBufferSize

获取管道的入站缓冲区的大小(以字节为单位)。Gets the size, in bytes, of the inbound buffer for a pipe.

(继承自 PipeStream)
IsAsync

获取一个值,该值指示 PipeStream 对象是异步打开还是同步打开。Gets a value indicating whether a PipeStream object was opened asynchronously or synchronously.

(继承自 PipeStream)
IsConnected

获取或设置一个值,该值指示是否已连接 PipeStream 对象。Gets or sets a value indicating whether a PipeStream object is connected.

(继承自 PipeStream)
IsHandleExposed

获取一个值,该值指示是否公开了 PipeStream 对象的句柄。Gets a value indicating whether a handle to a PipeStream object is exposed.

(继承自 PipeStream)
IsMessageComplete

获取一个值,该值指示最近的读操作返回的消息中是否有更多数据。Gets a value indicating whether there is more data in the message returned from the most recent read operation.

(继承自 PipeStream)
Length

获取流长度(以字节为单位)。Gets the length of a stream, in bytes.

(继承自 PipeStream)
OutBufferSize

获取管道的出站缓冲区的大小(以字节为单位)。Gets the size, in bytes, of the outbound buffer for a pipe.

(继承自 PipeStream)
Position

获取或设置当前流的当前位置。Gets or sets the current position of the current stream.

(继承自 PipeStream)
ReadMode

获取或设置 PipeStream 对象的读取模式。Gets or sets the reading mode for a PipeStream object.

(继承自 PipeStream)
ReadTimeout

获取或设置一个值(以毫秒为单位),该值确定流在超时前将尝试读取的时间。Gets or sets a value, in milliseconds, that determines how long the stream will attempt to read before timing out.

(继承自 Stream)
SafePipeHandle

获取当前 PipeStream 对象所封装的本地管道末端的安全句柄。Gets the safe handle for the local end of the pipe that the current PipeStream object encapsulates.

(继承自 PipeStream)
TransmissionMode

获取当前管道支持的管道传输模式。Gets the pipe transmission mode supported by the current pipe.

(继承自 PipeStream)
WriteTimeout

获取或设置一个值(以毫秒为单位),该值确定流在超时前将尝试写入多长时间。Gets or sets a value, in milliseconds, that determines how long the stream will attempt to write before timing out.

(继承自 Stream)

方法

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)

开始执行异步读取操作。Begins an asynchronous read operation.

(继承自 PipeStream)
BeginWaitForConnection(AsyncCallback, Object)

开始异步操作以等待客户端建立连接。Begins an asynchronous operation to wait for a client to connect.

BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)

开始执行异步写入操作。Begins an asynchronous write operation.

(继承自 PipeStream)
CheckPipePropertyOperations()

验证管道是否处于可供获取或设置属性的正确状态。Verifies that the pipe is in a proper state for getting or setting properties.

(继承自 PipeStream)
CheckReadOperations()

验证管道是否处于可供进行读操作的连接状态。Verifies that the pipe is in a connected state for read operations.

(继承自 PipeStream)
CheckWriteOperations()

验证管道是否处于可供进行写操作的连接状态。Verifies that the pipe is in a connected state for write operations.

(继承自 PipeStream)
Close()

关闭当前流并释放与之关联的所有资源(如套接字和文件句柄)。Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. 不直接调用此方法,而应确保流得以正确释放。Instead of calling this method, ensure that the stream is properly disposed.

(继承自 Stream)
CopyTo(Stream)

从当前流中读取字节并将其写入到另一流中。Reads the bytes from the current stream and writes them to another stream.

(继承自 Stream)
CopyTo(Stream, Int32)

使用指定的缓冲区大小,从当前流中读取字节并将其写入到另一流中。Reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

(继承自 Stream)
CopyToAsync(Stream)

从当前流中异步读取字节并将其写入到另一个流中。Asynchronously reads the bytes from the current stream and writes them to another stream.

(继承自 Stream)
CopyToAsync(Stream, CancellationToken)

通过指定的取消令牌,从当前流中异步读取字节并将其写入到另一个流中。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified cancellation token.

(继承自 Stream)
CopyToAsync(Stream, Int32)

使用指定的缓冲区大小,从当前流中异步读取字节并将其写入到另一流中。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

(继承自 Stream)
CopyToAsync(Stream, Int32, CancellationToken)

使用指定的缓冲区大小和取消令牌,从当前流中异步读取字节并将其写入到另一个流中。Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token.

(继承自 Stream)
CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
CreateWaitHandle()

分配 WaitHandle 对象。Allocates a WaitHandle object.

(继承自 Stream)
Disconnect()

断开当前连接。Disconnects the current connection.

Dispose()

释放 Stream 使用的所有资源。Releases all resources used by the Stream.

(继承自 Stream)
Dispose(Boolean)

释放 PipeStream 类使用的非托管资源,并可以选择释放托管资源。Releases the unmanaged resources used by the PipeStream class and optionally releases the managed resources.

(继承自 PipeStream)
DisposeAsync()

异步释放 Stream 使用的非托管资源。Asynchronously releases the unmanaged resources used by the Stream.

(继承自 Stream)
EndRead(IAsyncResult)

结束挂起的异步读取请求。Ends a pending asynchronous read request.

(继承自 PipeStream)
EndWaitForConnection(IAsyncResult)

结束异步操作以等待客户端建立连接。Ends an asynchronous operation to wait for a client to connect.

EndWrite(IAsyncResult)

结束挂起的异步写入请求。Ends a pending asynchronous write request.

(继承自 PipeStream)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Finalize()

在通过垃圾回收将 NamedPipeServerStream 实例回收之前,释放非托管资源并执行其他清理操作。Releases unmanaged resources and performs other cleanup operations before the NamedPipeServerStream instance is reclaimed by garbage collection.

Flush()

清除当前流的缓冲区,并使所有缓冲的数据都写入到基础设备。Clears the buffer for the current stream and causes any buffered data to be written to the underlying device.

(继承自 PipeStream)
FlushAsync()

异步清除此流的所有缓冲区并导致所有缓冲数据都写入基础设备中。Asynchronously clears all buffers for this stream and causes any buffered data to be written to the underlying device.

(继承自 Stream)
FlushAsync(CancellationToken)

异步清理这个流的所有缓冲区,并使所有缓冲数据写入基础设备,并且监控取消请求。Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.

(继承自 Stream)
GetAccessControl()

获取一个 PipeSecurity 对象,该对象封装当前 PipeStream 对象所描述管道的访问控制列表 (ACL) 项。Gets a PipeSecurity object that encapsulates the access control list (ACL) entries for the pipe described by the current PipeStream object.

(继承自 PipeStream)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetImpersonationUserName()

获取管道另一端的客户端用户名。Gets the user name of the client on the other end of the pipe.

GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InitializeHandle(SafePipeHandle, Boolean, Boolean)

从指定的 PipeStream 对象中初始化 SafePipeHandle 对象。Initializes a PipeStream object from the specified SafePipeHandle object.

(继承自 PipeStream)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
ObjectInvariant()

提供对 Contract 的支持。Provides support for a Contract.

(继承自 Stream)
Read(Byte[], Int32, Int32)

从指定长度的指定位置开始,从流中读取一个字节块,并将数据写入指定的缓冲区。Reads a block of bytes from a stream and writes the data to a specified buffer starting at a specified position for a specified length.

(继承自 PipeStream)
Read(Span<Byte>)

从当前流中读取一个字节序列,将其写入字节数组,并按读取的字节数向前移动流中的位置。Reads a sequence of bytes from the current stream, writes them to a byte array, and advances the position within the stream by the number of bytes read.

(继承自 PipeStream)
ReadAsync(Byte[], Int32, Int32)

从当前流异步读取字节序列,并将流中的位置提升读取的字节数。Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

(继承自 Stream)
ReadAsync(Byte[], Int32, Int32, CancellationToken)

从指定字节数的指定位置开始,将当前流中的字节序列异步读取到字节数组,按读取的字节数向前移动流中的位置,并监视取消请求。Asynchronously reads a sequence of bytes from the current stream to a byte array starting at a specified position for a specified number of bytes, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

(继承自 PipeStream)
ReadAsync(Memory<Byte>, CancellationToken)

从当前流异步读取字节的序列,将其写入字节内存范围,按读取的字节数向前移动流中的位置,并监视取消请求。Asynchronously reads a sequence of bytes from the current stream, writes them to a byte memory range, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

(继承自 PipeStream)
ReadByte()

从管道读取字节。Reads a byte from a pipe.

(继承自 PipeStream)
RunAsClient(PipeStreamImpersonationWorker)

模拟客户端时调用委托。Calls a delegate while impersonating the client.

Seek(Int64, SeekOrigin)

将当前流的当前位置设置为指定值。Sets the current position of the current stream to the specified value.

(继承自 PipeStream)
SetAccessControl(PipeSecurity)

PipeSecurity 对象所指定的访问控制列表 (ACL) 项应用于由当前 PipeStream 对象指定的管道。Applies the access control list (ACL) entries specified by a PipeSecurity object to the pipe specified by the current PipeStream object.

(继承自 PipeStream)
SetLength(Int64)

将当前流的长度设为指定值。Sets the length of the current stream to the specified value.

(继承自 PipeStream)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
WaitForConnection()

等待客户端连接到该 NamedPipeServerStream 对象。Waits for a client to connect to this NamedPipeServerStream object.

WaitForConnectionAsync()

异步等待客户端连接到该 NamedPipeServerStream 对象。Asynchronously waits for a client to connect to this NamedPipeServerStream object.

WaitForConnectionAsync(CancellationToken)

异步等待客户端连接到该 NamedPipeServerStream 对象并监视取消请求。Asynchronously waits for a client to connect to this NamedPipeServerStream object and monitors cancellation requests.

WaitForPipeDrain()

等待管道另一端读取所有发送的字节。Waits for the other end of the pipe to read all sent bytes.

(继承自 PipeStream)
Write(Byte[], Int32, Int32)

使用缓冲区中的数据将字节块写入当前流。Writes a block of bytes to the current stream using data from a buffer.

(继承自 PipeStream)
Write(ReadOnlySpan<Byte>)

将字节序列写入当前流,并按写入的字节数向前调整流的当前位置。Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

(继承自 PipeStream)
WriteAsync(Byte[], Int32, Int32)

将字节序列异步写入当前流,并将流的当前位置提升写入的字节数。Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

(继承自 Stream)
WriteAsync(Byte[], Int32, Int32, CancellationToken)

从字节数组的指定位置开始异步写入指定字节数,按写入的字节数向前移动此流中的当前位置,并监视取消请求。Asynchronously writes a specified number of bytes from a byte array starting at a specified position, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

(继承自 PipeStream)
WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

将字节的序列异步写入当前流,将该流中的当前位置向前移动写入的字节数,并监视取消请求。Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

(继承自 PipeStream)
WriteByte(Byte)

将字节写入当前流。Writes a byte to the current stream.

(继承自 PipeStream)

显式接口实现

IDisposable.Dispose()

释放 Stream 使用的所有资源。Releases all resources used by the Stream.

(继承自 Stream)

扩展方法

GetAccessControl(PipeStream)
SetAccessControl(PipeStream, PipeSecurity)

适用于