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從已連接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

備註

類別提供在封鎖模式中透過Stream通訊端傳送和接收資料的方法。 NetworkStreamThe 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,您必須提供 [已Socket連接]。To create a NetworkStream, you must provide a connected Socket. 您也可以指定對FileAccess提供SocketNetworkStream具有哪些許可權。You can also specify what FileAccess permission the NetworkStream has over the provided Socket. 根據預設,關閉NetworkStream並不會關閉提供Socket的。By 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.

針對簡單的Read單一執行緒同步封鎖 i/o 使用和方法。WriteUse the Write and Read methods for simple single thread synchronous blocking I/O. 如果您想要使用個別的執行緒來處理 i/o,請考慮BeginWrite使用和EndWrite方法,或是BeginRead用於通訊的EndRead和方法。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屬性Seek 、讀取Length屬性或呼叫方法,將會擲回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)

為指定的 NetworkStream 建立 Socket 類別的新執行個體。Creates a new instance of the NetworkStream class for the specified Socket.

NetworkStream(Socket, Boolean) NetworkStream(Socket, Boolean) NetworkStream(Socket, Boolean) NetworkStream(Socket, Boolean)

使用指定的 NetworkStream 擁有權,為指定的 Socket 初始化 Socket 類別的新執行個體。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)

使用指定的存取權限,建立指定 NetworkStreamSocket 類別新執行個體。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)

使用指定的存取權限和指定的 NetworkStream 擁有權,建立指定 SocketSocket 類別新執行個體。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

取得或設定值,指出是否可以讀取 NetworkStreamGets 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)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。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()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。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 的淺層複本 (Shallow Copy)。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 and stores it to a byte array.

Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>)

NetworkStream 讀取資料,並將其儲存於記憶體中的位元組範圍。Reads data from the NetworkStream and stores it to a span of bytes in memory.

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)

以非同步作業從 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) ReadAsync(Memory<Byte>, CancellationToken) ReadAsync(Memory<Byte>, CancellationToken) ReadAsync(Memory<Byte>, CancellationToken)

以非同步作業從 NetworkStream 讀取資料,並將其儲存於位元組記憶體範圍。Reads data from the NetworkStream and stores it in a byte memory range as an asynchronous operation.

ReadByte() ReadByte() ReadByte() 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) 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 from a specified range of a byte array.

Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>) Write(ReadOnlySpan<Byte>)

將資料從唯讀位元組範圍寫入至 NetworkStreamWrites data to the NetworkStream from a read-only byte span.

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)

以非同步作業將資料從指定的位元組陣列範圍寫入至 NetworkStreamWrites data to the NetworkStream from the specified range of a byte array as an asynchronous operation.

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

以非同步作業將資料從唯讀記憶體位元組記憶體範圍寫入至 NetworkStreamWrites data to the NetworkStream from a read-only memory byte memory range as an asynchronous operation.

WriteByte(Byte) WriteByte(Byte) WriteByte(Byte) 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() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

釋放 NetworkStream 所使用的所有資源。Releases all resources used by the NetworkStream.

適用於

另請參閱