NetworkStream.WriteTimeout Propiedad

Definición

Obtiene o establece la cantidad de tiempo que una operación de escritura se bloquea en espera de los datos.

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

Valor de propiedad

Objeto Int32 que especifica la cantidad de tiempo, en milisegundos, que pasará antes de que se produzca un error en la operación de escritura. El valor predeterminado, Infinite, especifica que no se agota el tiempo de espera de la operación de escritura.

Excepciones

El valor especificado es menor o igual que cero y distinto de Infinite.

Ejemplos

En el ejemplo de código siguiente se establece el tiempo de espera de escritura de una secuencia de red en 10 milisegundos.

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

Comentarios

Si la operación de escritura no se completa dentro del tiempo especificado por esta propiedad, la operación de escritura produce una IOExceptionexcepción .

Nota:

Esta propiedad solo afecta a las operaciones de escritura sincrónicas realizadas mediante una llamada al Write método . Esta propiedad no afecta a las escrituras asincrónicas realizadas mediante una llamada al BeginWrite método o WriteAsync .

Se aplica a