LoginSignup
0
0

More than 5 years have passed since last update.

Visual Studio | WPF > TCP > 応答がない場合のエラーのキャッチ > HResult == -2146232800

Posted at
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

概要

TCPクライアントソフトにて、下記の処理を行う。

  1. サーバーに接続
  2. コマンドを送信
  3. 応答を受信
  4. サーバーと切断

3の処理において、応答がない場合のエラー処理を検討した。

エラー処理

以下のような処理となるようだ。

        private void ReceiveData(NetworkStream netStream)
        {
            // 1. rcvdは<CR><LF>付き
            byte[] ReceiveBytes = new byte[1024];

            string rcvd = string.Empty;
            try
            {
                int BytesReceived = netStream.Read(ReceiveBytes, 0, ReceiveBytes.Length);
                rcvd = Encoding.ASCII.GetString(ReceiveBytes, 0, BytesReceived);
            }
            catch(IOException exc)
            {
                if (exc.HResult == -2146232800) // Timeout
                {
                    // TODO: エラー処理
                    // MessageBox.Show("応答がありません");
                }
            }
            // 正常受信処理 (後略)
  • IOException excでエラーをとらえる
  • HResultが-2146232800の場合は受信失敗

-2146232800はマジックナンバーでない方がいいが、具体的に定義している情報は見つかっていない。

関連

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0