Hello Stack Overflow community,
I’m encountering an issue while working on a project involving Java NIO Datagram Channels. My goal is to open two sockets on the same port, one using IPv4 and the other using IPv6. However, every time I attempt to do so, I encounter the error message “port already in use” when trying to open the second socket. Interestingly, if one socket is opened with IPv4, attempting to open the second socket with IPv6 triggers the above-mentioned error, and vice versa.
Here’s a snippet of the relevant code:
// Opening IPv4 socket
DatagramChannel ipv4Channel = DatagramChannel.open(StandardProtocolFamily.INET);
ipv4Channel.bind(new InetSocketAddress(port));
// Opening IPv6 socket
DatagramChannel ipv6Channel = DatagramChannel.open(StandardProtocolFamily.INET6);
ipv6Channel.bind(new InetSocketAddress(port));
Is there a specific configuration i need to set to allow opening multiple sockets on the same port, each with different IP versions, using Java NIO DatagramChannels?
Any insights or suggestions would be greatly appreciated. Thank you!
I’ve ensured that the port I’m attempting to bind to is available and not in use by any other process.
Harshal Goyal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.