📄 chatserver.java
字号:
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Enumeration;
import java.util.Hashtable;
public class ChatServer extends UnicastRemoteObject implements IChatServer {
Hashtable<String, IChatClient> chatters = new Hashtable<String, IChatClient>();
protected ChatServer() throws RemoteException {
super();
// TODO Auto-generated constructor stub
}
/**
*
*/
private static final long serialVersionUID = 1L;
public synchronized void login(String name, IChatClient client)
throws RemoteException {
// TODO Auto-generated method stub
chatters.put(name, client);
Enumeration e = chatters.elements();
while (e.hasMoreElements()) {
((IChatClient) e.nextElement()).receiveEnter(name);
}
}
public void logout(String name) throws RemoteException {
// TODO Auto-generated method stub
chatters.remove(name);
Enumeration e = chatters.elements();
while (e.hasMoreElements()) {
((IChatClient) e.nextElement()).receiveExit(name);
}
}
public void send(Packet message) throws RemoteException {
// TODO Auto-generated method stub
System.out.println(message);
Enumeration e = chatters.elements();
while (e.hasMoreElements()) {
((IChatClient) e.nextElement()).receiveMessage(message);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
LocateRegistry.createRegistry(8808);
ChatServer server = new ChatServer();
Naming.rebind("//localhost:8808/RMI", server);
System.out.println("Waiting");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -