Question : Java Client Applet - AccessControlException

I have been told that if you are creating an applet with TCP/UDP code to do in the Applet viewer instead of the browser because it presents less problems... Anyway I get this error when I run it in the Applet Viewer:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
    java.security.AccessControlException: access denied (java.net.SocketPermission 1
    27.0.0.1:4444 connect,resolve)
            at java.security.AccessControlContext.checkPermission(AccessControlConte
    xt.java:323)
            at java.security.AccessController.checkPermission(AccessController.java:
    546)
            at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
            at java.lang.SecurityManager.checkConnect(SecurityManager.java:1034)
            at java.net.Socket.connect(Socket.java:519)
            at java.net.Socket.connect(Socket.java:475)
            at java.net.Socket.<init>(Socket.java:372)
            at java.net.Socket.<init>(Socket.java:186)
            at ClientApplet.run(ClientApplet.java:42)
            at java.lang.Thread.run(Thread.java:619)
    Exception in thread "Thread-4" java.lang.NullPointerException
            at ClientApplet.run(ClientApplet.java:58)
            at java.lang.Thread.run(Thread.java:619)


Here is the code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
    import java.io.*;
    import java.applet.Applet;
    import java.net.*;
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    
    public class ClientApplet extends JApplet implements Runnable {
    
          public static JTextField chat;
          public static JTextArea chattxt;
          static PrintStream os = null;
          static Socket clientSocket = null;
    
          public void init() {
                chat = new JTextField(40);
                chattxt = new JTextArea(20,50);
    
                chat.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent event) {
                        System.out.println(chat.getText());
                   }
                 });
                add(chat);
                add(chattxt);
                   chattxt.append("Hey!");
          }
    
          public void start() {
               new Thread(this).start();
          }
    
          public void run() {
              // communicate with server here
    
    
    Socket s = null;
    
    try {
    //s = new Socket(getParameter("host"), Integer.valueOf(getParameter("port")));
    s = new Socket("localhost", 4444);
    InputStream in = s.getInputStream();
    os = new PrintStream(clientSocket.getOutputStream());
    int buf = -1;
    while ((buf = in.read()) != '.') {
    
    chattxt.append("\n" + buf);
    
    }
    chattxt.append("\nIn read: " + in.read() + "\nServer sent: " + in);
    
    }catch(Exception e) {
    e.printStackTrace();
    }
    finally {
    try {
    	s.close();
    	} catch(IOException e)
    	{ }
    }
    
    
    
    }
    }


Here is the Server.java which I DO run before I start the ClientApplet...
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class Server{
    
        static  Socket clientSocket = null;
        static  ServerSocket serverSocket = null;
    
    	static  clientThread t[] = new clientThread[10];
    
    
        public static void main(String args[]) {
    
    	// The default port
    
    
    
    	int port_number=2222;
    
    	if (args.length < 1)
    	    {
    		System.out.println("Starting game server...\nPort number: "+port_number);
    	    } else {
    		port_number=Integer.valueOf(args[0]).intValue();
    	    }
    
            try {
    	    serverSocket = new ServerSocket(port_number);
    	    System.out.println("\n**********************\n*** SERVER STARTED ***\n**********************\n");
            }
            catch (IOException e)
    	    {System.out.println(e);}
    
    	while(true){
    	    try {
    		clientSocket = serverSocket.accept();
    		System.out.println("*** SOMEONE CONNECTED ***\n");
    		for(int i=0; i<=9; i++){
    		    if(t[i]==null)
    			{
    			    (t[i] = new clientThread(clientSocket,t)).start();
    			    break;
    			}
    		}
    	    }
    	    catch (IOException e) {
    		System.out.println(e);}
    	}
        }
    }
    
    class clientThread extends Thread{
    
        BufferedReader is = null;
        PrintStream os = null;
        Socket clientSocket = null;
        clientThread t[];
        Random roll = new Random();
    
        public clientThread(Socket clientSocket, clientThread[] t){
    	this.clientSocket=clientSocket;
            this.t=t;
        }
    
        public void run()
        {
    		System.out.println("Check 1");
    	String line;
            String name;
            int py;
    	try{
    		System.out.println("Check 2");
    	    is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    	    System.out.println("Check 3");
    	    os = new PrintStream(clientSocket.getOutputStream());
    	    System.out.println("Check 4");
    	    int px = roll.nextInt(200);
    	    //os.println("Enter your name.");
    	    System.out.println("Check 5");
    	    name = "Guest #" + roll.nextInt(6);
    	    System.out.println("Check 6\n");
    	    System.out.println(name+" has entered the game. Their attack: " + px);
    	    System.out.println("Check 7");
    	    for(int i=0; i<=9; i++)
    		if (t[i]!=null && t[i]!=this)
    		    t[i].os.println("*** A new user "+name+" entered the chat room !!! ***" );
    		    System.out.println("Check 8");
    	    while (true) {
    			System.out.println("Check 9");
    		line = is.readLine();
                    if(line.startsWith("/quit")) break;
    		for(int i=0; i<=9; i++)
    		    if (t[i]!=null)  t[i].os.println("<"+name+":"+px+"> "+line); // someone said something
    	    }
    	    for(int i=0; i<=9; i++)
    		if (t[i]!=null && t[i]!=this) {
    		System.out.println("\n" + name + " has left!\n");
    		    t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***" );
    		}
    
    	    os.println("*** Bye "+name+" ***");
    
    	    for(int i=0; i<=9; i++) {
    		if (t[i]==this) t[i]=null;
    		System.out.println("\nSomeone has left!\n");
    	}
    
    	    is.close();
    	    os.close();
    	    clientSocket.close();
    	}
    	catch(IOException e){};
        }
    }

Answer : Java Client Applet - AccessControlException

Sorry, change
            For Each strRegKey In objRegKeys

to
            For Each strRegKey In arrRegKeys


Regards,

Rob.
Random Solutions  
 
programming4us programming4us