NetworkStream 类

定义

为网络访问提供数据的基础流。Provides the underlying stream of data for network access.

public ref class NetworkStream : System::IO::Stream
public class NetworkStream : System.IO.Stream
type NetworkStream = class
    inherit Stream
type NetworkStream = class
    inherit Stream
    interface IDisposable
Public Class NetworkStream
Inherits Stream
继承
NetworkStream
继承
实现

示例

下面的代码示例演示如何 NetworkStream 从连接的创建 Stream Socket ,并执行基本的同步阻塞 i/o。The following code example demonstrates how to create a NetworkStream from a connected StreamSocket and perform basic synchronous blocking I/O.

// Examples for constructors that do not specify file permission.
// Create the NetworkStream for communicating with the remote host.
NetworkStream^ myNetworkStream;

if ( networkStreamOwnsSocket )
{
   myNetworkStream = gcnew NetworkStream( mySocket,true );
}
else
{
   myNetworkStream = gcnew NetworkStream( mySocket );
}
// Examples for constructors that do not specify file permission.

// Create the NetworkStream for communicating with the remote host.
NetworkStream myNetworkStream;

if (networkStreamOwnsSocket){
     myNetworkStream = new NetworkStream(mySocket, true);
}
else{
     myNetworkStream = new NetworkStream(mySocket);
}
' Examples for constructors that do not specify file permission.
' Create the NetworkStream for communicating with the remote host.
Dim myNetworkStream As NetworkStream

If networkStreamOwnsSocket Then
   myNetworkStream = New NetworkStream(mySocket, True)
Else
   myNetworkStream = New NetworkStream(mySocket)
End If

注解

NetworkStream类提供用于 Stream 在阻止模式下通过套接字发送和接收数据的方法。The NetworkStream class provides methods for sending and receiving data over Stream sockets in blocking mode. 有关阻止和非阻止性的详细信息 Socket ,请参阅 使用异步客户端套接字For more information about blocking versus nonblocking Sockets, see Using an Asynchronous Client Socket. 可以将类用于 NetworkStream 同步和异步数据传输。You can use the NetworkStream class for both synchronous and asynchronous data transfer. 有关同步和异步通信的详细信息,请参阅 套接字For more information about synchronous and asynchronous communication, see Sockets.

若要创建 NetworkStream ,必须提供连接的 SocketTo create a NetworkStream, you must provide a connected Socket. 你还可以指定对 FileAccess NetworkStream 提供的具有哪些权限 SocketYou can also specify what FileAccess permission the NetworkStream has over the provided Socket. 默认情况下,关闭 NetworkStream 不会关闭提供的 SocketBy default, closing the NetworkStream does not close the provided Socket. 如果希望 NetworkStream 具有关闭提供的权限 Socket ,则必须 true 为参数的值指定 ownsSocketIf you want the NetworkStream to have permission to close the provided Socket, you must specify true for the value of the ownsSocket parameter.

Write Read 对于简单的单线程同步阻塞 i/o,使用和方法。Use the Write and Read methods for simple single thread synchronous blocking I/O. 如果要使用单独的线程来处理 i/o,请考虑使用 BeginWriteEndWrite 方法,或者使用 BeginReadEndRead 方法进行通信。If you want to process your I/O using separate threads, consider using the BeginWrite and EndWrite methods, or the BeginRead and EndRead methods for communication.

NetworkStream 支持对网络数据流进行随机访问。The NetworkStream does not support random access to the network data stream. 属性的值 CanSeek ,该值指示流是否支持查找,始终为 false ; 读取 Position 属性、读取 Length 属性或调用 Seek 方法将引发 NotSupportedExceptionThe value of the CanSeek property, which indicates whether the stream supports seeking, is always false; reading the Position property, reading the Length property, or calling the Seek method will throw a NotSupportedException.

可以在类的实例上同时执行读写操作, NetworkStream 而无需进行同步。Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. 只要有一个唯一的线程用于写入操作,另一个线程用于读取操作,读取和写入线程之间就不会有交叉干扰,也不需要进行同步。As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.

构造函数

NetworkStream(Socket)

为指定的 Socket 创建 NetworkStream 类的新实例。Creates a new instance of the NetworkStream class for the specified Socket.

