NegotiateStream.Write(Byte[], Int32, Int32) メソッド

定義

指定したバッファーとオフセットを使用して、指定した数の Byteを基になるストリームに書き込みます。

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

ストリームに書き込まれるバイトの読み取りを開始するbufferの 0 から始まる位置を含むInt32

count
Int32

bufferから読み取るバイト数を含むInt32

例外

buffernullです。

offset が 0 未満です。

-または-

offsetbufferの長さを超えています。

-または-

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 メソッドを使用します。

正常に認証されるまで、このメソッドを呼び出すことはできません。 認証するには、 AuthenticateAsClientAuthenticateAsClientAsyncBeginAuthenticateAsClientAuthenticateAsServerAuthenticateAsServerAsync、または BeginAuthenticateAsServer のいずれかのメソッドを呼び出します。

NegotiateStream クラスは、複数の同時書き込み操作をサポートしていません。 別の書き込み操作が同じストリームで既に実行されている間に書き込み操作を開始しようとすると、 NotSupportedException 例外がスローされます。

適用対象