What should I do to completely close the tcpClient connection with mcu?(我应该怎么做才能完全关闭与 mcu 的 tcpClient 连接?)
问题描述
我现在正在处理与 ESP32 中运行的 tcp 服务器的 tcp 套接字连接.它适用于通信,但我未能关闭连接.在搜索关闭/重置 tcpClient 的解决方案后,似乎关闭 tcpClient 的正确方法应该是:
tcpClient.GetStream().Close();tcpCLient.Close();
如果在关闭 tcpClient 之前关闭应用程序,它会立即发送 RST.
这样它就可以立即关闭服务器中的连接.
测试了不同的命令组合,发现下面的代码确实可以立即关闭连接,但大约40秒后会有另一个RST.
tcpClient.Client.Close();tcpClient.Close();
不要调用 tcpClient.GetStream().Close();!!!会导致RST延迟.
我不知道这样关闭连接有没有影响,但这是我真正可以立即关闭连接的唯一方法.
I'm now working on a tcp socket connection to tcp server running in ESP32. It works fine for the communication, but I failed to close the connection. After searching for the solution on close/reset tcpClient, it seems that the proper way to close a tcpClient should be:
tcpClient.GetStream().Close();
tcpCLient.Close();
The example in msdn also use this method.
But unforunately, it cannot really close the connection. As checked in the mcu, the connection has not been closed. And it will not be closed even I close the application later. In this case, the mcu's tcp connection cannot be released, it cannot receive other connections. So, I think this should not be a proper solution to close the tcpClient.
If I did not execute above statement, and close the application directly, it can close the connection successfully. And the mcu's tcp connection is released. It seems that the there has something been done in application close which can really close the connection.
In some situation, I need to close the connection after some operation, and reconnect it. So, I cannot rely on the application close.
I have tried all the following methods in some different combination, but none of them can successfully release the tcpClient connection:
tcpClient.Close();
tcpClient.Client.Close();
tcpClient.GetStream().Close();
tcpClient.Client.Disconnect(true);
tcpClient.Client.Disconnect(false);
tcpClient.Dispose();
tcpClient.Client.Dispose();
tcpCLient = null;
Maybe it should be done with some of above commands in a proper sequence.
Does anyone know how I cannot close the connection by myself.
Thanks in advance.
After studying the network packet using WireShark, it was found that the problem is due to the delay of sending RST with the code as suggested in MSDN:
tcpClient.GetStream().Close();
tcpCLient.Close();
There has no different even adding
tcpClient.LingerState = new LingerOptions(true, 0);
Because it will send FIN immediately, but not the RST after the Close method, it will be sent around 2 minutes later. Unfortunately, it won't be sent even you close the application after tcpClient close is issued.
If you close the application before closing the tcpClient, it will send the RST immedicately.
So that it can close the connection in server immediately.
After testing different combination of command, it was found that the following code can really close the connection immediately, but there will have another RST around 40 seconds later.
tcpClient.Client.Close();
tcpClient.Close();
Don't call tcpClient.GetStream().Close(); !!! It will cause the delay of RST.
I don't know if there has any impact closing the connection in this way, but this is the only way I can really close the connection immediately.
这篇关于我应该怎么做才能完全关闭与 mcu 的 tcpClient 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我应该怎么做才能完全关闭与 mcu 的 tcpClient 连接?
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30