我该怎么办?关闭套接字?我看不到问题,谷歌没有帮助 什么会导致此错误?据我所知这是系统类型异常。 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