[I'm not sure if I should post this to net-dev, nio-dev or something else, so I 
posted to both.]

I have a trivial DatagramChannel program that fails on Windows if run with 
Java.net.preferIPv6Addresses=true.  The exception and code are included below.

The problem is that when the bind parameter is null, DatagramChannelImpl calls 
new InetSocketAddress(0), but that returns an AF_INET6 address when 
preferIPv6Addresses=true.  Even when the caller passes a SocketAddress to bind, 
it's hard to get it right if you want to specify the any address or the 
loopback address.  The methods to get these addresses all change their behavior 
based on the setting of preferIPv6Addresses.

I'd be inclined to believe it's a user error to specify an address family on 
open and then pass a different address family on bind, except that the TCK does 
exactly this.  It opens with AF_INET and then passes any and loopback addresses 
in AF_INET6 format.

I also wonder why it's even an option to specify an explicit address family.   
According to the Java Docs this is the only place in the entire JDK that 
doesn't just infer the family from an address.  There's no other references to 
ProtocolFamily and StandardProtocolFamily.


java.net.SocketException: Address family not supported by protocol family: bind
        at sun.nio.ch.Net.bind0(Native Method)
        at sun.nio.ch.Net.bind(Net.java:344)
        at sun.nio.ch.DatagramChannelImpl.bind(DatagramChannelImpl.java:672)
        at DatagramChannelTest.main(DatagramChannelTest.java:14)

import java.io.*;
import java.net.*;
import java.nio.channels.*;
import static java.lang.System.out;

public class DatagramChannelTest
{
    public static void main(String[] args)
    {
        DatagramChannel dgc;
        try
        {
            dgc = DatagramChannel.open( StandardProtocolFamily.INET );
            dgc.bind( null );
            out.println("dgc:" + dgc.getLocalAddress() );
        } catch( IOException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

This was run on Windows 7 with Java 7:
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)




Tom Salter  |  Software Engineer  |  Java & Middleware Development

Unisys  |  2476 Swedesford Road  |  Malvern, PA  19355   |  610-648-2568 |  
N385-2568


[cid:image001.gif@01CD1BB2.56799670]


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.



<<inline: image001.gif>>

Reply via email to