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)

為指定的 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.

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.

適用於

另請參閱