NetworkStream.WriteTimeout Vlastnost

Definice

Získá nebo nastaví dobu, po kterou operace zápisu blokuje čekání na data.

public override int WriteTimeout { get; set; }

Hodnota vlastnosti

Určuje Int32 dobu v milisekundách, která uplyne před selháním operace zápisu. Výchozí hodnota určuje, Infiniteže časový limit operace zápisu nevypadá.

Výjimky

Zadaná hodnota je menší nebo rovna nule a není Infinite.

Příklady

Následující příklad kódu nastaví časový limit zápisu pro síťový datový proud na 10 milisekund.

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

Poznámky

Pokud se operace zápisu nedokončuje v čase určeném touto vlastností, vyvolá operace zápisu .IOException

Poznámka

Tato vlastnost ovlivňuje pouze synchronní operace zápisu prováděné voláním Write metody . Tato vlastnost nemá vliv na asynchronní zápisy prováděné voláním BeginWrite metody nebo WriteAsync .

Platí pro

Produkt Verze
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1