Name: rmT116609 Date: 01/16/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)
On Solaris, DatagramSocket.receive() returns when the thread is interrupted.
Since it has not actually received anything, the datagram packet that
you "received" is invalid - in particular, getAddress() returns null.
This means if you try:
ds.receive(dp);
System.out.println("received packet from " + dp.getAddress().getHostAddress());
and then interrupt the thread (e.g. to terminate it)
you will get a null pointer exception.
The preferred behavior on interrupt is to throw an InterruptedIOException.
Source code follows, with results for Win32 and Solaris.
Note the behavior is quite different for Win32 than for Solaris.
// SocketDemo.java
import java.net.*;
import java.io.*;
/**
* Class to demonstrate bug in DatagramSocket.receive()
*/
public class SocketDemo implements Runnable
{
public void run()
{
try
{
DatagramSocket ds = new DatagramSocket(20000);
ds.setSoTimeout(5000);
byte[] buffer = new byte[512];
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
ds.receive(dp);
System.out.println("receive returned, dp address is " + dp.getAddress());
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
Thread t = new Thread(new SocketDemo());
t.start();
try { Thread.sleep(2500); }
catch (Exception e) {}
t.interrupt();
}
}
/*
Output on NT4.0 with JDK 1.3.0:
java.io.InterruptedIOException: Receive timed out
at java.net.PlainDatagramSocketImpl.receive(Native Method)
at java.net.DatagramSocket.receive(Unknown Source)
at SocketDemo.run(SocketDemo.java)
at java.lang.Thread.run(Unknown Source)
This occurs after the 5 second socket timeout
Output on Solaris with JDK 1.3.0:
receive returned, dp address is null
This occurs after the thread is interrupted
*/
(Review ID: 115252)
======================================================================
- duplicates
-
JDK-4379156 JDK 1.3/Solaris: Interrupted I/O broken
-
- Resolved
-