NetworkStream NetworkStream NetworkStream NetworkStream Class

定义

为网络访问提供数据的基础流。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
继承

示例

下面的代码示例演示如何创建NetworkStream从连接StreamSocket并执行基本的同步阻塞 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. 此外可以指定哪些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的值的ownsSocket参数。If 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方法将引发NotSupportedException.The 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) NetworkStream(Socket) NetworkStream(Socket) NetworkStream(Socket)

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

NetworkStream(Socket, Boolean) NetworkStream(Socket, Boolean) NetworkStream(Socket, Boolean) 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, FileAccess) NetworkStream(Socket, FileAccess) 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) NetworkStream(Socket, FileAccess, Boolean) NetworkStream(Socket, FileAccess, Boolean) 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 CanRead CanRead CanRead

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

CanSeek CanSeek CanSeek CanSeek

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

CanTimeout CanTimeout CanTimeout CanTimeout

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

CanWrite CanWrite CanWrite CanWrite

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

DataAvailable DataAvailable DataAvailable DataAvailable

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

Length Length Length Length

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

Position Position Position Position

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

Readable Readable Readable Readable

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

ReadTimeout ReadTimeout ReadTimeout ReadTimeout

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

Socket Socket Socket Socket

获取基础 SocketGets the underlying Socket.

Writeable Writeable Writeable Writeable

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

WriteTimeout WriteTimeout WriteTimeout WriteTimeout

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

方法

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

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

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

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

Close() Close() Close() Close()

关闭 NetworkStreamCloses the NetworkStream.

Close(Int32) Close(Int32) Close(Int32) Close(Int32)

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

CopyTo(Stream) CopyTo(Stream) CopyTo(Stream) CopyTo(Stream)

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

(Inherited from Stream)
CopyTo(Stream, Int32) CopyTo(Stream, Int32) CopyTo(Stream, Int32) CopyTo(Stream, Int32)

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

(Inherited from Stream)
CopyToAsync(Stream) CopyToAsync(Stream) CopyToAsync(Stream) CopyToAsync(Stream)

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

(Inherited from Stream)
CopyToAsync(Stream, CancellationToken) CopyToAsync(Stream, CancellationToken) CopyToAsync(Stream, CancellationToken) CopyToAsync(Stream, CancellationToken)

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

(Inherited from Stream)
CopyToAsync(Stream, Int32) CopyToAsync(Stream, Int32) CopyToAsync(Stream, Int32) CopyToAsync(Stream, Int32)

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

(Inherited from Stream)
CopyToAsync(Stream, Int32, CancellationToken) CopyToAsync(Stream, Int32, CancellationToken) CopyToAsync(Stream, Int32, CancellationToken) 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.

(Inherited from Stream)
CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type) CreateObjRef(Type)

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

(Inherited from MarshalByRefObject)
CreateWaitHandle() CreateWaitHandle() CreateWaitHandle() CreateWaitHandle()

分配 WaitHandle 对象。Allocates a WaitHandle object.

(Inherited from Stream)
Dispose() Dispose() Dispose() Dispose()

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

(Inherited from Stream)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

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

DisposeAsync() DisposeAsync() DisposeAsync() DisposeAsync()

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

(Inherited from Stream)
EndRead(IAsyncResult) EndRead(IAsyncResult) EndRead(IAsyncResult) EndRead(IAsyncResult)

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

EndWrite(IAsyncResult) EndWrite(IAsyncResult) EndWrite(IAsyncResult) EndWrite(IAsyncResult)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()

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

Flush() Flush() Flush() Flush()

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

FlushAsync() FlushAsync() FlushAsync() FlushAsync()

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

(Inherited from Stream)
FlushAsync(CancellationToken) FlushAsync(CancellationToken) FlushAsync(CancellationToken) FlushAsync(CancellationToken)

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

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetLifetimeService() GetLifetimeService() GetLifetimeService() GetLifetimeService()

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

(Inherited from MarshalByRefObject)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService() InitializeLifetimeService()

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

(Inherited from MarshalByRefObject)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean) MemberwiseClone(Boolean)

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

(Inherited from MarshalByRefObject)
ObjectInvariant() ObjectInvariant() ObjectInvariant() ObjectInvariant()

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

(Inherited from Stream)
Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32)

NetworkStream 读取数据。Reads data from the NetworkStream.

Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>)
ReadAsync(Byte[], Int32, Int32) ReadAsync(Byte[], Int32, Int32) ReadAsync(Byte[], Int32, Int32) 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.

(Inherited from Stream)
ReadAsync(Byte[], Int32, Int32, CancellationToken) ReadAsync(Byte[], Int32, Int32, CancellationToken) ReadAsync(Byte[], Int32, Int32, CancellationToken) ReadAsync(Byte[], Int32, Int32, CancellationToken)
ReadAsync(Memory<Byte>, CancellationToken) ReadAsync(Memory<Byte>, CancellationToken) ReadAsync(Memory<Byte>, CancellationToken) ReadAsync(Memory<Byte>, CancellationToken)
ReadByte() ReadByte() ReadByte() ReadByte()
Seek(Int64, SeekOrigin) Seek(Int64, SeekOrigin) Seek(Int64, SeekOrigin) 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) SetLength(Int64) SetLength(Int64) SetLength(Int64)

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

ToString() ToString() ToString() ToString()

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

(Inherited from Object)
Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32) Write(Byte[], Int32, Int32)

将数据写入 NetworkStreamWrites data to the NetworkStream.

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

(Inherited from Stream)
WriteAsync(Byte[], Int32, Int32, CancellationToken) WriteAsync(Byte[], Int32, Int32, CancellationToken) WriteAsync(Byte[], Int32, Int32, CancellationToken) WriteAsync(Byte[], Int32, Int32, CancellationToken)
WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)
WriteByte(Byte) WriteByte(Byte) WriteByte(Byte) WriteByte(Byte)

显式界面实现

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

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

适用于

另请参阅