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

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

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ý stream 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 operace zápisu není dokončena v době určené touto vlastností, vyvolá operace zápisu .IOException

Note

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 nebo WriteAsync metodou.

Platí pro