[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 589
  • Last Modified:

Error : java.security.AccessControlException: access denied (java.net.SocketPermission copet resolve)

When running my applet i recieve this error;

Error : java.security.AccessControlException: access denied (java.net.SocketPermission copet resolve)

have no idea why!?

here is my code for the client/server chat program. NOTE: I am running the server inn MS-Dos and the Applet through IE

Hope someone can enlighten me :)

Kind regard,

Pingu

ChatApplet.java

package Chat;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.awt.Event.*;
import java.util.*;

public class ChatApplet extends Applet
{
     private static final int PortNum = 4000;
     private String msg, name;
     private boolean isExit = false;
     private TextArea chatArea = new TextArea(15,60);
     private TextField msgField = new TextField(50);
     char[] pressed = new char[200];
     int wordCount = 0;
     String message;
     
     public void init()
     {          
          Button button = new Button("EXIT");
          chatArea.setText("MMU Chat Service\n\n");
          chatArea.setEditable(false);
          chatArea.setBackground(Color.BLUE);
          setLayout(new GridLayout(2,1,10,10));
          Panel top = new Panel();
          Panel bottom = new Panel();
          add(top);
          add(bottom);
         
          top.add(chatArea);
          bottom.add(msgField);
          bottom.add(button);
          msgField.requestFocus();
     }

     void updateChatArea(int key)
     {
          if (key == Event.ENTER) {
               try {
                    DatagramSocket socket;
                    DatagramPacket packet;
                    byte[] data = new byte[256];
         
                    msg = msgField.getText();
                    msg.getBytes(0, msg.length(), data, 0);
                    socket = new DatagramSocket();
                    //InetAddress address = InetAddress.getLocalHost();
                    packet = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), PortNum);
                    socket.send(packet);
               
                    packet = new DatagramPacket(data, data.length);
                    socket.receive(packet);
                    msg = new String(packet.getData(), 0);
               
                    chatArea.appendText("<Your Name> " + msg + '\n');
                    socket.close();        
               }
               catch (UnknownHostException e) {
                    chatArea.appendText("Host couldn't be reached\n");
               }
               catch (Exception e) {
                    chatArea.appendText("Error : " + e + '\n');
               }
               msgField.setText("");
          }
     }
     
     public boolean keyDown(Event event, int key)
     {
          if (event.target == msgField)
               updateChatArea(key);
          return false;
     }
}

Server.Java

package Chat;
import java.net.*;
import java.io.*;
import java.lang.*;

public class Server
{
     private static final int portNum = 4000;
     private DatagramSocket Socket;
     private String msg = new String();
         
     public Server()
     {
          try {
               Socket = new DatagramSocket(portNum);
               System.out.println("MMU server is started...");
          }
          catch (SocketException e) {
               System.err.println("Couldn't create Datagram socket");
               System.exit(1);
          }
     }
     
     public void start()
     {
          if (Socket == null) return;
         
          while (true) {
               try {
                    InetAddress address;
                    int port;
                    DatagramPacket packet;
                    byte[] data = new byte[256];
                                             
                    packet = new DatagramPacket(data, data.length);
                    Socket.receive(packet);
                    System.out.println("Data Received...");
                   
                    msg = new String(packet.getData(), 0);
                    address = packet.getAddress();
                    port = packet.getPort();
                   
                    msg.getBytes(0, msg.length(), data, 0);
                    packet = new DatagramPacket(data, data.length, address, port);
                    Socket.send(packet);
                    System.out.println("Data Sent...");
               }
               catch (SocketException e) {
                    System.err.println("Socket error : " + e);
                    e.printStackTrace();
               }
               catch (Exception e) {
                    System.err.println("Error: " + e);
                    System.exit(1);
               }
          }
     }
     
     public static void main(String args[])
     {
          Server server = new Server();
          server.start();
     }
}
0
peterwittering
Asked:
peterwittering
3 Solutions
 
peterwitteringAuthor Commented:
heres my HTML code

<html>
<applet code = "Chat/ChatApplet.class" width = "500" height = "500">
</applet>
</html>

0
 
OBCTCommented:
You'll need to sign the applet.
http://mindprod.com/jgloss/signedapplets.html
0
 
CEHJCommented:
OBCT is correct. You could run a chat applet through sockets without signing it, but you would have to open a socket that binds to the host it originated from. You are trying to bind to localhost
0
 
objectsCommented:
Applets are limited in the activity they can perform, see the follwoing for more details

http://java.sun.com/docs/books/tutorial/applet/practical/security.html

You could try using this:

packet = new DatagramPacket(data, data.length, InetAddress.getByName(new URL(getCodeBase()).getHost(), PortNum);

Though this assumes that the server is running on the same box the applet was loaded from, not sure if that is the case or not (if its not then you'll need sign as OBCT has suggested).
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now