デュアルロール アプリ (別のマシンで実行されている "サーバー" としての役割) を閉じると、実際にはシャットダウンに失敗します (アプリを閉じた後もタスク マネージャーの "プロセス" リストに残っています)。
TcpListener クラスがまだリッスンしているために「ライブ」のままなのか、アプリが実際にシャットダウンしていないために TcpListener クラスがまだリッスンしているのかはわかりません。
そこで、次の 2 つのコードを追加しました。
1) ここの最後の行 (listener.Stop()):
static void Server()
{
TcpListener listener = new TcpListener(IPAddress.Any, 51111);
listener.Start();
var shouldExit = false;
while (!shouldExit)
using (TcpClient c = listener.AcceptTcpClient())
{
using (NetworkStream n = c.GetStream())
{
string msg = new BinaryReader(n).ReadString();
if (msg == "exit")
// Client told us to exit...
shouldExit = true;
BinaryWriter w = new BinaryWriter(n);
w.Write(msg + " back atcha!");
w.Flush(); // Must call Flush because we're not disposing the writer.
}
}
//listener.EndAcceptTcpClient; //unnecessary because AcceptTcpClient() is wrapped in a "using"?
listener.Stop();
}
...と:
2)
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Close(); // The app/process is staying "live" even after shutting down the app...maybe this will help?
}
// but on closing the app, I got: "System.StackOverflowException was unhandled" here...?
Close を呼び出すときにスタック オーバーフローが発生するのはなぜですか?