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입니다.

예외

buffer이(가) null인 경우

offset is less than 0.

또는 offsetbuffer의 길이보다 큽니다.

또는 offset과 count의 합이 buffer의 길이보다 큰 경우.

쓰기 작업이 실패했습니다.

또는

암호화를 사용 중이지만 데이터를 암호화할 수 없는 경우.

쓰기 작업을 진행 중인 경우

이 개체가 닫힌 경우.

인증이 수행되지 않은 경우

예제

다음 코드 예제에서는 .에 쓰는 방법을 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.");
}

설명

암호화, 서명 또는 암호화 및 서명을 사용하는 경우 이 메서드는 버퍼에서 데이터를 읽고, 데이터를 암호화, 서명 또는 암호화하고, 서명하고, 기본 스트림을 사용하여 전송합니다. 데이터 암호화 또는 서명과 같은 보안 서비스를 사용하지 않는 경우 이 메서드는 기본 스트림에서 호출합니다 Write .

이 메서드는 쓰기 작업이 완료되는 동안 차단합니다. 작업이 완료되는 동안 차단을 방지하려면 메서드를 WriteAsync 사용합니다.

성공적으로 인증될 때까지 이 메서드를 호출할 수 없습니다. 인증하려면 , , AuthenticateAsClientAsync, AuthenticateAsServerAsyncBeginAuthenticateAsClientAuthenticateAsServer또는 BeginAuthenticateAsServer 메서드 중 AuthenticateAsClient하나를 호출합니다.

클래스는 NegotiateStream 여러 동시 쓰기 작업을 지원하지 않습니다. 다른 쓰기 작업이 동일한 스트림에서 이미 실행되는 동안 쓰기 작업을 시작하려고 하면 예외가 NotSupportedException throw됩니다.

적용 대상