Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 30th, 2005, 1:39 PM   #1
Necronaut
Newbie
 
Join Date: Apr 2005
Posts: 11
Rep Power: 0 Necronaut is on a distinguished road
Angry Instant Messenger using RMI

Hey guys

I'm trying to get this to work. But after hours of fiddling with it and pulling my hair out I still can't get rid of the error I'm getting.

Can you see whee I'm going wrong?
Do you think I could do it better a different way?

I have three files:
MyMessengerInt.java - The interface that declares the methods to be used

MyMessengerImpl.java - Implementation of the methods that will be invoked remotely

RandomGUI.java - The main bulk of the instant messenger bit - Here is where i'm getting the problem

Basically, whenever the actionListener is executed the sendMessage method fails and i get an ArrayOutOfBounds Exception (???). Should the same method be declared anywhere else in the class then it works ok.

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface MyMessengerInt extends Remote
{	
	public String sendMessage(String msg) throws RemoteException;
}

// Server
import java.rmi.*;
import java.net.*;
import java.util.ArrayList;

public class MyMessengerImpl extends java.rmi.server.UnicastRemoteObject implements MyMessengerInt
{
	public static ArrayList userList = new ArrayList();
	public static int numOfUsers = 0;
	public String messages = "";
	
	public MyMessengerImpl() throws RemoteException
	{
		super();
	}

	public String sendMessage(String msg) throws RemoteException
	{
		//messages = messages + msg;
		//return messages;
		System.out.println("Server Returns:\n" + msg);
		return msg;
	}

	public static void main(String args[])
	{
		try
		{
			MyMessengerImpl m = new MyMessengerImpl();
      Naming.rebind("rmi://localhost/MyMessenger", m);
      System.out.println("MyMessenger Server ready");
    }
    catch (RemoteException re)
    {
      System.out.println("Remote Exception " + re);
    }
    catch (Exception e)
    {
      System.out.println(" Exception " + e);
    }
  }

}

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;

public class RandomGUI extends JFrame
{
	private JTextArea messageField;
	private JTextField newTextField;
	public MyMessengerInt m;
	String mess = "";

	public RandomGUI(String userName)
	{
		setTitle("My Messenger");
		//
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		//
		this.setSize(400, 450);

		Container cntnr = this.getContentPane();
		cntnr.setLayout(new BorderLayout());
		
		messageField = new JTextArea("Welcome to MyMessenger "+ userName);
		messageField.setEditable(false);
		cntnr.add(messageField, BorderLayout.CENTER);
		
		newTextField = new JTextField("Type your text here: ");
		cntnr.add(newTextField, BorderLayout.SOUTH);
		//newTextField.addActionListener(this);
		
		MyHandler handler = new MyHandler();
		newTextField.addActionListener(handler);

		this.setVisible(true);

		try
    {
			MyMessengerInt m = (MyMessengerInt) Naming.lookup("rmi://localhost/MyMessenger");		//	Look for the server called 'MyMessenger', cast it into the known server type 'MyMessenger'
//			mess = m.sendMessage("Poopy");
//			System.out.println(mess);
//				mess = m.sendMessage(newTextField.getText() + "\n\n RashyBoy");
				messageField.append("\n\n" + mess);
		}
    catch (RemoteException re)
    {
      System.out.println("Remote Exception " + re);
    }
    catch (Exception e)
    {
      System.out.println("\nException " + e);
    }
/*
		m.startNewClient(userName);
		mess = m.sendMessage("Poopy");	//	Ask the server to give us a string and store the response
		mess += m.sendMessage("\n\nFtsaddf");
		messageField.append("\n\nMyMessengerClient: " + mess + "\n");		//	This is the response from the server...
	}
/*	
	public void actionPerformed(ActionEvent evt)
	{
//		if (evt.getSource() == newTextField)
//		{
			try
			{
//				tempString = m.sendMessage(newTextField.getText() + "\n\n RashyBoy");
//				messageField.append("\n\n" + tempString);

				mess = m.sendMessage("Poopy");	//	Ask the server to give us a string and store the response
//				messageField.append("\n\nMyMessengerClient: " + mess + "\n");		//	This is the response from the server...
				System.out.println("Fuck");
			}
	    catch (RemoteException re)
	    {
	      System.out.println("Remote Exception " + re);
	    }
	    catch (Exception e)
	    {
	      System.out.println(" Exception " + e);
	    }
//			newTextField.setText("");
			//System.out.println(newTextField.getText());
//		}
*/
	}


class MyHandler implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
					if (e.getSource()== newTextField){
		String s="";
		System.out.println("This happened ");
		try
		{
			//s = m.sendMessage(e.getActionCommand());
			s = m.sendMessage("fuck");
			System.out.println("This happened2 : " + s);
		}
		catch (RemoteException re)
		{
			System.out.println("RemoteException");
			System.out.println(re);
		}//kill
		catch (Exception ex)
		{
			System.out.println("Mashup");
			//System.out.println(ex);
		}
		newTextField.setText("");
		messageField.append(s);
		//show();
		System.out.println("Mashup2" + e);
	}
				}
}

	public static void main(String[] args)
	{
		if (args.length !=0)
		{
			RandomGUI thingy = new RandomGUI(args[0]);
		}
		else
		{
			System.out.println("Please provide the username as an argument");
		}
	}
}

Any help/comments/suggestions are appreciated :-)
Necronaut is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:30 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC