Buonasera,
Per esercizio volevo provare a fare un programmino Client/Server con RMI. Il mio scopo sarebbe quello di avere una piccola interfaccia grafica su server (un JFrame con un JButton e una JLabel), in cui il bottone una volta cliccato cambi il testo mostrato nella JLabel. Vorrei che a seconda del Client che clicca il bottone, la label prenda un nome differente. Ad esempio se lancio un client che si chiama Giovanni e un client che si chiama Pietro voglio che a seconda di chi clicca esca il proprio nome nella label. Ho fatto una classe server, una server interface e una classe client. Per ultimo ho implementato una classe con all'interno un main dove creo due client differenti con nomi differenti. Purtroppo non riesco a farlo funzionare. Provo a postare il codice delle mie classi magari qualcuno di voi sa aiutarmi, grazie mille in anticipo a tutti
codice:public class Server implements ServerInterface{ private JFrame frame; private JButton btnCambia; private JLabel lblNome; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Server window = new Server(); window.frame.setVisible(true); Server server = new Server(); ServerInterface stub = (ServerInterface) UnicastRemoteObject.exportObject((Remote) server,0); Registry registro = LocateRegistry.getRegistry(); registro.bind("server", stub); } catch (Exception e) { e.printStackTrace(); } } }); } public Server() { initialize(); } private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); lblNome = new JLabel("Nome"); lblNome.setBounds(198, 76, 61, 16); frame.getContentPane().add(lblNome); btnCambia = new JButton("Cambia"); btnCambia.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { cambia(); } catch (RemoteException e1) { e1.printStackTrace(); } } }); btnCambia.setBounds(165, 178, 117, 29); frame.getContentPane().add(btnCambia); } public void cambia() throws RemoteException { lblNome.setText(Client.nome); } } public interface ServerInterface extends Remote { void cambia() throws RemoteException; } public class Client{ public static String nome; public static void main(String args[]) { try{ Registry registro = LocateRegistry.getRegistry(); ServerInterface i = (ServerInterface) registro.lookup("server"); } catch (Exception e){ } } public Client(String n){ nome = n; } } public class Application { public static void main(String[] args) { Client C1 = new Client("Paolo"); Client C2 = new Client("Giovanni"); } }

Rispondi quotando