NetworkStream.WriteTimeout Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient ou définit la durée pendant laquelle une opération d’écriture bloque l’attente des données.
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
Valeur de propriété
Valeur Int32 qui spécifie la durée, en millisecondes, qui s’écoule avant l’échec d’une opération d’écriture. La valeur par défaut, Infinitespécifie que l’opération d’écriture n’expire pas.
Exceptions
La valeur spécifiée est inférieure ou égale à zéro et n’est pas Infinite.
Exemples
L’exemple de code suivant définit le délai d’attente d’écriture d’un flux réseau sur 10 millisecondes.
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();
}
}
}
Remarques
Si l’opération d’écriture ne se termine pas dans le délai spécifié par cette propriété, l’opération d’écriture lève un IOException.
Note
Cette propriété affecte uniquement les opérations d’écriture synchrones effectuées en appelant la Write méthode. Cette propriété n’affecte pas les écritures asynchrones effectuées en appelant la méthode ou WriteAsync l’appelBeginWrite.