Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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:
property int WriteTimeout {
	virtual int get() override;
	virtual void set(int value) override;
}

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.

Exception Condition
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.

System_CAPS_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.

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

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

.NET Framework
Available since 2.0
Return to top
Show:
© 2017 Microsoft