NetworkStream.WriteTimeout Właściwość

Definicja

Pobiera lub ustawia czas, przez który operacja zapisu blokuje oczekiwanie na dane.

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

Wartość właściwości

Element Int32 określający ilość czasu w milisekundach, który upłynął, zanim operacja zapisu zakończy się niepowodzeniem. Wartość domyślna , określa, Infiniteże operacja zapisu nie przekracza limitu czasu.

Wyjątki

Określona wartość jest mniejsza lub równa zero i nie Infinitejest równa .

Przykłady

Poniższy przykład kodu ustawia limit czasu zapisu dla strumienia sieciowego na 10 milisekund.

#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();
        }
    }
}

Uwagi

Jeśli operacja zapisu nie zostanie ukończona w czasie określonym przez tę właściwość, operacja zapisu zgłasza błąd IOException.

Uwaga

Ta właściwość ma wpływ tylko na synchroniczne operacje zapisu wykonywane przez wywołanie Write metody . Ta właściwość nie ma wpływu na asynchroniczne zapisy wykonywane przez wywołanie BeginWrite metody lub WriteAsync .

Dotyczy