• Post Reply Bookmark Topic Watch Topic
  • New Topic

rmi chat applet

 
Maksim Kogan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys i'm writing a chat applet using RMI and am so far having no luck. having never dealt with RMI in the past i took my pointers from some code i found on the internet and yet i keep getting an error which i don't really know how to take care of. below is my serverimpl and clientimpl code and the error that i get, if anyone can see what the problem is please post, thanks.
//CLIENT CODE
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.Hashtable;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.rmi.Naming;
public class newNameSockets extends JApplet implements ActionListener, Chatter
{
protected Hashtable hash = new Hashtable();
protected Registry registry;
protected String my_name;
protected Chatter chatter;
protected ChatServer server;
protected boolean got_name = true;
private int boldClicks = 0, italicClicks = 0, sendClicks = 0;
protected Color paneColor = new Color(255,255,192);
protected JPanel convBoxPane, sendBoxPane, toolPane, sendButtonPane, leftPane, rightPane;
protected JButton sendButton = new JButton("Send");
protected JButton boldButton = new JButton("Bold");
protected JButton italicButton = new JButton("Italic");
protected JTextPane convBox = new JTextPane();
protected JScrollPane convScroll = new JScrollPane(convBox);
protected Document doc = convBox.getDocument();
protected JTextPane sendBox = new JTextPane();
protected JScrollPane sendScroll = new JScrollPane(sendBox);
protected String[] fontSizes = {"6", "8", "10", "12", "14", "16", "18", "20"};
protected JComboBox fontSizeChooser = new JComboBox(fontSizes);
protected Socket clientSock = null;
protected PrintWriter out = null;
protected BufferedReader in = null;
protected Vector sendStrings;
protected Vector sendStyles;
public newNameSockets()
{
getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("SEND"))
{
if ( ! sendBox.getText().equals("") )
{
try
{
display(my_name);
sendBox.grabFocus();
sendBox.setText("");
boldClicks=0;
italicClicks=0;
fontSizeChooser.setSelectedIndex(3);
sendClicks++;
try
{
server.chat(my_name, sendStrings, sendStyles);
}
catch (Exception ex) {ex.printStackTrace();doc.insertString(doc.getLength(), "not getting there" , convBox.getStyle("regular"));}
}
catch(BadLocationException ble)
{
System.err.println("Couldn't insert sent text.");
}
}
}
else if (e.getActionCommand().equals("BOLD"))
{
if (boldClicks % 2 == 0)
{
sendBox.setCharacterAttributes(convBox.getStyle("bold"), false);
}
else
{
sendBox.setCharacterAttributes(convBox.getStyle("unbold"), false);
}
boldClicks++;
}
else if (e.getActionCommand().equals("ITALIC"))
{
if (italicClicks % 2 == 0)
{
sendBox.setCharacterAttributes(convBox.getStyle("italic"), false);
}
else
{
sendBox.setCharacterAttributes(convBox.getStyle("unitalicize"), false);
}
italicClicks++;
}
else if (e.getActionCommand().equals("FONT"))
{
sendBox.setCharacterAttributes(convBox.getStyle((String)fontSizeChooser.getSelectedItem()), false);
}
sendBox.grabFocus();
}
protected void sendStringParser()
{
sendStyles = new Vector(1,1);
sendStrings = new Vector(1,1);
String sendMessage = sendBox.getText();
int carPos = 0;
sendBox.setCaretPosition(carPos);
int incr = 0;
if (sendMessage.length()==1)
{
sendStyles.add(0, this.getCharacterAttributes(0));
sendStrings.add(0, sendMessage);
}
else if (sendMessage.length()==2)
{
String string0 = "" + sendMessage.charAt(0);
String string1 = "" + sendMessage.charAt(1);
sendStyles.add(0, this.getCharacterAttributes(0));
sendStrings.add(0, string0);
sendStyles.add(1, this.getCharacterAttributes(1));
sendStrings.add(1, string1);
}
else
{
while (carPos+1 < sendMessage.length())
{
if (incr == 0)
sendStyles.add(incr, this.getCharacterAttributes(carPos));
else
sendStyles.add(incr, this.getCharacterAttributes(carPos+1));
String newString;
if (incr==0)
newString = "" + sendMessage.charAt(carPos);
else
newString = "";
while(true)
{
carPos++;
newString += sendMessage.charAt(carPos);
if(!this.getCharacterAttributes(carPos).isEqual(this.getCharacterAttributes(carPos + 1))|| carPos+2 > sendMessage.length())
break;
}
sendStrings.add(incr, newString);
incr++;
}
}
}
protected AttributeSet getCharacterAttributes(int pos)
{
sendBox.setCaretPosition(pos);
return sendBox.getCharacterAttributes();
}
protected void initTextPaneStyles(JTextPane textPane)
{
//Initialize some styles.
Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style regular = textPane.addStyle("regular", def);
StyleConstants.setFontFamily(def, "SansSerif");
StyleConstants.setForeground(def, Color.black);
Style s = textPane.addStyle("italic", regular);
StyleConstants.setItalic(s, true);
StyleConstants.setForeground(s, Color.black);
s = textPane.addStyle("unitalicize", regular);
StyleConstants.setItalic(s, false);
StyleConstants.setForeground(s, Color.black);
s = textPane.addStyle("bold", regular);
StyleConstants.setBold(s, true);
StyleConstants.setForeground(s, Color.black);
s = textPane.addStyle("unbold", regular);
StyleConstants.setBold(s, false);
StyleConstants.setForeground(s, Color.black);
s = textPane.addStyle("userName", regular);
StyleConstants.setBold(s, true);
StyleConstants.setForeground(s, Color.red);
s = textPane.addStyle("otherUser", regular);
StyleConstants.setBold(s, true);
StyleConstants.setForeground(s, Color.blue);
s = textPane.addStyle("6", regular);
StyleConstants.setFontSize(s, 6);
StyleConstants.setForeground(s, Color.black);
s = textPane.addStyle("8", regular);
StyleConstants.setFontSize(s, 8);
StyleConstants.setForeground(s, Color.black);
s = textPane.addStyle("10", regular);
StyleConstants.setFontSize(s, 10);
StyleConstants.setForeground(s, Color.black);
s = textPane.addStyle("12", regular);
StyleConstants.setFontSize(s, 12);
StyleConstants.setForeground(s, Color.black);
s = textPane.addStyle("14", regular);
StyleConstants.setFontSize(s, 14);
StyleConstants.setForeground(s, Color.black);
s = textPane.addStyle("16", regular);
StyleConstants.setFontSize(s, 16);
StyleConstants.setForeground(s, Color.black);
s = textPane.addStyle("18", regular);
StyleConstants.setFontSize(s, 18);
StyleConstants.setForeground(s, Color.black);
s = textPane.addStyle("20", regular);
StyleConstants.setFontSize(s, 20);
StyleConstants.setForeground(s, Color.black);
}
protected void initPanes()
{
// initialize convBoxPane
//*****************************************************
convBoxPane = new JPanel(new GridBagLayout());
convBoxPane.setBorder(BorderFactory.createEmptyBorder(
5, //top
5, //left
0, //bottom
5) //right
);
convBoxPane.setPreferredSize(new Dimension (400, 190));
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.anchor = GridBagConstraints.CENTER;
convBoxPane.add(convScroll, c);
// initialize sendBoxPane
//*****************************************************
sendBoxPane = new JPanel(new GridBagLayout());
sendBoxPane.setBorder(BorderFactory.createEmptyBorder(
5, //top
5, //left
5, //bottom
5) //right
);
sendBoxPane.setPreferredSize(new Dimension (400, 120));
sendBoxPane.add(sendScroll, c);
// initialize sendButtonPane
//*****************************************************
sendButtonPane = new JPanel(new GridBagLayout());
sendButtonPane.setBorder(BorderFactory.createEmptyBorder(
5, //top
5, //left
5, //bottom
5) //right
);
sendButtonPane.setPreferredSize(new Dimension (80, 35));
c.anchor = GridBagConstraints.SOUTH;
sendButtonPane.add(sendButton, c);
// initialize toolPane
//*****************************************************
toolPane = new JPanel(new GridBagLayout());
toolPane.setBorder(BorderFactory.createEmptyBorder(
5, //top
5, //left
0, //bottom
5) //right
);
toolPane.setPreferredSize(new Dimension (400, 70));
toolPane.add(boldButton, c);
c.gridx = 1;
c.gridy = 0;
toolPane.add(italicButton, c);
c.gridx = 2;
c.gridy = 0;
toolPane.add(fontSizeChooser, c);
// initialize leftPane
//*****************************************************
leftPane = new JPanel(new GridBagLayout());
leftPane.setBorder(BorderFactory.createEmptyBorder(
5, //top
5, //left
5, //bottom
5) //right
);
leftPane.setPreferredSize(new Dimension (420, 350));
leftPane.setMinimumSize(new Dimension (390, 340));
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
leftPane.add(convBoxPane, c);
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
leftPane.add(toolPane, c);
c.gridx = 0;
c.gridy = 2;
c.anchor = GridBagConstraints.SOUTHWEST;
leftPane.add(sendBoxPane, c);
leftPane.setBackground(paneColor);
// initialize rightPane
//*****************************************************
rightPane = new JPanel(new GridBagLayout());
rightPane.setBorder(BorderFactory.createEmptyBorder(
5, //top
5, //left
5, //bottom
5) //right
);
rightPane.setPreferredSize(new Dimension (90, 350));
JPanel placeHolder = new JPanel();
placeHolder.setMinimumSize(new Dimension (80, 270));
placeHolder.setPreferredSize(new Dimension (80, 310));
placeHolder.setBackground(paneColor);
placeHolder.setForeground(paneColor);
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.VERTICAL;
c.anchor = GridBagConstraints.NORTH;
rightPane.add(placeHolder, c);
c.gridy = 1;
c.anchor = GridBagConstraints.SOUTH;
rightPane.add(sendButtonPane, c);
rightPane.setBackground(paneColor);
}
public void init()
{
/*
** Adding padding all around by using empty borders
**
*/
JPanel pane = new JPanel(new GridBagLayout());
pane.setBorder(BorderFactory.createEmptyBorder(
30, //top
30, //left
30, //bottom
30) //right
);
convBox.setEditable(false);
initTextPaneStyles(convBox);
convScroll.setHorizontalScrollBarPolicy(convScroll.HORIZONTAL_SCROLLBAR_NEVER);
convScroll.setVerticalScrollBarPolicy(convScroll.VERTICAL_SCROLLBAR_ALWAYS);
convScroll.setPreferredSize(new Dimension(350, 160));
sendScroll.setHorizontalScrollBarPolicy(sendScroll.HORIZONTAL_SCROLLBAR_NEVER);
sendScroll.setVerticalScrollBarPolicy(sendScroll.VERTICAL_SCROLLBAR_ALWAYS);
sendScroll.setPreferredSize(new Dimension(350, 80));
sendButton.setPreferredSize(new Dimension(70, 30));
sendButton.addActionListener(this);
sendButton.setActionCommand("SEND");
boldButton.setPreferredSize(new Dimension(70, 30));
boldButton.addActionListener(this);
boldButton.setActionCommand("BOLD");
italicButton.setPreferredSize(new Dimension(70, 30));
italicButton.addActionListener(this);
italicButton.setActionCommand("ITALIC");
fontSizeChooser.setPreferredSize(new Dimension(70, 30));
fontSizeChooser.addActionListener(this);
fontSizeChooser.setSelectedIndex(3);
fontSizeChooser.setActionCommand("FONT");
initPanes();
GridBagConstraints c = new GridBagConstraints();
c.ipadx = 60;
c.ipady = 60;
c.weightx = 1;
c.weighty = 1;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHWEST;
pane.add(leftPane, c);
c.gridx = 1;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHEAST;
pane.add(rightPane, c);
pane.setBackground(paneColor);
getContentPane().add(pane, BorderLayout.CENTER);
Object[] possibilities = null;
my_name = (String)JOptionPane.showInputDialog(pane, "Enter desired username:\n",
"ENTER USERNAME",
JOptionPane.QUESTION_MESSAGE,
null,
possibilities,
"");
try
{
chatter = new IMImpl(this);
}
catch (Exception ex)
{
System.out.println("Can't do: chatter = new IMImpl(this);");
ex.printStackTrace();
return;
}
try
{
connect();
}
catch (Exception ex)
{
try
{
doc.insertString(doc.getLength(), "can't connect" + ex.getMessage(), convBox.getStyle("regular"));
}
catch(BadLocationException ble)
{
System.err.println("Couldn't show exception.");
}
return;
}
}
public void destroy()
{
try
{
disconnect();
}
catch (java.rmi.RemoteException ex) {ex.printStackTrace();}
}
protected void connect() // throws java.rmi.RemoteException, java.net.MalformedURLException, java.rmi.NotBoundException
{

try
{
String hostname = getParameter("hostname");

if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
else
System.out.println(System.getSecurityManager());
registry = LocateRegistry.getRegistry(1099);
server = (ChatServerImpl)Naming.lookup("//" + hostname + "//ChatServer");
server.login(my_name, chatter);

}
catch(RemoteException e)
{
try
{
doc.insertString(doc.getLength(), "RemoteException " + registry, convBox.getStyle("regular"));
}
catch(BadLocationException ble)
{
System.err.println("Couldn't insert entering name.");
}
}
catch(MalformedURLException e)
{
try
{
doc.insertString(doc.getLength(), "MalformedURLException" , convBox.getStyle("regular"));
}
catch(BadLocationException ble)
{
System.err.println("Couldn't insert entering name.");
}
}
catch(NotBoundException e)
{
try
{
doc.insertString(doc.getLength(), "NotBoundException" , convBox.getStyle("regular"));
}
catch(BadLocationException ble)
{
System.err.println("Couldn't insert entering name.");
}
}
catch (java.security.AccessControlException ex)
{
try
{
doc.insertString(doc.getLength(), "can't connect" + System.getSecurityManager().toString(), convBox.getStyle("regular"));
}
catch(BadLocationException ble)
{
System.err.println("Couldn't show exception.");
}
return;
}
}
protected void disconnect() throws java.rmi.RemoteException
{
if (server != null) server.logout(my_name);
}
public void receiveEnter(String name, Chatter chatter)
{
if (name != null && chatter != null)
{
hash.put(name, chatter);
try
{
doc.insertString(doc.getLength(), name + ": Entered", convBox.getStyle("regular"));
}
catch(BadLocationException ble)
{
System.err.println("Couldn't insert entering name.");
}
}
}
public void receiveExit(String name)
{
if (name != null && chatter != null)
{
hash.remove(name);
try
{
doc.insertString(doc.getLength(), name + ": Left", convBox.getStyle("regular"));
}
catch(BadLocationException ble)
{
System.err.println("Couldn't insert leaving name.");
}
}
}
public void receiveChat(String name, Vector strings, Vector styles) throws BadLocationException
{
sendStrings = strings;
sendStyles = styles;
display(name);
}
public void receiveWhisper(String name, String message) throws BadLocationException
{
display(name+" whispers: "+message);
}
public void display(String name) throws BadLocationException
{
doc.insertString(doc.getLength(), name + ": ", convBox.getStyle("userName"));
this.sendStringParser();
for (int i = 0; i < sendStyles.capacity(); i++)
{
doc.insertString(doc.getLength(), (String)sendStrings.elementAt(i), (AttributeSet)sendStyles.elementAt(i));
}
doc.insertString(doc.getLength(), "\n", (AttributeSet)sendStyles.elementAt(sendStyles.capacity()-1));
}
}
//SERVER CODE
import java.util.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
/**
*
*/
public class ChatServerImpl extends java.rmi.server.UnicastRemoteObject implements ChatServer
{
ChatServerImpl server;
Vector chatters = new Vector();
/**
* Constructor.
*/
public ChatServerImpl() throws java.rmi.RemoteException
{
}
/**
*
*/
public static void main(String[] args)
{
try
{
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
else
System.out.println(System.getSecurityManager());
ChatServerImpl server = new ChatServerImpl();

Registry registry = LocateRegistry.createRegistry( 1099 );
registry.rebind("ChatServer", server);
System.out.println("Server is ready...");
}
catch (Exception ex)
{
System.out.println("Server error: ");
ex.printStackTrace();
}
}
/**
* Register a new chatter, notify other chatters about it.
*/
public void login(String name, Chatter c) throws java.rmi.RemoteException
{
System.out.println(name+" entered");
if (c != null && name != null)
{
Enumeration enume = chatters.elements();
UserInfo u = new UserInfo(name, c);
chatters.addElement(u);
while (enume.hasMoreElements())
{
UserInfo u2 = (UserInfo) enume.nextElement();
u2.chatter.receiveEnter(name, c); //what happens if this chatter is already gone?
}
}
}
/**
* Forget about a chatter, notify other chatters that it left.
*/
public void logout(String name) throws java.rmi.RemoteException
{
if (name == null)
{
System.out.println("null name on logout: cannot remove chatter");
return;
}
UserInfo u_gone = null;
Enumeration enum = null;
synchronized (chatters)
{
for (int i = 0; i < chatters.size(); i++)
{
UserInfo u = (UserInfo) chatters.elementAt(i);
if (u.name.equals(name))
{
System.out.println(name+" left");
u_gone = u;
chatters.removeElementAt(i);
enum = chatters.elements();
break;
}
}
}
if (u_gone == null || enum == null)
{
System.out.println("no user by name of "+name+" found: not removing chatter");
return;
}
while (enum.hasMoreElements())
{
UserInfo u = (UserInfo) enum.nextElement();
u.chatter.receiveExit(name);
}
}
/**
* Called by clients to broadcast chat messages
*/
public void chat(String name, Vector sendStrings, Vector sendStyles) throws java.rmi.RemoteException, javax.swing.text.BadLocationException
{
Enumeration enume = chatters.elements();
while (enume.hasMoreElements())
{
UserInfo u = (UserInfo) enume.nextElement();
u.chatter.receiveChat(name, sendStrings, sendStyles);
}
}
/**
* Called by clients to look up the addresses of other clients
*
* @returns null if the given Chatter's name isn't in the server.
*/
public Chatter lookup(String name)
{
Chatter chatter = null;
if (name != null)
{
System.out.println("looking up "+name);
for (int i=0; i < chatters.size(); i++)
{
UserInfo u = (UserInfo) chatters.elementAt(i);
if (name.equals(u.name))
{
System.out.println("found "+u.name);
chatter = u.chatter;
break;
}
}
}
return chatter;
}
}
I have a few other files but they should not have a bearing on this error.
 
Maksim Kogan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i didnt realize how much code it was. the problem is in the connect method (or at least thats whats throwing the AccesControlException) in the client class (neNameSockets)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!