语言

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

定义

使用指定的缓冲区和偏移量将指定的 s 数 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

Int32包含从零开始读取要写入流的字节的位置buffer。

count
Int32

一个 Int32 包含要从 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、、BeginAuthenticateAsClientAuthenticateAsServer、AuthenticateAsServerAsync或BeginAuthenticateAsServer方法。

该 NegotiateStream 类不支持多个同时写入操作。 如果尝试在同一流上执行另一个写入操作时启动写入操作, NotSupportedException 则会引发异常。

适用于