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
Public Class NetworkStream
Inherits Stream
继承

示例

下面的代码示例演示如何从连接的 StreamSocket 创建 NetworkStream 并执行基本的同步阻塞 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. 有关阻止和非阻止 Sockets 的详细信息,请参阅使用异步客户端套接字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. 你还可以指定 NetworkStream 对提供 SocketFileAccess 权限。You 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,则必须指定 ownsSocket 参数的值 trueIf you want the NetworkStream to have permission to close the provided Socket, you must specify true for the value of the ownsSocket parameter.

WriteRead 方法用于简单的单线程同步阻塞 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. 当前不支持此属性,它始终返回 falseThis property is not currently supported.This property always returns false.

CanTimeout

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

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.

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.

方法

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

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

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

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

Close()

关闭 NetworkStreamCloses the NetworkStream.

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.

EndWrite(IAsyncResult)

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

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.

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.

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(Memory<Byte>, CancellationToken)

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

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.

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

返回一个表示当前对象的 string。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.

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(ReadOnlyMemory<Byte>, CancellationToken)

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

WriteByte(Byte)

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

显式界面实现

IDisposable.Dispose()

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

适用于

另请参阅