NetworkStream.WriteTimeout Property (System.Net.Sockets)

Switch View :
ScriptFree
.NET Framework Class Library
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)
Syntax

Visual Basic
Public Overrides Property WriteTimeout As Integer
C#
public override int WriteTimeout { get; set; }
Visual C++
public:
virtual property int WriteTimeout {
	int get () override;
	void set (int value) override;
}
F#
abstract WriteTimeout : int with get, set
override WriteTimeout : int with get, set

Property Value

Type: System.Int32
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.
Exceptions

Exception Condition
ArgumentOutOfRangeException

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

Remarks

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

Note Note

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.

Examples

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

Visual Basic

Imports System
Imports System.Text
Imports System.Net
Imports System.Net.Sockets

Namespace Examples.System.Net
	Public Class TCPListenerExample
		Public Shared Sub Main()
			' Create the server side connection and 
			' start listening for clients.
			Dim tcpListener As New TcpListener(IPAddress.Any,11000)
			tcpListener.Start()
			Console.WriteLine("Waiting for a connection....")

			' Accept the pending client connection.
			Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
			Console.WriteLine("Connection accepted.")
			' Get the stream to write the message 
			' that will be sent to the client.
			Dim networkStream As NetworkStream = tcpClient.GetStream()
			Dim responseString As String = "Hello."
			' Set the write timeout to 10 millseconds.
			networkStream.WriteTimeout = 10
			' Convert the message to a byte array and sent it to the client.
			Dim sendBytes() As Byte = 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()
		End Sub
	End Class
End Namespace


C#

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


Visual C++

#using <System.dll>
using namespace System;
using namespace System::Text;
using namespace System::Net;
using namespace System::Net::Sockets;

int main()
{
    // Create the server side connection and
    // start listening for clients.
    TcpListener^ tcpListener = gcnew 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.
    array<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();
}


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

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.
See Also

Reference