Results 1 to 7 of 7
- 07-26-2009, 11:07 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 30
- Rep Power
- 0
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.
- 07-26-2009, 11:17 PM #2
Member
- Join Date
- Jul 2009
- Posts
- 30
- Rep Power
- 0
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
- 07-27-2009, 05:53 AM #3
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); ... } }
- 07-27-2009, 03:43 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 30
- Rep Power
- 0
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...
- 07-28-2009, 12:40 PM #5
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
- 07-29-2009, 10:37 PM #6
Member
- Join Date
- Jul 2009
- Posts
- 30
- Rep Power
- 0
hey thx, i'll have a look at it tomorrow, need to go to bed xd i'm just 14 u know xD :p
- 07-30-2009, 11:23 AM #7
Member
- Join Date
- Jul 2009
- Posts
- 30
- Rep Power
- 0
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
-
Data Files - A problem that I dont understand :D
By Exhonour in forum New To JavaReplies: 7Last Post: 01-20-2009, 05:13 AM -
Problem generating jar files with ext Class folder
By nespresso in forum EclipseReplies: 0Last Post: 10-06-2008, 03:42 PM -
Problem with executing .jar files
By hiranya in forum NetBeansReplies: 2Last Post: 10-02-2007, 07:24 PM -
client-server communication problem
By revathi17 in forum New To JavaReplies: 1Last Post: 08-09-2007, 02:21 PM -
Communication with c++
By mathias in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 06:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks