NetworkStream.WriteTimeout プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
書き込み操作がデータの待機をブロックする時間を取得または設定します。
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 。 既定値 Infiniteは、書き込み操作がタイムアウトしないことを指定します。
例外
指定された値は 0 以下であり、 Infiniteされません。
例
次のコード例では、ネットワーク ストリームの書き込みタイムアウトを 10 ミリ秒に設定します。
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.
using TcpClient tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine("Connection accepted.");
// Get the stream to write the message
// that will be sent to the client.
using 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をスローします。
Note
このプロパティは、 Write メソッドを呼び出すことによって実行される同期書き込み操作にのみ影響します。 このプロパティは、 BeginWrite または WriteAsync メソッドを呼び出すことによって実行される非同期書き込みに影響しません。