NetworkStream(Socket, Boolean)

用指定的 Socket 所属权为指定的 Socket 初始化 NetworkStream 类的新实例。Initializes a new instance of the NetworkStream class for the specified Socket with the specified Socket ownership.

NetworkStream(Socket, FileAccess)

用指定的访问权限为指定的 NetworkStream 创建 Socket 类的新实例。Creates a new instance of the NetworkStream class for the specified Socket with the specified access rights.

NetworkStream(Socket, FileAccess, Boolean)

用指定的访问权限和指定的 Socket 所属权为指定的 Socket 创建 NetworkStream 类的新实例。Creates a new instance of the NetworkStream class for the specified Socket with the specified access rights and the specified Socket ownership.

属性

CanRead

获取一个值,该值指示 NetworkStream 是否支持读取。Gets a value that indicates whether the NetworkStream supports reading.

CanSeek

获取一个值,该值指示流是否支持查找。Gets a value that indicates whether the stream supports seeking. 目前不支持此属性。This property is not currently supported. 此属性始终返回 falseThis property always returns false.

CanTimeout

指示超时属性是否可用于 NetworkStreamIndicates whether timeout properties are usable for NetworkStream.

CanTimeout

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

(继承自 Stream)
CanWrite

获取一个值,该值指示 NetworkStream 是否支持写入。Gets a value that indicates whether the NetworkStream supports writing.

DataAvailable

获取一个值,该值指示在要读取的 NetworkStream 上是否有可用的数据。Gets a value that indicates whether data is available on the NetworkStream to be read.

Length

获取流上可用数据的长度。Gets the length of the data available on the stream. 此属性当前不受支持,总是引发 NotSupportedExceptionThis property is not currently supported and always throws a NotSupportedException.

Position

获取或设置流中的当前位置。Gets or sets the current position in the stream. 此属性当前不受支持,总是引发 NotSupportedExceptionThis property is not currently supported and always throws a NotSupportedException.

Readable

获取或设置一个值,该值指示 NetworkStream 是否可以读取。Gets or sets a value that indicates whether the NetworkStream can be read.

ReadTimeout

获取或设置读取操作阻止等待数据的时间。Gets or sets the amount of time that a read operation blocks waiting for data.

ReadTimeout

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

(继承自 Stream)
Socket

获取基础 SocketGets the underlying Socket.

Writeable

获取一个值,该值指示 NetworkStream 是否可写入数据。Gets a value that indicates whether the NetworkStream is writable.

WriteTimeout

获取或设置写入操作阻止等待数据的时间。Gets or sets the amount of time that a write operation blocks waiting for data.

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)

NetworkStream 开始异步读取。Begins an asynchronous read from the NetworkStream.

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

开始异步读操作。Begins an asynchronous read operation. (请考虑改用 ReadAsync(Byte[], Int32, Int32)。)(Consider using ReadAsync(Byte[], Int32, Int32) instead.)

(继承自 Stream)
BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)

开始向流异步写入。Begins an asynchronous write to a stream.

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

开始异步写操作。Begins an asynchronous write operation. (请考虑改用 WriteAsync(Byte[], Int32, Int32)。)(Consider using WriteAsync(Byte[], Int32, Int32) instead.)

(继承自 Stream)
Close()

关闭 NetworkStreamCloses the NetworkStream.

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)
Close(Int32)

等待指定的时间获许发送数据之后,关闭 NetworkStreamCloses the NetworkStream after waiting the specified time to allow data to be sent.

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)
Dispose()

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

(继承自 Stream)
Dispose(Boolean)

释放由 NetworkStream 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the NetworkStream and optionally releases the managed resources.

DisposeAsync()

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

(继承自 Stream)
EndRead(IAsyncResult)

处理异步读取的结束。Handles the end of an asynchronous read.

EndRead(IAsyncResult)

等待挂起的异步读取完成。Waits for the pending asynchronous read to complete. (请考虑改用 ReadAsync(Byte[], Int32, Int32)。)(Consider using ReadAsync(Byte[], Int32, Int32) instead.)

(继承自 Stream)
EndWrite(IAsyncResult)

处理异步写入的结束。Handles the end of an asynchronous write.

