A
Angus Parvis
Hi,
I want to implement a simple RMI chat with a (Swing-)GUI on the client
side. I manage to send a chat-message to the server and from there to
all clients.
But here the strange things start: the output that should appear in the
client's window appears in the server's. And the client is unable to
access the textarea ment to display the incoming chat messages.
I'm working with Java 1.5 under WinXP Pro. These are commands i use to
start the app:
rmic chat.server.ChatServer
start rmiregistry
start java -Djava.security.policy=policy.txt chat.server.ChatServer
start java -Djava.security.policy=policy.txt chat.client.ChatClient
The source code and the policy file are attached to this posting.
Maybe someone can point me to an existing solution or give a hint
himself. Thx for any help.
Cheers,
Angus
package chat.client;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import chat.server.IChatServer;
public class ChatClient extends JFrame implements ActionListener {
private JTextArea _textarea = new JTextArea();
private JTextField _inputField = new JTextField();
private IChatServer _chatServer;
private void createGUI() {
setSize(400, 600);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
_textarea.setEditable(true);
contentPane.add(new JScrollPane(_textarea), BorderLayout.CENTER);
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(_inputField, BorderLayout.CENTER);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(this);
inputPanel.add(sendButton, BorderLayout.LINE_END);
contentPane.add(inputPanel, BorderLayout.PAGE_END);
}
public ChatClient(IChatServer chatServer) throws RemoteException {
super("Chat Client");
_chatServer = chatServer;
_chatServer.addClient(this);
createGUI();
}
public void actionPerformed(ActionEvent event) {
try {
_chatServer.sendMessage(_inputField.getText());
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void receiveMessage(String message) {
System.out.println("client: message received: " + message);
_textarea.setText(_textarea.getText() + "\n" + message);
}
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
ChatClient client = new ChatClient((IChatServer) Naming
.lookup("Chat"));
client.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package chat.server;
import java.rmi.Remote;
import java.rmi.RemoteException;
import chat.client.ChatClient;
public interface IChatServer extends Remote {
public void sendMessage(String message) throws RemoteException;
public void addClient(ChatClient client) throws RemoteException;
}
package chat.server;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Iterator;
import chat.client.ChatClient;
public class ChatServer extends UnicastRemoteObject implements IChatServer {
private ArrayList<ChatClient> _clients = new ArrayList<ChatClient>();
public ChatServer() throws RemoteException {
}
public synchronized void sendMessage(String message) throws RemoteException {
System.out.println("server: received message: " + message);
for (ChatClient client : _clients) {
System.out.println("Sending to client.");
client.receiveMessage(message);
}
}
public void addClient(ChatClient client) throws RemoteException {
_clients.add(client);
System.out.println("server: client registered");
}
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String name = "Chat";
System.out.println("Starting service " + name);
try {
IChatServer server = new ChatServer();
Naming.rebind(name, server);
System.out.println("ChatServer bound");
} catch (Exception e) {
System.err.println("ChatServer exception: " + e.getMessage());
e.printStackTrace();
}
}
}
grant {
permission java.security.AllPermission;
};
I want to implement a simple RMI chat with a (Swing-)GUI on the client
side. I manage to send a chat-message to the server and from there to
all clients.
But here the strange things start: the output that should appear in the
client's window appears in the server's. And the client is unable to
access the textarea ment to display the incoming chat messages.
I'm working with Java 1.5 under WinXP Pro. These are commands i use to
start the app:
rmic chat.server.ChatServer
start rmiregistry
start java -Djava.security.policy=policy.txt chat.server.ChatServer
start java -Djava.security.policy=policy.txt chat.client.ChatClient
The source code and the policy file are attached to this posting.
Maybe someone can point me to an existing solution or give a hint
himself. Thx for any help.
Cheers,
Angus
package chat.client;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import chat.server.IChatServer;
public class ChatClient extends JFrame implements ActionListener {
private JTextArea _textarea = new JTextArea();
private JTextField _inputField = new JTextField();
private IChatServer _chatServer;
private void createGUI() {
setSize(400, 600);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
_textarea.setEditable(true);
contentPane.add(new JScrollPane(_textarea), BorderLayout.CENTER);
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(_inputField, BorderLayout.CENTER);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(this);
inputPanel.add(sendButton, BorderLayout.LINE_END);
contentPane.add(inputPanel, BorderLayout.PAGE_END);
}
public ChatClient(IChatServer chatServer) throws RemoteException {
super("Chat Client");
_chatServer = chatServer;
_chatServer.addClient(this);
createGUI();
}
public void actionPerformed(ActionEvent event) {
try {
_chatServer.sendMessage(_inputField.getText());
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void receiveMessage(String message) {
System.out.println("client: message received: " + message);
_textarea.setText(_textarea.getText() + "\n" + message);
}
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
ChatClient client = new ChatClient((IChatServer) Naming
.lookup("Chat"));
client.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package chat.server;
import java.rmi.Remote;
import java.rmi.RemoteException;
import chat.client.ChatClient;
public interface IChatServer extends Remote {
public void sendMessage(String message) throws RemoteException;
public void addClient(ChatClient client) throws RemoteException;
}
package chat.server;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Iterator;
import chat.client.ChatClient;
public class ChatServer extends UnicastRemoteObject implements IChatServer {
private ArrayList<ChatClient> _clients = new ArrayList<ChatClient>();
public ChatServer() throws RemoteException {
}
public synchronized void sendMessage(String message) throws RemoteException {
System.out.println("server: received message: " + message);
for (ChatClient client : _clients) {
System.out.println("Sending to client.");
client.receiveMessage(message);
}
}
public void addClient(ChatClient client) throws RemoteException {
_clients.add(client);
System.out.println("server: client registered");
}
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String name = "Chat";
System.out.println("Starting service " + name);
try {
IChatServer server = new ChatServer();
Naming.rebind(name, server);
System.out.println("ChatServer bound");
} catch (Exception e) {
System.err.println("ChatServer exception: " + e.getMessage());
e.printStackTrace();
}
}
}
grant {
permission java.security.AllPermission;
};