diff --git a/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java b/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java index afbc664760..d79ae6caa1 100644 --- a/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java +++ b/testsuite/src/main/java/io/netty/testsuite/transport/socket/DatagramUnicastTest.java @@ -21,6 +21,7 @@ import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; @@ -50,6 +51,21 @@ public class DatagramUnicastTest extends AbstractDatagramTest { NONE, DUP, SLICE, READ_ONLY } + @Test + public void testBindWithPortOnly() throws Throwable { + run(); + } + + public void testBindWithPortOnly(Bootstrap sb, Bootstrap cb) throws Throwable { + Channel channel = null; + try { + cb.handler(new ChannelHandlerAdapter() { }); + channel = cb.bind(0).sync().channel(); + } finally { + closeChannel(channel); + } + } + @Test public void testSimpleSendDirectByteBuf() throws Throwable { run(); diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java index 0c0d0f81d5..f355770edf 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/EpollDatagramChannel.java @@ -276,8 +276,10 @@ public final class EpollDatagramChannel extends AbstractEpollChannel implements if (localAddress instanceof InetSocketAddress) { InetSocketAddress socketAddress = (InetSocketAddress) localAddress; if (socketAddress.getAddress().isAnyLocalAddress() && - socketAddress.getAddress() instanceof Inet4Address && Socket.isIPv6Preferred()) { - localAddress = new InetSocketAddress(LinuxSocket.INET6_ANY, socketAddress.getPort()); + socketAddress.getAddress() instanceof Inet4Address) { + if (socket.family() == InternetProtocolFamily.IPv6) { + localAddress = new InetSocketAddress(LinuxSocket.INET6_ANY, socketAddress.getPort()); + } } } super.doBind(localAddress); diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/LinuxSocket.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/LinuxSocket.java index db2d2469c8..96d3da441e 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/LinuxSocket.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/LinuxSocket.java @@ -45,7 +45,7 @@ final class LinuxSocket extends Socket { super(fd); } - private InternetProtocolFamily family() { + InternetProtocolFamily family() { return ipv6 ? InternetProtocolFamily.IPv6 : InternetProtocolFamily.IPv4; }