Export (0) Print
Expand All

NetworkStream.WriteTimeout Property

Gets or sets the amount of time that a write operation blocks waiting for data.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

public override int WriteTimeout { get; set; }
/** @property */
public int get_WriteTimeout ()

/** @property */
public void set_WriteTimeout (int value)

public override function get WriteTimeout () : int

public override function set WriteTimeout (value : int)

Not applicable.

Property Value

A Int32 that specifies the amount of time, in milliseconds, that will elapse before a write operation fails. The default value, Infinite, specifies that the write operation does not time out.

Exception typeCondition

ArgumentOutOfRangeException

The value specified is less than or equal to zero and is not Infinite.

If the write operation does not complete within the time specified by this property, the write operation throws a IOException.

NoteNote:

This property affects only synchronous write operations performed by calling the Write method. This property does not affect asynchronous writes performed by calling the BeginWrite method.

Windows Mobile 2003 for Pocket PC, Windows Mobile 2003 for Smartphone, Windows CE Platform Note: In .NET Compact Framework applications, the WriteTimeout property is supported, but reserved for future use. Using this property will raise an InvalidOperationException, until it is available in a future release of the .NET Compact Framework.

The following code example sets the write time-out for a network stream to 10 milliseconds.

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft