i want applet connect to another machine but the socket cannot connect to the server i searched the web and there is something about giving permissions to the applet but i donnot know how to do so

Recommended Answers

All 23 Replies

how can i do that can you explain a little

I think some of it is discussed in these tutorials:
http://docs.oracle.com/javase/tutorial/deployment/applet/index.html
http://www.oracle.com/technetwork/java/javase/jnlp-136707.html

You can use the JDK's policytool program to update the .java.policy file to give an applet permission on one PC.

Here is a sample entry:

grant {
permission java.net.SocketPermission "www.rxcots.com", "connect, resolve";
};

what can i write in the target name feild

Show the record and the field you are asking about.

check this photo

permission java.net.SocketPermission "127.0.0.1:8889", "connect, resolve";

it does not work what can i do else

What error message do you get?
Did you make a mistake that must be corrected to allow the code to work correctly?

there is no error message but the socket of the applet doesnot connect to the server socket couuld you give me some steps to do

If there is no error message, then it should be working.
Make sure that there are no error messages. Do all the catch blocks call printStackTrace()?


Try writing a simple application (not applet) to connect to the server so that you can be sure that the code is working. If you have problems with the simple program, post a complete program (compiles and executes) here with the error messages.

here is the code of the applet

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testa;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.DatagramSocket;
import java.net.Socket;
import java.net.SocketPermission;

/**
 *
 * @author ali
 */
public class NewJApplet extends javax.swing.JApplet {

    /**
     * Initializes the applet NewJApplet
     */
    @Override
    public void init() {
        
        System.out.println("you entered");
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the applet
         */
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the init() method to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        textField1 = new java.awt.TextField();
        button1 = new java.awt.Button();
        textField2 = new java.awt.TextField();

        setBackground(new java.awt.Color(255, 153, 255));
        getContentPane().setLayout(null);

        jLabel1.setText("الاسم الرباعي للطالب");
        jLabel1.setName("label1");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(251, 35, 151, 29);

        textField1.setName("text1");
        textField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField1ActionPerformed(evt);
            }
        });
        textField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textField1KeyReleased(evt);
            }
        });
        getContentPane().add(textField1);
        textField1.setBounds(27, 35, 193, 29);

        button1.setLabel("button1");
        button1.setName("button1");
        button1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button1MouseClicked(evt);
            }
        });
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });
        getContentPane().add(button1);
        button1.setBounds(51, 130, 183, 33);

        textField2.setName("text2");
        textField2.setText("text2");
        getContentPane().add(textField2);
        textField2.setBounds(68, 90, 195, 20);
    }// </editor-fold>                        

    private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
try{

}
catch(Exception et){
    
}
    }                                       

    private void button1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        try{
           
            // this.textField2.setText(textField1.getText());
       Socket dd=null;
      if(dd.isConnected()==true){
      System.out.println("hello");
      }
      else{
           System.err.println("not connected");
          dd=new Socket("127.0.0.1",3000);
          System.err.println("not connected");
      }
      DataInputStream c=new DataInputStream(dd.getInputStream());
      DataOutputStream c1=new DataOutputStream(dd.getOutputStream());
      c1.writeUTF("hello");
 System.out.println("yeeeeeeeeeeeeee");

}
catch(Exception et){
 System.out.println("the error is:"+et.getStackTrace());  
}
    }                                    

    private void textField1KeyReleased(java.awt.event.KeyEvent evt) {                                       
this.textField2.setText(textField1.getText());     
// TODO add your handling code here:
    try{
            // this.textField2.setText(textField1.getText());
    /*  Socket dd=new Socket("127.0.0.1",3000);
      DataInputStream c=new DataInputStream(dd.getInputStream());
      DataOutputStream c1=new DataOutputStream(dd.getOutputStream());
      c1.writeUTF("hello");
 System.out.println("yeeeeeeeeeeeeee");
*/
}
catch(Exception et){
  et.getStackTrace();  
}
    }                                      

    // Variables declaration - do not modify                     
    private java.awt.Button button1;
    private javax.swing.JLabel jLabel1;
    private java.awt.TextField textField1;
    private java.awt.TextField textField2;
    // End of variables declaration                   
}

and here is the erreor message
the error is:[Ljava.lang.StackTraceElement;@47609b

a simple application (not applet) to connect to the server

the error is:[Ljava.lang.StackTraceElement;@47609b

Please post the full contents of the error message. It will show the stack trace and where the error occurred.

I told you in your other posting of this problem to use printStackTrace().

i write these permission to the applet
grant {
permission java.net.SocketPermission "*:*", "accept, connect, listen,resolve";
};
and i get this srror message
access denied ("java.net.SocketPermission" "127.0.0.1:3000" "connect,resolve")

In what file do you put that statement and where is the file located?

Try changing the * to the actual IP address you are using.

i use netbeans and after i change the permission from the policytool a file was created with the project and i try to change it to the ip but i get this error
access denied ("java.net.SocketPermission" "127.0.0.1:3000" "connect,resolve")

You did not answer my two questions:
In what file do you put that statement >>> What is the filename?
and where is the file located? >>>> What is the path to the file?

the path for the applet policy is
f:\test1\applet.policy

How do you tell the java program to use that policy file?

i donnot know how to do that i think it will take it by default

The java program will not find your policy file.

so what can i do to make it see the policy file

My .java.policy file is located here:
C:\Documents and Settings\Norm\.java.policy

The first part of the path is from java's user.home property

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.