Assigning ephemeral port before binding.

61 views
Skip to first unread message

Tim Fox

unread,
Dec 1, 2020, 11:11:09 AM12/1/20
to Netty discussions
Hi folks,

When using Netty I know I can specify local port as zero before binding and then after the bindfuture has completed I can read the actual port.

However, I need to know the ephemeral port before the server socket is bound and the system is ready to accept connections.

I can get my own ephemeral port manually before binding by opening a ServerSocket like this:

ServerSocket s = new ServerSocket(0);
int localPort = s.getLocalPort();

However I have to close that port before I can pass the result to Netty to bind the actual socket, but that means there's a race condition where something else can get the port assigned between it being closed and Netty creating its ServerSocket.

Any thoughts on how I can resolve this?


Norman Maurer

unread,
Dec 1, 2020, 11:15:00 AM12/1/20
to ne...@googlegroups.com
There is no way to know before you actual bound it as the system may re-use it in the meantime. 

--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netty/75f8769b-11c6-47b0-9028-1cf38580e68bn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages