I was looking at failing tests on FreeBSD and I spotted the following test in DatagramPacketTest.java:
public void test_getPort() throws IOException { DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress.getLocalHost(), 1000); assertEquals("Incorrect port returned", 1000, dp.getPort()); final InetAddress localhost = InetAddress.getLocalHost(); DatagramSocket socket = new DatagramSocket(0, localhost); final int port = socket.getLocalPort(); final Object lock = new Object(); Thread thread = new Thread(new Runnable() { public void run() { DatagramSocket socket = null; try { socket = new DatagramSocket(0, localhost); synchronized (lock) { started = true; lock.notifyAll(); } socket.setSoTimeout(3000); DatagramPacket packet = new DatagramPacket(new byte[256], 256); socket.receive(packet); socket.send(packet); socket.close(); } catch (IOException e) { System.out.println("thread exception: " + e); if (socket != null) socket.close(); } } }); thread.start(); socket.setSoTimeout(3000); DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4, 5, 6 }, 6, localhost, port); synchronized (lock) { while (!started) { try { lock.wait(); } catch (InterruptedException e) { } } } socket.send(packet); socket.receive(packet); socket.close(); assertTrue("datagram received wrong port: " + packet.getPort(), packet .getPort() == port); } Notice that main threads socket is being used to send to itself - not to the other socket which is left waiting until timeout for a packet from itself that it hasn't sent yet! I think this can be more simply written as: public void test_getPort() throws IOException { DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress.getLocalHost(), 1000); assertEquals("Incorrect port returned", 1000, dp.getPort()); final InetAddress localhost = InetAddress.getLocalHost(); DatagramSocket socket = new DatagramSocket(0, localhost); final int port = socket.getLocalPort(); socket.setSoTimeout(3000); DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4, 5, 6 }, 6, localhost, port); socket.send(packet); socket.receive(packet); socket.close(); assertTrue("datagram received wrong port: " + packet.getPort(), packet .getPort() == port); } Does that make sense or am I missing something? Sadly they both fail on FreeBSD. Regards, Mark.