TcpClient/Listener: Do I have to call Sockets.Close when my program ends?

一个人想着一个人 提交于 2019-12-13 07:00:56

问题


I'm using the TcpClient and TcpListener classes to establish a connection between two computers. I've read documentation and questions online, but can't seem to find the answer to this question:

Is it necessary to call Sockets.Close() before the program ends, or will VB close all sockets which were opened by the program for me before it ends? In addition, what if the program crashes?

From what I've seen, the suggested implementation of the TcpClient and TcpListener classes suggest closing the sockets in the Finally block so that they are closed even if any exceptions occur.

However, this finally block is not necessarily called if I just simply close the program by calling End, and I'm not entirely sure if it is called if a crash outside of the try block or on another thread occurs.

来源:https://stackoverflow.com/questions/38946641/tcpclient-listener-do-i-have-to-call-sockets-close-when-my-program-ends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!