NetworkStream.WriteTimeout Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau mengatur jumlah waktu operasi tulis memblokir menunggu data.
public:
virtual property int WriteTimeout { int get(); void set(int value); };
public override int WriteTimeout { get; set; }
member this.WriteTimeout : int with get, set
Public Overrides Property WriteTimeout As Integer
Nilai Properti
Int32 yang menentukan jumlah waktu, dalam milidetik, yang akan berlalu sebelum operasi tulis gagal. Nilai default, Infinite, menentukan bahwa operasi tulis tidak kehabisan waktu.
Pengecualian
Nilai yang ditentukan kurang dari atau sama dengan nol dan bukan Infinite.
Contoh
Contoh kode berikut mengatur waktu tulis habis untuk aliran jaringan menjadi 10 milidetik.
#using <System.dll>
using namespace System;
using namespace System::Text;
using namespace System::Net;
using namespace System::Net::Sockets;
int main()
{
// Create the server side connection and
// start listening for clients.
TcpListener^ tcpListener = gcnew TcpListener(IPAddress::Any, 11000);
tcpListener->Start();
Console::WriteLine("Waiting for a connection....");
// Accept the pending client connection.
TcpClient^ tcpClient = tcpListener->AcceptTcpClient();
Console::WriteLine("Connection accepted.");
// Get the stream to write the message
// that will be sent to the client.
NetworkStream^ networkStream = tcpClient->GetStream();
String^ responseString = "Hello.";
// Set the write timeout to 10 millseconds.
networkStream->WriteTimeout = 10;
// Convert the message to a byte array and sent it to the client.
array<Byte>^ sendBytes = Encoding::UTF8->GetBytes(responseString);
networkStream->Write(sendBytes, 0, sendBytes->Length);
Console::WriteLine("Message Sent.");
// Close the connection to the client.
tcpClient->Close();
// Stop listening for incoming connections
// and close the server.
tcpListener->Stop();
// Dispose allocated resources.
delete networkStream;
delete tcpClient;
}
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Examples.System.Net
{
public class TCPListenerExample
{
public static void Main()
{
// Create the server side connection and
// start listening for clients.
TcpListener tcpListener = new TcpListener(IPAddress.Any,11000);
tcpListener.Start();
Console.WriteLine("Waiting for a connection....");
// Accept the pending client connection.
using TcpClient tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine("Connection accepted.");
// Get the stream to write the message
// that will be sent to the client.
using NetworkStream networkStream = tcpClient.GetStream();
string responseString = "Hello.";
// Set the write timeout to 10 millseconds.
networkStream.WriteTimeout = 10;
// Convert the message to a byte array and sent it to the client.
Byte[] sendBytes = Encoding.UTF8.GetBytes(responseString);
networkStream.Write(sendBytes, 0, sendBytes.Length);
Console.WriteLine("Message Sent.");
// Close the connection to the client.
tcpClient.Close();
// Stop listening for incoming connections
// and close the server.
tcpListener.Stop();
}
}
}
Keterangan
Jika operasi tulis tidak selesai dalam waktu yang ditentukan oleh properti ini, operasi tulis akan melempar IOException.
Catatan
Properti ini hanya memengaruhi operasi penulisan sinkron yang dilakukan dengan memanggil Write metode . Properti ini tidak memengaruhi penulisan asinkron yang dilakukan dengan memanggil BeginWrite metode atau WriteAsync .