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

定義

指定したバッファーとオフセットを使用して、基になるストリームに指定した Byte 数を書き込みます。Write the specified number of Bytes to the underlying stream using the specified buffer and offset.

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 配列。A Byte array that supplies the bytes written to the stream.

offset
Int32

ストリームに書き込むバイトの読み取りを開始する位置を示す、Int32 内のインデックス番号が 0 から始まる位置を格納する bufferAn Int32 containing the zero-based location in buffer at which to begin reading bytes to be written to the stream.

count
Int32

Int32 から読み取るバイト数を格納する bufferA Int32 containing the number of bytes to read from buffer.

例外

buffernull です。buffer is null.

offset is less than 0 で初期化します。offset is less than 0.

または-or- offsetbuffer の長さを超えています。offset is greater than the length of buffer.

または-or- offset に count を加算した値が、buffer の長さを超えています。offset plus count is greater than the length of buffer.

書き込み操作に失敗しました。The write operation failed.

または-or-

暗号化が使用されていますが、データを暗号化できませんでした。Encryption is in use, but the data could not be encrypted.

既に実行中の書き込み操作が存在します。There is already a write operation in progress.

このオブジェクトは閉じられました。This object has been closed.

認証が行われていません。Authentication has not occurred.

次のコード例は、NegotiateStreamへの書き込みを示しています。The following code example demonstrates writing to a NegotiateStream.

int main()
{
   
   // 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 = gcnew IPEndPoint( ipAddress,11000 );
   
   // Create a TCP/IP socket.
   TcpClient^ client = gcnew TcpClient;
   
   // Connect the socket to the remote endpoint.
   client->Connect( remoteEP );
   Console::WriteLine( L"Client connected to {0}.", remoteEP );
   
   // Ensure the client does not close when there is 
   // still data to be sent to the server.
   client->LingerState = (gcnew LingerOption( true,0 ));
   
   // Request authentication.
   NetworkStream^ clientStream = client->GetStream();
   NegotiateStream^ authStream = gcnew 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( dynamic_cast<NetworkCredential^>(CredentialCache::DefaultCredentials), 
          L"", 
          ProtectionLevel::EncryptAndSign, 
          TokenImpersonationLevel::Impersonation );
   
   DisplayAuthenticationProperties( authStream );
   DisplayStreamProperties( authStream );
   if ( authStream->CanWrite )
   {
      
      // Encode the test data into a byte array.
      array<Byte>^message = System::Text::Encoding::UTF8->GetBytes( L"Hello from the client." );
      authStream->Write( message, 0, message->Length );
      authStream->Flush();
      Console::WriteLine( L"Sent {0} bytes.", message->Length );
   }

   
   // Close the client connection.
   authStream->Close();
   Console::WriteLine( L"Client closed." );
}

    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.");
}

注釈

暗号化、署名、または暗号化と署名が有効になっている場合、このメソッドは、バッファーからデータを読み取り、暗号化、署名、または暗号化と署名を行い、基になるストリームを使用して送信します。If encryption, signing, or encryption and signing are enabled, this method reads the data from buffer, encrypts, signs, or encrypts and signs it, and transmits it using the underlying stream. データの暗号化や署名などのセキュリティサービスが使用されていない場合、このメソッドは、基になるストリームで Write を呼び出します。If no security services such as data encryption or signing are in use, this method invokes Write on the underlying stream.

このメソッドは、書き込み操作が完了するまでブロックします。This method blocks while the write operation completes. 操作の完了中にブロックされないようにするには、Write メソッドを使用します。To prevent blocking while the operation completes, use the Write method.

が正常に認証されるまで、このメソッドを呼び出すことはできません。You cannot call this method until you have successfully authenticated. 認証するには、AuthenticateAsClientBeginAuthenticateAsClientAuthenticateAsServer、または BeginAuthenticateAsServer メソッドのいずれかを呼び出します。To authenticate, call one of the AuthenticateAsClient, BeginAuthenticateAsClient, AuthenticateAsServer, or BeginAuthenticateAsServer methods.

NegotiateStream クラスは、複数の同時書き込み操作をサポートしていません。The NegotiateStream class does not support multiple simultaneous write operations. 同じストリームで別の書き込み操作が既に実行されているときに書き込み操作を開始しようとすると、NotSupportedException 例外がスローされます。If you attempt to start a write operation while another write operation is already executing on the same stream, a NotSupportedException exception will be thrown.

適用対象