回答編集履歴

3

ReadKey → ReadLine

2022/07/18 08:23

投稿

KOZ6.0
KOZ6.0

スコア2626

test CHANGED
@@ -5,6 +5,7 @@
5
5
  {
6
6
  using System;
7
7
  using System.Collections.Generic;
8
+ using System.IO;
8
9
  using System.Net;
9
10
  using System.Net.Sockets;
10
11
  using System.Threading;
@@ -34,7 +35,7 @@
34
35
  breidge.Stop(false);
35
36
 
36
37
  Console.WriteLine("Enter キーを押すと終了します。");
37
- Console.ReadKey();
38
+ Console.ReadLine();
38
39
  }
39
40
  }
40
41
 

2

CanRead が効かないケースがある

2022/07/18 08:04

投稿

KOZ6.0
KOZ6.0

スコア2626

test CHANGED
@@ -85,9 +85,15 @@
85
85
 
86
86
  private void OnAcceptTcpClient(IAsyncResult ar) {
87
87
  TcpClient client;
88
- if (!IsStoped) {
88
+ try {
89
89
  client = listener.EndAcceptTcpClient(ar);
90
- } else {
90
+ if (IsStoped) {
91
+ client.Close();
92
+ return;
93
+ }
94
+ } catch (IOException) {
95
+ return;
96
+ } catch (ObjectDisposedException) {
91
97
  return;
92
98
  }
93
99
  Console.WriteLine("Connect From {0}", client.Client.RemoteEndPoint);
@@ -244,13 +250,12 @@
244
250
  }
245
251
 
246
252
  private int EndRead(IAsyncResult ar) {
247
- int size;
253
+ int size = 0;
248
- if (reader.CanRead) {
254
+ try {
249
255
  size = reader.EndRead(ar);
250
- } else {
256
+ } catch (IOException) {
251
- return 0;
257
+ } catch (ObjectDisposedException) {
252
- }
258
+ }
253
-
254
259
  // size がゼロ以上なら正常
255
260
  if (size >= 0) {
256
261
  return size;

1

ダブった

2022/07/18 02:17

投稿

KOZ6.0
KOZ6.0

スコア2626

test CHANGED
@@ -1,8 +1,6 @@
1
1
  サンプルを書いてみました。何かあっても自己責任でお願いします。
2
2
 
3
3
  ```C#
4
- サンプルを書いてみました。何かあっても自己責任でお願いします。
5
-
6
4
  namespace ConsoleApp8
7
5
  {
8
6
  using System;