我有一个基于 TCP 套接字的客户端服务器系统。
一切正常,但是当网络从客户端断开连接并再次重新连接时,我会自动 SocketError.ConnectionReset
从客户端发送,并且关于此命令,套接字在服务器端关闭。这也可以。
但是当我查看客户端时,它显示套接字仍与服务器连接。 (关于socket仍然与服务器连接【不是每次都发生】,有时显示断开,有时显示连接)
Does it make sense that "server get a
SocketError.ConnectionReset
from client end but client is still connected"?
所以我想知道 SocketError.ConnectionReset
的可能原因是什么以及如何处理我提到的此类问题?
我再说一遍,在正常环境下一切正常(例如,如果我退出客户端,它会断开与服务器相同的套接字)
提前致谢。
编辑:
这是客户端的代码。实际上它是一个计时器,在程序生命周期中每 3 秒计时一次,并检查 Socket 是否已连接,如果断开连接,则它会尝试通过新的套接字实例重新连接
private void timerSocket_Tick(object sender, EventArgs e)
{
try
{
if (sck == null || !sck.Connected)
{
ConnectToServer();
}
}
catch (Exception ex)
{
RPLog.WriteDebugLog("Exception occcured at: "+ System.Reflection.MethodBase.GetCurrentMethod().ToString()+"Message: "+ex.Message);
}
}
In normal situation (without network disconnect/reconnect) if TCP server get a SocketError.ConnectionReset form any client, in the client side i see clients socket is disconnected and it tries to reconnect it again through the code shown. but when situation happen explained earlier, server gets a SocketError.ConnectionReset but client shows it still connected. though the TCP server shows the reset command is send form the exact client side.
最佳答案
有多种原因,但最常见的是您写入了一个已经关闭但在另一端的连接。换句话说,应用程序协议(protocol)错误。当它发生时你别无选择,只能关闭套接字,它已经死了。但是,您可以解决根本原因。
关于c# - TCP Socket中SocketError.ConnectionReset的可能原因有哪些,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17923191/