Article ID: 186032 - Last Review: November 14, 2005 - Revision: 2.2

FIX: DatagramSocket Causes Security Exception

This article was previously published under Q186032

On This Page

Expand all | Collapse all

SYMPTOMS

Creating a DatagramSocket generates the following exception in your Java console:
com.ms.security.SecurityExceptionEx[className.methodName]:
cannot access port 0
NOTE: This exception may also occur on a port other than zero.

CAUSE

The Microsoft virtual machine incorrectly implements creating a DatagramSocket as a trusted network operation. The correct behavior would be to only treat the act of using the DatagramSocket to communicate with a computer other than the computer hosting the applet is a trusted operation.

RESOLUTION

To workaround this problem, you must make your applet trusted. See the "REFERENCES" section below for articles explaining how to make your applet trusted.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

This problem was corrected in Microsoft virtual machine 31xx and later builds.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create the following file, naming it DatagramSocketDemo.java:
    // DatagramSocketDemo.java
    // ======================
    import java.awt.*;
    import java.net.*;
    public class DatagramSocketDemo extends java.applet.Applet {
      Label label=new Label("");
      public void init() {
        setLayout(new BorderLayout());
        add(new Button("Click me to create a Datagram"),"North");
        add(label,"Center");
      }
      public boolean action(Event ev, Object ob) {
        try {
          DatagramSocket ds=new DatagramSocket(9000);
          System.out.println("Succeeded");
          label.setText(
            "Bug Fixed -- successfully created a DatagramSocket."
          );
        } catch (SocketException ex) {
          ex.printStackTrace();
          label.setText("SocketException: "+ex.getMessage());
        } catch (com.ms.security.SecurityExceptionEx ex) {
          ex.printStackTrace();
          label.setText("Bug -- SecurityExceptionEx:  "+ex.getMessage());
        }
        return true;
      }
    }
    						
  2. Compile the DatagramSocketDemo.java file, creating a DatagramSocketDemo.class.
  3. Create an HTML file named DatagramSocketDemo.html with the following code:
    <HTML>
    <HEAD>
    <TITLE>DatagramSocketDemo</TITLE>
    </HEAD>
    <BODY>
    <APPLET CODE=DatagramSocketDemo width=600 height=100>
    </APPLET>
    <HR>
    <A HREF="DatagramSocketDemo.java">The source.</a>
    </BODY>
    </HTML>
    						
  4. Put DatagramSocketDemo.java, DatagramSocketDemo.class, and DatagramSocketDemo.html in the same folder on your Web server.
  5. Open DatagramSocketDemo.html from Internet Explorer.
  6. Click the button in the applet.
  7. You should to see the message "bug - SecurityExceptionEx: cannot access port 9000"

REFERENCES

For other information on making your code trusted, please see the Microsoft SDK for Java documentation, available at the following Web site:
http://www.microsoft.com/mscorp/java/ (http://www.microsoft.com/mscorp/java/)
For additional information, please see the following articles in the Microsoft Knowledge Base:
181374  (http://support.microsoft.com/kb/181374/EN-US/ ) INFO: Related Component Download Articles
175622  (http://support.microsoft.com/kb/175622/EN-US/ ) PRB: SecurityExceptionEx Exception Running a Java Applet
For additional information on making your Java Code trusted in Microsoft Internet Explorer, please refer to the following Knowledge Base article:
193877  (http://support.microsoft.com/kb/193877/EN-US/ ) HOWTO: Make your Java Code Trusted in Internet Explorer



APPLIES TO
  • Microsoft Java Virtual Machine
  • Microsoft Software Development Kit for Java 2.02
  • Microsoft Software Development Kit for Java 2.01
  • Microsoft Software Development Kit for Java 3.0
  • Microsoft Software Development Kit for Java 3.1
Keywords: 
kbbug kbfix kbnetwork kbjava kbsecurity kbjavavm31xxfix KB186032
 

Article Translations