我該怎麼辦?關閉套接字?我看不到問題,谷歌沒有幫助 什麼會導致此錯誤?據我所知這是系統類型異常。 SocketError:現有連接被遠程主機強制關閉
Console.WriteLine("Connection Recieved");
while (true)
{
var handler = socket.Accept();
try
{
while (handler.Connected)
{
var x = new byte[200000];
try
{
handler.Receive(x);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException);
Console.WriteLine(ex.StackTrace);
}
var command = new string(Encoding.UTF8.GetChars(x.Where(t => t != 0).ToArray()));
if (string.IsNullOrEmpty(command))
{
break;
}
var data = (Dictionary<string, object>)JsonConvert.DeserializeObject(command, (typeof(Dictionary<string, object>)));
if (data["command"].ToString() != "getLog") continue;
var response = GetStatus();
handler.Send(Encoding.UTF8.GetBytes(response));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException);
Console.WriteLine(ex.StackTrace);
}
}
問題可能出在連接的另一端。很可能是未處理的異常。 – HebeleHododo
這主要發生在客戶端斷開連接時。 –
[現有連接被遠程主機強行關閉]的可能重複(http://stackoverflow.com/questions/2582036/an-existing-connection-was-forcibly-closed-by-the-remote-host) – HebeleHododo