NegotiateStream.Flush 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
造成任何緩衝資料都將寫入基礎裝置。
public:
override void Flush();
public override void Flush ();
override this.Flush : unit -> unit
Public Overrides Sub Flush ()
範例
下列程式代碼範例示範如何排清數據流。
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.");
}
備註
這個方法會在基礎數據流上叫用 Flush 。