Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Security error with chat applet

 
Daniel Hoffman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am writing a chat application to be used on a website to enable visitors to have private chat sessions. I have the code written and it works as an application. However when, I converted it to an applet I get a security error when I try and run it:
java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM)
I understand that applets are limited in functionalty between a client and a host but I thought this might work. I have attached the code I am using and would appreciate any assitance.
Thanks
Daniel
------------------ The Code -------------------------
Applet Code
-----------
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.JApplet;
import javax.swing.*;
import java.security.*;
public class CL_Applet extends JApplet
{
private static String ip = new String();
private static String user = new String();
private static ChatApp window;
private static CL_Applet clAPP;

public static void main( String[] args )
{
clAPP = new CL_Applet();
clAPP.init();
}

public void init( )
{
// This code will get the page parameters set for user name and remote ip
// This code is only required once the security issue with the applet is resolved.

user = getParameter("user");
ip = getParameter("ip");

System.out.println( "The value of user is: " + user );
System.out.println( "The value of ip is: " + ip );

// This code is required when running as an application

//user = "Daniel";
//ip = "192.168.0.100";

try
{
WindowListener l = new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
try
{
window.c_out.println( "\n" + user + " has left the chat room" );
window.displayArea.append( "\nYou have left the chat room" );
System.out.println( "\nYou have left the chat room" );
}
catch( Exception c )
{
window.displayArea.append( "\nYou have left the chat room" );
System.out.println( "\nYou have left the chat room" );
}
System.exit( 0 );
}
};

window = new ChatApp( "Connecting Lovers Chat", user, ip );

Toolkit kit = window.getToolkit();
Dimension wSize = kit.getScreenSize();

window.setBounds( wSize.width/4, wSize.height/4, wSize.width/2, wSize.height/2);
window.addWindowListener( l );
window.setVisible( true );
window.show();
window.listenSocket();
}
catch ( Exception s )
{
System.out.println( "\nAn unexpected error has occured.\nThe error message is: " + s );
}
}
}
GUI Stuff
---------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.BorderLayout;
class ChatApp extends JFrame implements ActionListener
{
protected JButton button, sClient;
protected JLabel text, label3;
protected JPanel panel, panel1, panel2, panel3, panel4;
protected JTextField textField;
protected JTextArea displayArea;

protected ServerSocket server = null;
protected Socket socket = null;
protected Socket client = null;
protected BufferedReader c_in = null;
protected PrintWriter c_out = null;
protected BufferedReader s_in = null;
protected PrintWriter s_out = null;
protected String line;
protected String remoteIP;
protected String userName;
private Font hFont;

public ChatApp( String title, String user, String ip )
{
// GUI construction
setTitle( title );
remoteIP = ip;
userName = user;
setDefaultCloseOperation( EXIT_ON_CLOSE );

text = new JLabel( "Compose your message below" );
hFont = new Font( "Times New Roman", Font.BOLD, 12 );
textField = new JTextField(20);
button = new JButton( "Send Message" );
sClient = new JButton( "Start Conversation" );
button.addActionListener( this );
sClient.addActionListener( this );
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
panel2 = new JPanel();
panel2.setLayout( new BorderLayout() );
panel2.add( "North", text);
panel2.add( "Center", textField);
panel3 = new JPanel();
panel3.setLayout( new BorderLayout() );

panel4 = new JPanel();
panel4.setLayout( new BorderLayout());
panel4.add( "North", button );
panel4.add( "Center", sClient );

label3 = new JLabel( "Connecting Lovers Conversation:" );
displayArea = new JTextArea();
displayArea.setEditable( false );
displayArea.setFont( hFont );
panel3.add( "North", label3 );
panel3.add( "Center", displayArea );
panel3.add ( ( new JScrollPane(displayArea) ) );

panel.add("North", panel2);
panel.add("Center", panel3);
panel.add("South", panel4);
getContentPane().add( panel );
}

// implementation of an event handler
public void actionPerformed( ActionEvent event )
{
Object source = event.getSource();
if ( source == button )
{
String text = textField.getText(); // grab the text from JTextField
try
{
displayArea.append( "\n" + userName + ": " + text );// display the text
textField.setText( new String( "" ) );
c_out.println( userName + ": " + text );
}
catch ( Exception e )
{
displayArea.append( "\nThe other chat partner has not joined the conversation. Please wait" );
}
}
else
{
if ( connectSocket( remoteIP ) )
{
sClient.setEnabled( false );
c_out.println( userName + ": Has connected." );
}
}
}

public boolean connectSocket( String targetIP ) // create a socket connection to the server
{
boolean connected;

try
{
socket = new Socket( targetIP, 10000 );
c_out = new PrintWriter( socket.getOutputStream(), true );
c_in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
connected = true;
}
catch ( IOException e )
{
displayArea.append( "\nThe other chat partner chat client has not loaded yet." );
displayArea.append( "\nPlease try connecting again." );
connected = false;
}
return connected;
}
// the server's engine
public void listenSocket( )
{
try
{
server = new ServerSocket( 10000 );
}
catch ( IOException e )
{
System.out.println( "Could not listen on port 10000" );
System.exit( -1 );
}
try
{ // listen for a socket conncetion
client = server.accept();
}
catch ( IOException e )
{
System.out.println( "Accept failed: 10000" );
System.exit( -1 );
}
try
{
s_in = new BufferedReader(new InputStreamReader(client.getInputStream()));
s_out = new PrintWriter( client.getOutputStream(), true );
}
catch ( IOException e )
{
System.out.println( "Accept failed: 10000" );
System.exit(-1);
}

// the loop run by the server
try
{
try
{
while ( true )
{
line = s_in.readLine(); // receive data from the client
if ( line != "" )
{
displayArea.append( "\n" + line );
s_out.println( line );
}
}
}
catch( IOException eof ) { } // connection closed by the client
}
catch ( Exception e )
{
e.printStackTrace();
}
} // end listenSocket
}
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The security model of Java by default prevents applets from having the same level of access to your machine as an application. For example, applets can't call System.exit(), because that would quit the JVM, and the browser might still need it for other applets it is displaying. Also, you can't make network connections to any machine except for the machine from which the applet was downloaded.
If you want to override these default security constraints, you'll have to sign your applet and users of your applet will have to confirm they accept the security overrides you are attempting.
[ February 23, 2004: Message edited by: Rob Ross ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic