This documentation is archived and is not being maintained.

NetworkStream.ReadTimeout Property

Note: This property is new in the .NET Framework version 2.0.

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

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

public override int ReadTimeout { get; set; }
/** @property */
public int get_ReadTimeout ()

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

public override function get ReadTimeout () : int

public override function set ReadTimeout (value : int)

Property Value

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

Exception typeCondition

ArgumentOutOfRangeException

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

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

NoteNote

This property affects only synchronous reads performed by calling the Read method. This property does not affect asynchronous reads performed by calling the BeginRead method.

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

import System.*;
import System.Text.*;
import System.Net.*;
import System.Net.Sockets.*;

public class TCPClientExample
{
    public static void main(String[] args)
    {
        // Create a client that will connect to a 
        // server listening on the contoso1 computer
        // at port 11000.
        TcpClient tcpClient = new TcpClient();
        tcpClient.Connect("contosoServer", 11000);
        // Get the stream used to read the message sent by the server.
        NetworkStream networkStream = tcpClient.GetStream();
        // Set a 10 millisecond timeout for reading.
        networkStream.set_ReadTimeout(10);
        // Read the server message into a byte buffer.
        ubyte bytes[] = new ubyte[1024];
        networkStream.Read(bytes, 0, 1024);
        //Convert the server's message into a string and display it.
        String data = Encoding.get_UTF8().GetString(bytes);
        Console.WriteLine("Server sent message: {0}", data);
        tcpClient.Close();
    } //main
} //TCPClientExample

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
Show: