package mil.navy.nps.testing; /** UDPTestApplet.java Test UDP (port 8004) into a browser. Still no joy. Horrid saga at mail list archive message 0168.html 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 UDPTestApplet 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 privilige 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 UDPTestApplet extends Applet implements Runnable { /** * @serial */ int port; public UDPTestApplet() { } public String getAppletInfo() { return "Name: UDPTestApplet\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(); } //-------------------------------------------------------------------------- // AwtPduViewer Paint Handler public void paint(Graphics g) { this.doSocketStuff(); } public void doSocketStuff() { String portString = getParameter("port"); if (portString == null) { port = 8004; System.out.println("using default port " + port); } else { port = Integer.parseInt(portString); System.out.println("using HTML-specified port " + port); } DatagramSocket unicastDatagramSocket = null; DatagramPacket datagramPacket; byte dataBuffer[] = new byte[1500]; InetAddress destAddress; // PrivilegeManager privilegeManager = PrivilegeManager.getPrivilegeManager(); 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. enablePrivilege() turns off the priv when // the method it's called from exits. //privilegeManager.enablePrivilege("UniversalMulticast"); //privilegeManager.enablePrivilege("UniversalConnect"); //privilegeManager.enablePrivilege("UniversalAccept"); //privilegeManager.enablePrivilege("UniversalListen"); unicastDatagramSocket = new DatagramSocket(port); System.out.println("created a socket correctly, port " + port); unicastDatagramSocket.receive(datagramPacket); System.out.println("Did a receive() correctly"); } catch (SocketException socketException) { System.out.println("socket exception error: " + socketException); throw new RuntimeException("Exception in UDPTestApplet. Socket exception thrown."); } catch(UnknownHostException unkException) // for getLocalHost() call { System.out.println("Unknown host exception: " + unkException); throw new RuntimeException("Exception in UDPTestApplet. Unable to look up localhost correctly"); } catch (IOException ioError) { System.out.println("IO exception error: " + ioError.getMessage()); throw new RuntimeException("Exception in UDPTestApplet. IOException thrown."); } } } // end of class UDPTestApplet