package mil.navy.nps.testing; /** DatagramTestApplet.java A minimal applet designed to demonstrate a problem reading from datagram sockets. This works under Netscape 3.x, but not under Netscape 4.x; it throws an exception on receive(). The code executes until it does a receive(). It blocks there; once a packet actually arrives, a socket exception is thrown and execution stops. To duplicate the error, run the TestApplet in the browser from the HTML file. The applet will block attempting to read from the datagram socket. Then fire up the datagram sender on the same machine. The first packet that arrives will cause an exception to be thrown. I've provided a small datagram sender application to send packets. running under Netscape Navigator 3.x requires that you take out the references to privilegeManager. Under 3.x, packets from the same machine were considered kosher by the security manager, and no signed applets or privelige managers were required. The socket code is isolated in doSocketStuff(). The rest of the applet code is just overhead. Environment: Running under NT Workstation 4.0, Netscape Navigator 4.04, Java 1.1 extensions installed, and codebase principals turned on. */ import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; // import netscape.security.*; // for netscape-style signed applets public class DatagramTestApplet extends Applet implements Runnable { public DatagramTestApplet() { } public String getAppletInfo() { return "Name: AwtPduViewer\r\n" + "Author: Don McGregor\r\n" + "mcgredo@cs.nps.navy.mil"; } /** Applet initialization code */ public void init() { TextArea textArea; // scrolling text area to display output (lines, width) textArea = new TextArea(13,45); add(textArea); return; } public void run() { repaint(); } public void paint(Graphics g) { this.doSocketStuff(); } public void doSocketStuff() { DatagramSocket unicastDatagramSocket = null; DatagramPacket datagramPacket; byte dataBuffer[] = new byte[1500]; InetAddress destAddress; // PrivilegeManager privilegeManager = PrivilegeManager.getPrivilegeManager(); byte byteArray[]; try { destAddress = InetAddress.getLocalHost(); datagramPacket = new DatagramPacket(dataBuffer, dataBuffer.length); // @@@THIS IS NETSCAPE COMMUNICATOR SPECIFIC. It basically // switches on network access for everything under the sun, // so you can attach or be attached to random machines on // the internet. /* privilegeManager.enablePrivilege("UniversalMulticast"); privilegeManager.enablePrivilege("UniversalConnect"); privilegeManager.enablePrivilege("UniversalAccept"); privilegeManager.enablePrivilege("UniversalListen"); */ unicastDatagramSocket = new DatagramSocket(8004); System.out.println("created a socket correctly"); unicastDatagramSocket.receive(datagramPacket); System.out.println("Did a receive() correctly"); byteArray = datagramPacket.getData(); System.out.println(byteArray[0] + ", " + byteArray[1] + ", " + byteArray[2]); } catch (SocketException socketException) { System.out.println("socket exception error: " + socketException); throw new RuntimeException("Exception in DatagramTestApplet. Socket exception thrown."); } catch(UnknownHostException unkException) // for getLocalHost() call { System.out.println("Unknown host exception: " + unkException); throw new RuntimeException("Exception in DatagramTestApplet. Unable to look up localhost correctly"); } catch (IOException ioError) { System.out.println("IO exception error: " + ioError.getMessage()); throw new RuntimeException("Exception in DatagramTestApplet. IOException thrown."); } } } // end of class AwtPduViewer