NetworkStream.WriteTimeout Proprietà

Definizione

Ottiene o imposta l'intervallo di tempo per il quale un'operazione di scrittura si blocca in attesa dei dati.

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

Valore della proprietà

Valore Int32 che specifica l'intervallo di tempo, in millisecondi, che deve trascorrere prima che l'operazione di scrittura generi un errore. Il valore predefinito, Infinite, specifica che non è previsto un timeout per l'operazione di scrittura.

Eccezioni

Il valore specificato è minore o uguale a zero e non rappresenta il campo Infinite.

Esempio

L'esempio di codice seguente imposta il timeout di scrittura per un flusso di rete su 10 millisecondi.

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

Commenti

Se l'operazione di scrittura non viene completata entro il tempo specificato da questa proprietà, l'operazione di scrittura genera un'eccezione IOException.

Nota

Questa proprietà influisce solo sulle operazioni di scrittura sincrone eseguite chiamando il Write metodo . Questa proprietà non influisce sulle scritture asincrone eseguite chiamando il BeginWrite metodo o WriteAsync .

Si applica a