語言

NegotiateStream.Write(Byte[], Int32, Int32) 方法

定義

利用指定的緩衝區和偏移量,將指定數量 Byte的 s 寫入底層串流。

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

參數

buffer
Byte[]

一個 Byte 陣列,用來提供寫入串流的位元組。

offset
Int32

an Int32 包含以零為 buffer 基礎的位置,從那裡開始讀取要寫入資料串流的位元組。

count
Int32

Int32 A 包含從 中讀取buffer的位元組數。

例外狀況

buffer 是 null。

offset 小於0。

-或-

offset 大於 的 buffer長度。

-或-

offset 加數大於 的 buffer長度。

寫入操作失敗了。

-或-

雖然有加密技術,但資料無法加密。

已經有一個寫入操作正在進行中。

這個物件已經關閉了。

尚未進行認證。

範例

以下程式碼範例示範如何寫入 NegotiateStream。

    public static void Main(String[] args)
    {
        // Establish the remote endpoint for the socket.
        // For this example, use the local machine.
        IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        // Client and server use port 11000.
        IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
        // Create a TCP/IP socket.
       TcpClient client = new TcpClient();
        // Connect the socket to the remote endpoint.
        client.Connect(remoteEP);
        Console.WriteLine("Client connected to {0}.",
            remoteEP.ToString());
        // Ensure the client does not close when there is
        // still data to be sent to the server.
        client.LingerState = (new LingerOption(true,0));
        // Request authentication.
        NetworkStream clientStream = client.GetStream();
        NegotiateStream authStream = new NegotiateStream(clientStream);
        // Request authentication for the client only (no mutual authentication).
        // Authenicate using the client's default credetials.
        // Permit the server to impersonate the client to access resources on the server only.
        // Request that data be transmitted using encryption and data signing.
        authStream.AuthenticateAsClient(
             (NetworkCredential) CredentialCache.DefaultCredentials,
             "",
             ProtectionLevel.EncryptAndSign,
             TokenImpersonationLevel.Impersonation);
        DisplayAuthenticationProperties(authStream);
        DisplayStreamProperties(authStream);
        if (authStream.CanWrite)
        {
             // Encode the test data into a byte array.
            byte[] message = System.Text.Encoding.UTF8.GetBytes("Hello from the client.");
            authStream.Write(message, 0, message.Length);
            authStream.Flush();
            Console.WriteLine("Sent {0} bytes.", message.Length);
        }
        // Close the client connection.
        authStream.Close();
        Console.WriteLine("Client closed.");
}

備註

若啟用加密、簽名或加密加簽名,此方法會從緩衝區讀取資料,進行加密、簽署或加密並簽署,並透過底層串流傳輸。 若未使用資料加密或簽章等安全服務,此方法會 Write 呼叫底層串流。

此方法在寫入操作完成時會阻塞。 為了防止操作完成時阻塞,請使用此 WriteAsync 方法。

在成功驗證之前,你不能呼叫這個方法。 要驗證,請呼叫其中一個 AuthenticateAsClient、 AuthenticateAsClientAsync、 BeginAuthenticateAsClient、 AuthenticateAsServerAuthenticateAsServerAsync、 或 BeginAuthenticateAsServer 方法。

此 NegotiateStream 類別不支援多個同時寫入操作。 如果你嘗試在另一個寫入操作同時啟動,而另一個寫入操作已經在同一串流 NotSupportedException 上執行,將會拋出異常。

適用於