NetworkStream.ReadTimeout Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the amount of time that a read operation blocks waiting for data.
public:
virtual property int ReadTimeout { int get(); void set(int value); };
public override int ReadTimeout { get; set; }
member this.ReadTimeout : int with get, set
Public Overrides Property ReadTimeout As Integer
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.
The value specified is less than or equal to zero and is not Infinite.
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();
If the read operation does not complete within the time specified by this property, the read operation throws an IOException.
Note
This property affects only synchronous reads performed by calling the Read method. This property does not affect asynchronous reads performed by calling the BeginRead or ReadAsync method.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.3, 1.4, 1.6, 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: