需要修改的Java聊天应用程序中的Rmi,但如何修改?
我制作了JavaRMI应用程序聊天 聊天类:需要修改的Java聊天应用程序中的Rmi,但如何修改?,java,eclipse,sockets,rmi,Java,Eclipse,Sockets,Rmi,我制作了JavaRMI应用程序聊天 聊天类: public class Chat extends UnicastRemoteObject implements ChatInterface { public String name; public ChatInterface client=null; public Chat(String n) throws RemoteException { this.name=n; } pu
public class Chat extends UnicastRemoteObject implements ChatInterface {
public String name;
public ChatInterface client=null;
public Chat(String n) throws RemoteException {
this.name=n;
}
public String getName() throws RemoteException {
return this.name;
}
public void setClient(ChatInterface c){
client=c;
}
public ChatInterface getClient(){
return client;
}
public void send(String s) throws RemoteException{
System.out.println(s);
}
}
聊天室客户端类:
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class ChatClient {
public static void main (String[] argv) {
try {
System.setProperty("java.rmi.server.hostname","127.0.0.1");
System.setSecurityManager(new RMISecurityManager());
Scanner s=new Scanner(System.in);
System.out.println("Enter Your name and press Enter:");
String name=s.nextLine().trim();
ChatInterface client = new Chat(name);
ChatInterface server = (ChatInterface)Naming.lookup("rmi://localhost/ABC");
String msg="["+client.getName()+"] got connected";
server.send(msg);
System.out.println("[System] Chat Remote Object is ready:");
server.setClient(client);
while(true){
msg=s.nextLine().trim();
msg="["+client.getName()+"] "+msg;
server.send(msg);
}
}catch (Exception e) {
System.out.println("[System] Server failed: " + e);
}
}
}
聊天室界面:
public interface ChatInterface extends Remote{
public String getName() throws RemoteException;
public void send(String msg) throws RemoteException;
public void setClient(ChatInterface c)throws RemoteException;
public ChatInterface getClient() throws RemoteException;
}
聊天服务器类:
public class ChatServer {
public static void main (String[] argv) {
try {
System.setProperty("java.rmi.server.hostname","127.0.0.1");
System.setSecurityManager(new RMISecurityManager());
Scanner s=new Scanner(System.in);
System.out.println("Enter Your name and press Enter:");
String name=s.nextLine().trim();
Chat server = new Chat(name);
Naming.rebind("rmi://localhost/ABC", server);
System.out.println("[System] Chat Remote Object is ready:");
while(true){
String msg=s.nextLine().trim();
if (server.getClient()!=null){
ChatInterface client=server.getClient();
msg="["+server.getName()+"] "+msg;
client.send(msg);
}
}
}catch (Exception e) {
System.out.println("[System] Server failed: " + e);
}
}
}
聊天-两个项目中的代码相同
ChatInterface-两个项目中的代码也相同
ChatClient-ChatServer-它们是具有不同代码的客户端
看起来是这样的: 问题是,这种聊天的效果就像。。。 我们得到了所有的消息(就像整个历史),但当用户发送新消息时。 例如,我们有user1、user2、user3
- 用户1发送消息:“有趣!!!”李>
- User2和User3什么也看不到
- 用户2发送“okkk”
- 现在user1仍然只看到“有趣!!!”
- 用户2查看“乐趣!!!”和“OK”
- 用户1一无所获
- 用户2一无所获
- 用户3什么都没有
- Users3发送“AAAA”
- 所有用户现在都有“AAAA”
setClient()
的本质是服务器一次只知道一个客户机。当然,服务器应该有一个客户机列表,方法应该是addClient(chatcinterface)
getClient()
作为一种远程方法的目的让我难以理解。我开始明白了。如何将此消息发送给所有客户端?所以他们更新了聊天记录?反复浏览客户列表。
public interface ChatInterface extends Remote{
public String getName() throws RemoteException;
public void send(String msg) throws RemoteException;
public void setClient(ChatInterface c)throws RemoteException;
public ChatInterface getClient() throws RemoteException;
}