我应该怎么做才能完全关闭与 mcu 的 tcpClient 连接?

What should I do to completely close the tcpClient connection with mcu?(我应该怎么做才能完全关闭与 mcu 的 tcpClient 连接?)
本文介绍了我应该怎么做才能完全关闭与 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 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)