When I run the two programs below, I run the Server as an application from the command prompt, then I run the Client as an applet. I always get this exception.
java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:8000" "connect,resolve")
at java.security.AccessControlContext.checkPermission (AccessControlContext.java:366)
at java.security.AccessController.checkPermission(Acc essController.java:555)
at java.lang.SecurityManager.checkPermission(Security Manager.java:549)
at java.lang.SecurityManager.checkConnect(SecurityMan ager.java:1051)
at java.net.Socket.connect(Socket.java:574)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at AppletClient.init(AppletClient.java:25)
at sun.applet.AppletPanel.run(AppletPanel.java:434)
at java.lang.Thread.run(Thread.java:722)
The programs are:
Client:
Code:
import java.io.*;
import java.net.*;
import javax.swing.*;
public class AppletClient extends JApplet {
// Label for displaying the visit count
private JLabel jlblCount = new JLabel();
// Indicate if it runs as application
private boolean isStandAlone = false;
// Host name or ip
private String host = "localhost";
/** Initialize the applet */
public void init() {
add(jlblCount);
try {
// Create a socket to connect to the server
Socket socket;
if (isStandAlone)
socket = new Socket(host, 8000);
else
socket = new Socket(getCodeBase().getHost(), 8000);
// Create an input stream to receive data from the server
DataInputStream inputFromServer =
new DataInputStream(socket.getInputStream());
// Receive the count from the server and display it on label
int count = inputFromServer.readInt();
jlblCount.setText("You are visitor number " + count);
// Close the stream
inputFromServer.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
/** Run the applet as an application */
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Applet Client");
// Create an instance of the applet
AppletClient applet = new AppletClient();
applet.isStandAlone = true;
// Get host
if (args.length == 1) applet.host = args[0];
// Add the applet instance to the frame
frame.add(applet, java.awt.BorderLayout.CENTER);
// Invoke init() and start()
applet.init();
applet.start();
// Display the frame
frame.pack();
frame.setVisible(true);
}
}
Server:
Code:
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class Server extends JFrame {
// Text area for displaying contents
private JTextArea jta = new JTextArea();
public static void main(String[] args) {
new Server();
}
public Server() {
// Place text area on the frame
setLayout(new BorderLayout());
add(new JScrollPane(jta), BorderLayout.CENTER);
setTitle("Server");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // It is necessary to show the frame here!
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
jta.append("Server started at " + new Date() + '\n');
// Listen for a connection request
Socket socket = serverSocket.accept();
// Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(
socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(
socket.getOutputStream());
while (true) {
// Receive radius from the client
double radius = inputFromClient.readDouble();
// Compute area
double area = radius * radius * Math.PI;
// Send area back to the client
outputToClient.writeDouble(area);
jta.append("Radius received from client: " + radius + '\n');
jta.append("Area found: " + area + '\n');
}
}
catch(IOException ex) {
System.err.println(ex);
}
}
}
We are getting the same issue in a project, this is just a much simpler version of the code.