Sdílet prostřednictvím


NetworkStream.WriteTimeout Vlastnost

Definice

Získá nebo nastaví dobu, po kterou operace zápisu blokuje čekání na 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

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