如何以尾递归方式关闭Socket?
我创建一个udp客户端,需要每5秒发送一次消息,所以我写
start() ->
{ok, Sock} = gen_udp:open(0, []),
send(Sock).
send(Sock) ->
gen_udp:send(Sock, "127.0.0.1", 3211, "hello world"),
timer:sleep(5000),
send(Sock).
我想知道关闭套接字的好地方
I create one udp client, and need to send message every 5s, so i write
start() ->
{ok, Sock} = gen_udp:open(0, []),
send(Sock).
send(Sock) ->
gen_udp:send(Sock, "127.0.0.1", 3211, "hello world"),
timer:sleep(5000),
send(Sock).
I want to know a good place to close the socket
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的目标是每 5 秒发送一条消息,那么为什么要关闭套接字呢?如果您有一些逻辑来确定何时发送了足够的消息(例如,您对它们进行计数),那么这将是关闭套接字的地方。
下面是如何在长时间运行的进程中对消息进行计数的示例:
通过向上计数而不是向下计数,您可以在进程运行时通过简单地重新加载代码来更改此数字。
If your goal is to send a message every 5 seconds, then why would you want to close the socket? If you have some logic to determine when you have sent enough messages (you count them for example), then that would be the place to close the socket.
Here's an example of how you could count the messages in a long-running process:
By counting up to a given number, instead of down, you can change this number while the process is running by simply reloading the code.