I guess my question is rather straight forward.

I am wondering if there is any point in using gen_tcp:close/1 on a socket owned by a process that is about to terminate. The connection is closed automatically and it returns {tcp_closed, Socket} to the connected process on the other side, just like when close is called, so I'm guessing the effect is the same and that close is only needed when the connection has to be closed some time before termination.

Is this correct or are there any reasons why I should try to use close on the socket even when the process is about to terminate?

Is there any difference for the listening socket or when the connected process on the other side is not an Erlang process?

有帮助吗?

解决方案

Not necessary since, as you've observed, this happens automatically on exit of the owning process.

I usually make the effort to do it anyway in case I change my code down the road in a way that doesn't have the process exiting at that same point. For example, open a socket, read data, then do a giant calc. Would leave the socket open for a long time if the calc was big.

Pretty sure I've ever actually run in to that situation, but I'm sure I won't if I always explicitly close my socket at the earliest point that makes sense.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top