EndWrite(IAsyncResult)

结束异步写操作。Ends an asynchronous write operation. (请考虑改用 WriteAsync(Byte[], Int32, Int32)。)(Consider using WriteAsync(Byte[], Int32, Int32) instead.)

(继承自 Stream)
Equals(Object)

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

(继承自 Object)
Finalize()

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

Flush()

刷新流中的数据。Flushes data from the stream. 此方法保留供将来使用。This method is reserved for future use.

FlushAsync()

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

(继承自 Stream)
FlushAsync(CancellationToken)

以异步操作形式刷新流中的数据。Flushes data from the stream as an asynchronous operation.

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)
GetHashCode()

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

(继承自 Object)
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)
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)

NetworkStream 读取数据,并将其存储到字节数组中。Reads data from the NetworkStream and stores it to a byte array.

Read(Span<Byte>)

NetworkStream 读取数据,并将其存储到内存中的字节范围内。Reads data from the NetworkStream and stores it to a span of bytes in memory.

Read(Span<Byte>)

当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。When overridden in a derived class, 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)

从当前流异步读取字节序列,并将流中的位置提升读取的字节数。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)

以异步操作形式,从 NetworkStream 读取数据,并将其存储到字节数组的指定范围内。Reads data from the NetworkStream and stores it to a specified range of a byte array as an asynchronous operation.

ReadAsync(Byte[], Int32, Int32, CancellationToken)

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

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

以异步操作形式,从 NetworkStream 读取数据,并将其存储在字节内存范围内。Reads data from the NetworkStream and stores it in a byte memory range as an asynchronous operation.

ReadAsync(Memory<Byte>, CancellationToken)

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

(继承自 Stream)
ReadByte()

NetworkStream 中读取一个字节,并将流内的位置向前提升一个字节,或者如果已到达流结尾,则返回 -1。Reads a byte from the NetworkStream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

ReadByte()

从流中读取一个字节,并将流内的位置向前提升一个字节,或者如果已到达流结尾,则返回 -1。Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

(继承自 Stream)
Seek(Int64, SeekOrigin)

将流的当前位置设置为给定值。Sets the current position of the stream to the given value. 此方法当前不受支持,总是引发 NotSupportedExceptionThis method is not currently supported and always throws a NotSupportedException.

SetLength(Int64)

设置流的长度。Sets the length of the stream. 此方法始终引发 NotSupportedExceptionThis method always throws a NotSupportedException.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

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

从字节数组的指定范围向 NetworkStream 写入数据。Writes data to the NetworkStream from a specified range of a byte array.

Write(ReadOnlySpan<Byte>)

从只读字节范围向 NetworkStream 写入数据。Writes data to the NetworkStream from a read-only byte span.

Write(ReadOnlySpan<Byte>)

当在派生类中重写时,向当前流中写入字节序列,并将此流中的当前位置提升写入的字节数。When overridden in a derived class, 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)

将字节序列异步写入当前流,并将流的当前位置提升写入的字节数。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)

以异步操作形式,将指定字节数组范围内的数据写入 NetworkStreamWrites data to the NetworkStream from the specified range of a byte array as an asynchronous operation.

WriteAsync(Byte[], Int32, Int32, 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.

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

以异步操作的形式,将只读内存字节内存范围中的数据写入 NetworkStreamWrites data to the NetworkStream from a read-only memory byte memory range as an asynchronous operation.

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.

(继承自 Stream)
WriteByte(Byte)

将一个字节写入 NetworkStream 内的当前位置,并将流内的位置向前提升一个字节。Writes a byte to the current position in the NetworkStream and advances the position within the stream by one byte.

WriteByte(Byte)

将一个字节写入流内的当前位置,并将流内的位置向前提升一个字节。Writes a byte to the current position in the stream and advances the position within the stream by one byte.

(继承自 Stream)

显式接口实现

IDisposable.Dispose()

此 API 支持产品基础结构,不能在代码中直接使用。

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

IDisposable.Dispose()

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

(继承自 Stream)

扩展方法

ConfigureAwait(IAsyncDisposable, Boolean)

配置如何执行从异步可处置项返回的任务的等待。Configures how awaits on the tasks returned from an async disposable are performed.

适用于

另请参阅