开发者

Erlang open socket or know

How can开发者_运维问答 i know socket open or not with erlang?


Use erlang:port_info/1. It returns undefined if the socket is closed. For example:

Eshell V5.8.3  (abort with ^G)
1> {ok,S} = gen_tcp:connect("localhost", 8000, [binary,{active,false}]).
{ok,#Port}
2> erlang:port_info(S).
[{name,"tcp_inet"},
 {links,[]},
 {id,634},
 {connected,},
 {input,0},
 {output,0}]
3> gen_tcp:close(S).
ok
4> erlang:port_info(S).
undefined

But be careful about using this for defensive programming, as that's not the Erlang way. It's better to make your code assume the socket is open, and if it hits an error because it tries to use an already-closed socket, let it crash and let something else like a supervisor handle the error.


try this in your terminal when your app reaches the point at which you want to know


inet:i()


What kind of sockets are you talking about? My guess is that most socket functions will return an error tuple if the socket is not open.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