NetworkStream.ReadTimeout Property
.NET Framework 4
Gets or sets the amount of time that a read operation blocks waiting for data.
Assembly: System (in System.dll)
| Exception | Condition |
|---|---|
| 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.
The following code example sets the read time-out for a network stream to 10 milliseconds.
// Create a client that will connect to a // server listening on the contosoServer 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.ReadTimeout = 10; // Read the server message into a byte buffer. byte[] bytes = new byte[1024]; networkStream.Read(bytes, 0, 1024); //Convert the server's message into a string and display it. string data = Encoding.UTF8.GetString(bytes); Console.WriteLine("Server sent message: {0}", data); networkStream.Close(); tcpClient.Close();
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note