c# - TCP Socket中SocketError.ConnectionReset的可能原因有哪些

标签 c# sockets

我有一个基于 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/

相关文章:

c# - 在没有 dll 的情况下在 2 个项目之间共享代码

c# - List<T>.ForEach 与自定义 IEnumerable<T> 扩展

c# - 我如何告诉 Resharper ICollectionView 包含的类型?

c# - 脚本不在代码后面工作

c# - 反序列化 .NET 中已更改的类的策略

Node.js 在 Express 框架中使用 setKeepAlive()

php - HTML5游戏<->服务器

sockets - 如何使用 std.socket 和 std.socketstream? (D 编程语言 2.x)

c++ - 如何在QT中接收正确的UDP数据包?

c++ - 具有 FIONREAD 返回值的 ioctl