see test below
--------------------
import java.net.InetAddress;
import java.net.DatagramSocket;
import java.net.DatagramPacket;
public class Test2 {
public static void main( String[] args ){
try {
InetAddress lh = InetAddress.getLocalHost();
DatagramSocket s = new DatagramSocket(0, lh);
try {
DatagramSocket ss = new DatagramSocket(0, lh);
try {
s.connect(ss.getLocalAddress(), ss.getLocalPort());
s.disconnect();
System.out.println( String.format( "DatagramSocket: (%s, %s), bound = %s, closed = %s, connected = %s",
s.getLocalAddress(), s.getLocalPort(),
s.isBound(), s.isClosed(), s.isConnected() ) );
byte[] data = { 0, 1, 2 };
DatagramPacket p = new DatagramPacket(data, data.length, s.getLocalAddress(), s.getLocalPort());
s.setSoTimeout( 10000 );
s.send( p );
s.receive( p );
System.out.println( "OK" );
} finally {
ss.close();
}
} finally {
s.close();
}
} catch( Exception x ){
x.printStackTrace( System.out );
}
}
}
--------------------
output:
DatagramSocket: (/10.16.106.217, 33411), bound = true, closed = false, connected = false
java.net.SocketTimeoutException: Receive timed out
at java.net.PlainDatagramSocketImpl.receive0(Native Method)
at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136)
at java.net.DatagramSocket.receive(DatagramSocket.java:712)
at Test2.main(Test2.java:25)
--------------------
This error appears only on linux OS.
It is interesting that changing order of DatagramSockets creation leads to normal test result.
JCK test
api/java_net/DatagramSocket/index.html#connect[DatagramSocket2016]
is failed