简体   繁体   中英

How to get Ephemeral port for UDP in Erlang?

I'm trying to find a way to open an ephemeral port ( https://en.wikipedia.org/wiki/Ephemeral_port basic one without having to provide a specific port number, and from the ephemeral range of ports). This is for client requests via UDP protocol. I know how to open a specific port for UDP communications via:

{ok,Socket} = gen_udp:open(8000).

But prefer not to have to know an available port number ahead of time and just have the system provide one if this is even possible. Any suggestion or thought of how best to do this in Erlang?

Do it the same way you would in C: specify port 0.

1> {ok, S1} = gen_udp:open(0).
{ok,#Port<0.541>}
2> {ok, S2} = gen_udp:open(0).
{ok,#Port<0.547>}
3> {ok, S3} = gen_udp:open(0).
{ok,#Port<0.548>}
4> inet:port(S1).
{ok,55398}
5> inet:port(S2).
{ok,44963}
6> inet:port(S3).
{ok,58993}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM