我有一个客户端和一台服务器,它可以在我的计算机上本地运行。虽然当我部署它时,所有客户端都被拒绝,并出现以下错误:
System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at SlientServer_v2_1.ConnectionHandle.SendRequestInformation(Client target, Int32 messageId) in c:\Users\Ya\Dropbox\Private\Visual Studio\Projects\SlientServer_v2_1\SlientServer_v2_1\ConnectionHandle.cs:line 136
以下是我发送数据的代码:
public static void SendRequestInformation(Client target, int messageId)
{
NetworkStream stream = target.getClientSocket().GetStream();
byte[] byteData = null;
switch (messageId)
{
case 0:
byteData = Encoding.ASCII.GetBytes("ping");
break;
case 1:
byteData = Encoding.ASCII.GetBytes("hand");
break;
case 3:
byteData = Encoding.ASCII.GetBytes("osif");
break;
case 4:
byteData = Encoding.ASCII.GetBytes("idle");
break;
case 5:
byteData = Encoding.ASCII.GetBytes("cpus");
break;
case 6:
byteData = Encoding.ASCII.GetBytes("mine");
break;
case 2:
byteData = Encoding.ASCII.GetBytes("rein");
break;
case 7:
byteData = Encoding.ASCII.GetBytes("bcac");
break;
}
try
{
byte[] bufferedToSend = new byte[byteData.Length + 4];
byte[] lengthOfSend = BitConverter.GetBytes(byteData.Length);
Array.Copy(lengthOfSend, bufferedToSend, 4);
byteData.CopyTo(bufferedToSend, 4);
--- This is 136 ---- stream.Write(bufferedToSend, 0, bufferedToSend.Length);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
ClientHandle.RemoveFromClientPool(target, "Unable to reach Client");
}
}
我看了下面的线索,没有骰子An existing connection was forcibly closed by the remote host
第一个消息发送可能是案例1.