0% found this document useful (0 votes)
94 views9 pages

Import Java

This document describes code for a simple chat application using Java RMI (Remote Method Invocation). It defines interfaces and classes for a Client, Server, and GUI. The Client and Server classes extend UnicastRemoteObject to make their methods remotely accessible. The Server maintains a list of connected Clients and broadcasts messages by calling their remote getMessage() method. The GUI looks up the remote Server, logs into it as a Client, and displays messages received via its showMessage() method.

Uploaded by

yararayan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views9 pages

Import Java

This document describes code for a simple chat application using Java RMI (Remote Method Invocation). It defines interfaces and classes for a Client, Server, and GUI. The Client and Server classes extend UnicastRemoteObject to make their methods remotely accessible. The Server maintains a list of connected Clients and broadcasts messages by calling their remote getMessage() method. The GUI looks up the remote Server, logs into it as a Client, and displays messages received via its showMessage() method.

Uploaded by

yararayan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

import java.rmi.RemoteException; import java.rmi.server.

UnicastRemoteObject; /** * This class implements the interface ClientInterface and extends the class * UnicastRemoteObject. Its constructor and member function getMessage can be * accessed remotely. * @autor Matthias Braunhofer */ public class Client extends UnicastRemoteObject implements ClientInterface { /** * Constructor for the remote object of type Client. This constructor * throws a RemoteException if the object handle cannot be constructed. */ public Client() throws RemoteException { } /** * This method implements the remotely invocable method. It should be called * by the server to send a message and the nickname of its sender to a * client's graphical user interface. It also throws a RemoteException when an * error occurs during the remote method call. */ public void getMessage(String message, String nickname) throws RemoteException GUI.showMessage(message, nickname);

import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import java.util.ArrayList; /** * This class implements the interface ServerInterface and extends the class * UnicastRemoteObject. Its constructor and member functions login and * broadcast can be accessed remotely. * @autor Matthias Braunhofer */ public class Server extends UnicastRemoteObject implements ServerInterface { /** * This ArrayList stores a remote reference for each currently logged in client as * well as its nickname and is used for broadcasting a message, when a client * invokes the remotely accessible method login. */ protected ArrayList<ClientInterface> clients = new ArrayList<ClientInterface>(); /** * This constructor is used for creating a remote object of type Server. It * throws a RemoteException if the object handle cannot be constructed. */ public Server() throws RemoteException { } /** * Implementation of the remotely invocable method. If a client invokes this * method then all other currently logged in clients get a notification * that a new user has joined the chat. This is done by a remote call of the * method getMessage which is defined for the Client object. A remote reference * to the new client is added to the ArrayList. */ public void login(ClientInterface client, String nickname) throws RemoteException { broadcastMessage("--> " + nickname + " is entering the chatroom", ""); clients.add(client); } /** * This method is a remote method which is used for broadcasting an incoming message * and the nickname of its sender to all currently logged in clients. This * broadcasting is done by a remote call of the method getMessage which is

* defined for a client object. The references to such clients are obtained from * the ArrayList which stores a remote reference to each currently logged in client. */ public void broadcastMessage(String message, String nickname) throws RemoteException { for (int i = 0; i < clients.size(); i++) { ClientInterface c = clients.get(i); try { c.getMessage(message, nickname); } catch (RemoteException e) { /** * If a client is not accessible, then it is removed from * the ArrayList and the index i is decremented because * all other clients go down one place */ logout(c); i = i - 1; } } } /** * This method is a local method which is used for removing a connection * from the ArrayList of all connections. */ public void logout(ClientInterface client) { clients.remove(client); } /** * This main method registers a remote accessible Server object by invoking the * rebind method on the class Naming. The name of this object is "Server". This * means that all clients which want to get a reference to this object have to * specify this name in the lookup method. */ public static void main(String[] args) { try { Naming.rebind("Server", new Server()); System.out.println("Server is ready"); } catch (Exception e) { e.printStackTrace(); } } }

import java.rmi.*; /** * This interface extends the class Remote and specifies the remote accessible * methods login and broadcast, which can be accessed externally. * @autor Matthias Braunhofer */ public interface ServerInterface extends Remote { /** * This method can be invoked remotely from a non-local virtual machine. It takes * the nickname of a client and a reference to a remote object of type Client * as parameter. */ public void login(ClientInterface client, String nickname) throws RemoteException; /** * This method can be invoked remotely from a non-local virtual machine. It broadcasts * an incoming message to all currently connected clients. */ public void broadcastMessage(String message, String nickname) throws RemoteException; }

import java.rmi.Remote; import java.rmi.RemoteException; /** * This interface extends the class Remote and specifies a remote accessible * method getMessage, which can be accessed externally. * @autor Matthias Braunhofer */ public interface ClientInterface extends Remote { /** * This method can be invoked remotely from a non-local virtual machine. */ public void getMessage(String message, String nickname) throws RemoteException;

import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowListener; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.WindowConstants;

/** * This code was edited or generated using CloudGarden's Jigloo * SWT/Swing GUI Builder, which is free for non-commercial * use. If Jigloo is being used commercially (ie, by a corporation, * company or business for any purpose whatever) then you * should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. * Use of Jigloo implies acceptance of these licensing terms. * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE. * @autor Matthias Braunhofer */ public class GUI extends javax.swing.JFrame { private Client client; //reference to the local Client object private ServerInterface server; //reference to the remote Server object private static String nickname; private static String host; private static JTextArea History; private JTextField Message; private JScrollPane jScrollPaneHistory; /** * Auto-generated main method to display this JFrame */ public static void main(String[] args) {

"localhost");

host = JOptionPane.showInputDialog("Enter the host of the chatserver", nickname = JOptionPane.showInputDialog("Enter your nickname"); if (host != null && nickname != null && !nickname.equals("")) { try { GUI inst = new GUI(); inst.setVisible(true); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } else System.exit(0);

/** * Inside this constructor we invoke the lookup method on the class Naming for * receiving a reference to a remote Server object. This remote reference * is used for invoking the remote method login, which adds a reference to this * Client object to an existing list of other clients. The remote Server object * is also used for broadcasting messages. */ public GUI() throws MalformedURLException, RemoteException, NotBoundException { super(); server = (ServerInterface)Naming.lookup("//" + host + "/Server"); client = new Client(); server.login(client, nickname); initGUI(); } private void initGUI() { try { { jScrollPaneHistory = new JScrollPane(); getContentPane().add(jScrollPaneHistory); jScrollPaneHistory.setBounds(7, 7, 378, 203); jScrollPaneHistory.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCRO LLBAR_NEVER); { History = new JTextArea();

} {

History.setLineWrap(true); History.setEditable(false); jScrollPaneHistory.setViewportView(History);

} setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); getContentPane().setLayout(null); this.setTitle("MyRMIChat - " + nickname); this.setResizable(false); pack(); setSize(400, 300); } catch (Exception e) { e.printStackTrace(); }

Message = new JTextField(); getContentPane().add(Message); Message.setBounds(7, 217, 378, 42); Message.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent evt) { MessageKeyPressed(evt); } });

/** * This method is called by the getMessage method which is defined inside the * Client class. It simply writes a message and its sender to the TextArea field. * If the nickname is empty, then it means that a new client has joined the chat. */ public static void showMessage(String message, String nickname) { if (!nickname.equals("")) History.append(nickname + ": " + message + "\n"); else History.append(message + "\n"); } /** * This method is called whenever the user presses the enter key. If the text field * is not empty, then we invoke the remote method broadcastMessage on the remote * Server object, which broadcasts the entered message to all other clients.

*/ private void MessageKeyPressed(KeyEvent evt) { if (evt.getKeyCode() == KeyEvent.VK_ENTER && !Message.getText().equals("")) { try { server.broadcastMessage(Message.getText(), nickname); Message.setText(""); } catch (RemoteException e) { e.printStackTrace(); } } } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy