void Loop() { TcpListener listener = null; if (_runMode == RunMode.None) { return; } if (_runMode == RunMode.Bind) { listener = new TcpListener(_port); listener.Start();//待ち受け開始 } while (_life) { Stream ns = null; TcpClient tcp = null; if (_runMode == RunMode.Bind) { if (!listener.Pending()) //接続が無い場合は、処理なし { Thread.Sleep(100); continue; } tcp = listener.AcceptTcpClient(); ns = tcp.GetStream(); } else { ns = _substitute.Accept(); if (ns == null) { Thread.Sleep(100); continue; } } _log.Set(string.Format("ns!=null")); try { var buf = new byte[2048];//リクエストは2048バイト程度で収まるだろうとキメうちする var size = 0; int count = 0; while (size <= 0) { size = ns.Read(buf, 0, buf.Length); _log.Set(string.Format("count={0} size={1}", count, size)); if (size == 0) { Thread.Sleep(100); count++; } if (count > 3) //タイムアウト処理 { _log.Set(string.Format("goto end count={0}", count)); ns.Write(new byte[0], 0, 0);//FIN/ACK goto end; } } string request = Encoding.ASCII.GetString(buf, 0, size); Job(request, ns); } catch { } end: ns.Close(); if (tcp != null) { tcp.Close(); } } if (listener != null) { listener.Stop(); } }