NetworkStream.Write 方法

定義

多載

Write(ReadOnlySpan<Byte>)

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

Write(Byte[], Int32, Int32)

將資料從指定的位元組陣列範圍寫入至 NetworkStreamWrites data to the NetworkStream from a specified range of a byte array.

Write(ReadOnlySpan<Byte>)

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

public:
 override void Write(ReadOnlySpan<System::Byte> buffer);
public override void Write (ReadOnlySpan<byte> buffer);
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))

參數

buffer
ReadOnlySpan<Byte>

要寫入 NetworkStream 的資料。The data to write to the NetworkStream.

例外狀況

NetworkStream 不支援寫入。The NetworkStream does not support writing.

存取通訊端時發生錯誤。An error occurred when accessing the socket.

-或--or-

寫入網路時發生失敗。There was a failure while writing to the network.

備註

這個方法會將 buffer 中的所有位元組傳送到網路。This method sends all bytes in buffer to the network. Write 方法會封鎖,直到傳送要求的位元組數目或擲回 SocketException 為止。The Write method blocks until the requested number of bytes is sent or a SocketException is thrown.

注意

藉由呼叫 CanWrite 屬性,查看 NetworkStream 是否可寫入。Check to see if the NetworkStream is writable by calling the CanWrite property. 如果您嘗試寫入無法寫入的 NetworkStream,您會收到 InvalidOperationExceptionIf you attempt to write to a NetworkStream that is not writable, you will get an InvalidOperationException.

注意

如果您收到 IOException,請檢查 InnerException 屬性,判斷其是否由 SocketException所造成。If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. 若是如此,請使用 ErrorCode 屬性來取得特定的錯誤碼,並參閱 Windows Socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。If so, use the ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Write(Byte[], Int32, Int32)

將資料從指定的位元組陣列範圍寫入至 NetworkStreamWrites data to the NetworkStream from a specified range of a byte array.

public:
 override void Write(cli::array <System::Byte> ^ buffer, int offset, int size);
public override void Write (byte[] buffer, int offset, int size);
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (buffer As Byte(), offset As Integer, size As Integer)

參數

buffer
Byte[]

Byte 型別的陣列,包含要寫入至 NetworkStream 的資料。An array of type Byte that contains the data to write to the NetworkStream.

offset
Int32

buffer 中的位置,要開始自該處寫入資料。The location in buffer from which to start writing data.

size
Int32

要寫入至 NetworkStream 的位元組數。The number of bytes to write to the NetworkStream.

例外狀況

buffer 參數為 nullThe buffer parameter is null.

offset 參數小於 0。The offset parameter is less than 0.

-或--or- offset 參數大於 buffer 的長度。The offset parameter is greater than the length of buffer.

-或--or- size 參數小於 0。The size parameter is less than 0.

-或--or- size 參數大於 buffer 的長度減去 offset 參數值。The size parameter is greater than the length of buffer minus the value of the offset parameter.

NetworkStream 不支援寫入。The NetworkStream does not support writing.

存取通訊端時發生錯誤。An error occurred when accessing the socket.

-或--or-

寫入網路時發生失敗。There was a failure while writing to the network.

範例

下列程式碼範例會檢查 NetworkStream 是否為可寫入。The following code example checks to see whether the NetworkStream is writable. 如果是,則會使用 Write 來寫入小型訊息。If it is, then Write is used to write a small message.

// Examples for CanWrite, and CanWrite  
// Check to see if this NetworkStream is writable.
if ( myNetworkStream->CanWrite )
{
   array<Byte>^ myWriteBuffer = Encoding::ASCII->GetBytes(
      "Are you receiving this message?" );
   myNetworkStream->Write( myWriteBuffer, 0, myWriteBuffer->Length );
}
else
{
   Console::WriteLine( "Sorry.  You cannot write to this NetworkStream." );
}
 // Examples for CanWrite, and CanWrite  

 // Check to see if this NetworkStream is writable.
 if (myNetworkStream.CanWrite){
    
      byte[] myWriteBuffer = Encoding.ASCII.GetBytes("Are you receiving this message?");
      myNetworkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length);
 }
 else{
      Console.WriteLine("Sorry.  You cannot write to this NetworkStream.");  
 }

' Examples for CanWrite, and CanWrite  
' Check to see if this NetworkStream is writable.
If myNetworkStream.CanWrite Then
   
   Dim myWriteBuffer As Byte() = Encoding.ASCII.GetBytes("Are you receiving this message?")
   myNetworkStream.Write(myWriteBuffer, 0, myWriteBuffer.Length)
Else
   Console.WriteLine("Sorry.  You cannot write to this NetworkStream.")
End If

備註

這個方法會從指定的 offset 開始,並從 buffer 的內容將 size 位元組傳送到網路。This method starts at the specified offset and sends size bytes from the contents of buffer to the network. Write 方法會封鎖,直到傳送要求的位元組數目或擲回 SocketException 為止。The Write method blocks until the requested number of bytes is sent or a SocketException is thrown.

注意

藉由呼叫 CanWrite 屬性,查看 NetworkStream 是否可寫入。Check to see if the NetworkStream is writable by calling the CanWrite property. 如果您嘗試寫入無法寫入的 NetworkStream,您會收到 InvalidOperationExceptionIf you attempt to write to a NetworkStream that is not writable, you will get an InvalidOperationException.

注意

如果您收到 IOException,請檢查 InnerException 屬性,判斷其是否由 SocketException所造成。If you receive an IOException, check the InnerException property to determine if it was caused by a SocketException. 若是如此,請使用 ErrorCode 屬性來取得特定的錯誤碼,並參閱 Windows Socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。If so, use the ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

另請參閱

適用於