NetworkStream.WriteTimeout プロパティ

定義

書き込み操作がデータを待機する時間を取得または設定します。Gets or sets the amount of time that a write operation blocks waiting for data.

public:
 virtual property int WriteTimeout { int get(); void set(int value); };
public override int WriteTimeout { get; set; }
member this.WriteTimeout : int with get, set
Public Overrides Property WriteTimeout As Integer

プロパティ値

Int32

書き込み操作が失敗するまでの経過時間 (ミリ秒単位) を指定する Int32A Int32 that specifies the amount of time, in milliseconds, that will elapse before a write operation fails. 既定値は Infinite で、書き込み操作がタイムアウトしないことを示します。The default value, Infinite, specifies that the write operation does not time out.

例外

指定した値が 0 以下のため Infinite ではありません。The value specified is less than or equal to zero and is not Infinite.

次のコード例では、ネットワークストリームの書き込みタイムアウトを10ミリ秒に設定します。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();
}
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();
        }
    }
}

注釈

書き込み操作がこのプロパティで指定された時間内に完了しない場合、書き込み操作はをスロー IOException します。If the write operation does not complete within the time specified by this property, the write operation throws a IOException.

注意

このプロパティは、メソッドを呼び出すことによって実行される同期書き込み操作にのみ影響 Write します。This property affects only synchronous write operations performed by calling the Write method. このプロパティは、メソッドを呼び出すことによって実行される非同期書き込みには影響しません BeginWriteThis property does not affect asynchronous writes performed by calling the BeginWrite method.

適用対象