Results 1 to 7 of 7
  1. #1
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default problem communication between 2 files

    hey everyone,
    i've got a little problem with the communiction between files. I'm making a chat program for multiple pc's. i've got the communication and it worked, until i wanted to create a window. the window works, and there is only one problem: I cannot acces the functions on the pc that works as server.
    the file that holds the functions is called 'co', so the function here that i want to acces is called "co.Newmessage(newmessage, name)". It gives an error. what is wrong here?

    public class Tribalclient extends JFrame implements ActionListener {


    public static void main(String[] args) throws RemoteException {

    ipServeur = JOptionPane.showInputDialog("Ip = ?", "localhost");
    poort = JOptionPane.showInputDialog("port = ?", "1099");

    Common co = null;

    try {
    co = (Common) Naming.lookup("rmi://"+ipServeur+":"+poort+"/cpt");
    co.incr();
    } catch (MalformedURLException e1) {
    e1.printStackTrace();
    } catch (NotBoundException e1) {
    e1.printStackTrace();
    }

    new WindowCl("Chat");

    ...
    ...
    ...

    public void actionPerformed(ActionEvent actie) {
    if (actie.getSource() == jbtSend) {

    newmessage = textveld.getText();
    textveld.setText("");

    finalmessage = co.Newmessage(newmessage, name);

    textArea.append(finalmessage);

    }




    i marked the error. I think it's because that function is not contained by the 'public void main String args[] ' function.
    how can I acces co. in this function?
    btw this is java not html :p

    thx anyway,

    Questionmark.

  2. #2
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    this is copy of whole program, of anyone needs to see it
    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    import java.net.MalformedURLException;
    import java.rmi.*;
    import java.rmi.server.*;
    import java.security.Timestamp.*;
    import java.util.Date;
    
    public class Tribalclient extends JFrame implements ActionListener {
    	
    	
    	public static void main(String[] args) throws RemoteException {
    
    		ipServeur = JOptionPane.showInputDialog("Ip = ?", "localhost");
    		poort = JOptionPane.showInputDialog("port = ?", "1099");
    		Common co = null;
    		
    		try {
    			co = (Common) Naming.lookup("rmi://"+ipServeur+":"+poort+"/cpt");
    			co.incr();
    		} catch (MalformedURLException e1) {
    			e1.printStackTrace();
    		} catch (NotBoundException e1) {
    			e1.printStackTrace();
    		}		
    		
    		
    			
    new WindowCl("Chat");
    	
    		
    	
    	
    			
    }
    
    	private static final long serialVersionUID = 1L;
    
    	JButton jbtStart = new JButton("START");
    	
    	
    	static String poort;
    	static String messagecl;
    	static String QuiDitLeClnew;
    	static String QuiDitLeClold = "";
    	static long result; 
    	static boolean exit;
    		
    		static String ipServeur;
    		static String newmessage;
    		JButton jbtSend = new JButton("SEND");
    		JTextField textveld = new JTextField(10);
    		static JTextArea textArea = new JTextArea (18,60);
    		static  JScrollPane scrollablearea;
    		private final static String newline = "\n";
    		static String name;
    		static String finalmessage;
    
    		
    		
    
    		
    
    
    	public Tribalclient(String s){
    			super(s);
    			setSize(600, 600);
    			setLocationRelativeTo(null);
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		JPanel j1 = new JPanel();
    			this.add(j1, BorderLayout.SOUTH);
    			j1.add(textveld, BorderLayout.NORTH);
    			jbtSend.addActionListener(this);
    			j1.add(jbtSend);
    			
    
    			JPanel j2 = new JPanel();
    			this.add(j2, BorderLayout.NORTH);
    			j2.add(textArea, BorderLayout.SOUTH);
    			
    			
    
    			GridLayout gl = new GridLayout(1, 4);
    			j1.setLayout(gl);
    			 name = JOptionPane.showInputDialog("wat is uw naam?", "");
    			
    			
    
    			
    			
    			// Create a scrollable text area
    		
    		
    		    
    		    textArea.setEditable(false);
    		    j2.add(textArea, BorderLayout.NORTH);
    		    //scrollablearea = new JScrollPane(textArea);
    		    //j2.add(scrollablearea, BorderLayout.NORTH);
    		    textArea.setText("Client joined" + newline);
    		    setVisible(true);
    		    textArea.setEditable(false);
    		    String messagenu;
    		    String messagevorige;
    		    
    		    messagevorige = null;
    		    
    		    
    		    do{
    		    messagenu = newmessage;
    		    if(!messagenu.equals(messagevorige)) {
    		    
    		    //textArea.append(messagenu + newline);
    		    
    		    
    		    
    		    messagevorige = messagenu;
    		  
    		    }
    		    }while(1+1!=3);
    		   
    		   
    			
    		    
    		    
    
    		    
    		   
    	}
    		    	
    		    	
    		   
    		    
    			
    	
    
    
    		
    		public void actionPerformed(ActionEvent actie) {
    		if (actie.getSource() == jbtSend) {
    			
    				newmessage = textveld.getText();
    				textveld.setText("");
    				
    					finalmessage = co.Newmessage(newmessage, name);
    				
    				textArea.append(finalmessage);
    				
    		}	
    				
    			}
    		 
    
    }
    Last edited by Fubarable; 07-30-2009 at 03:46 PM. Reason: code tags added to improve readability

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    public class Tribalclient extends JFrame implements ActionListener {
        Common co;  // member variable has class scope
    
        public static void main(String[] args) throws RemoteException {
            ...
            // "co" is declared here as a local variable whose scope
            // is inside this method body, ie, curley braces. No one
            // beyond this method will be able to see/access "co".
            // Solution: move declaration into class scope as a
            // member variable.
    //        Common co = null;
            try {
                co = (Common) Naming.lookup("...");
            ...
        }
    
        public void actionPerformed(ActionEvent actie) {
            ...
            // "co" is unheard-of inside here when declared
            // as a local variable above. As a member
            // variable in class scope: no problem in here.
            finalmessage = co.Newmessage(newmessage, name);
            ...
        }
    }

  4. #4
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    thank you, I now understand my mistake, but i've got another problem: now it says 'RemoteException unhandled exception' or something likely :p but when I erase the 'RemoteExceptions' in the other files where this function needs to be described, my 'server' can't launch because of this function that does not contain RemoteException...

  5. #5
    josemoncas's Avatar
    josemoncas is offline Member
    Join Date
    Jul 2009
    Location
    Madrid (Spain)
    Posts
    11
    Rep Power
    0

    Default

    In RMI, there is an interface that describes the methods that are going to be used, right? (class implementing "Remote"). Well, all the methods that are remote must throw a "RemoteException".

    You can handle RemoteExceptions somewhere in your code but don't ever remove it from your methods signature.

    Hope it helps.
    The web that offers Java utilities (classes) which make programming Swing applications an easier task: JMC Java utilities

  6. #6
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    hey thx, i'll have a look at it tomorrow, need to go to bed xd i'm just 14 u know xD :p

  7. #7
    Questionmark is offline Member
    Join Date
    Jul 2009
    Posts
    30
    Rep Power
    0

    Default

    no, it isn't that, if you ment with the 'Remote' class the class that describes which functions that are going to be used by the client on the server (and which the server returns to the client...) and this remote class is a class in the Server package and the Client package. in these Remote classes i already have implemented the RemoteException after each function, so it must be something else...
    anyway thanks !!!

Similar Threads

  1. Data Files - A problem that I dont understand :D
    By Exhonour in forum New To Java
    Replies: 7
    Last Post: 01-20-2009, 05:13 AM
  2. Replies: 0
    Last Post: 10-06-2008, 03:42 PM
  3. Problem with executing .jar files
    By hiranya in forum NetBeans
    Replies: 2
    Last Post: 10-02-2007, 07:24 PM
  4. client-server communication problem
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 02:21 PM
  5. Communication with c++
    By mathias in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •