📄 chatserver.java
字号:
package rmichat;
import java.rmi.*;
import java.rmi.server.*;
import java.util.HashMap;
public class ChatServer extends UnicastRemoteObject implements Chat {
private HashMap userList = new HashMap();
private StringBuffer msg = new StringBuffer();
/*构造方法是一定要的*/
public ChatServer() throws RemoteException {
}
/**
* getMsg
*
* @return String
* @todo Implement this rmichat.Chat method
*/
public String getMsg()throws RemoteException {
System.out.println("getMsg()");
return msg.toString();
}
/**
* login
*
* @param name String
* @return boolean
* @todo Implement this rmichat.Chat method
*/
public boolean login(String name)throws RemoteException {
System.out.println("login()");
if(userList.get(name)==null){
return true;
}
return false;
}
/**
* logout
*
* @todo Implement this rmichat.Chat method
*/
public void logout(String name)throws RemoteException {
System.out.println("logout()");
msg.append("--->["+name+"] 退出聊天室\n");
}
/**
* sendMsg
*
* @param msg String
* @todo Implement this rmichat.Chat method
*/
public void sendMsg(String msg)throws RemoteException {
System.out.println("sendMsg()");
this.msg.append("->"+msg+"\n");
}
public static void main(String[] args) throws Exception {
System.setSecurityManager(new RMISecurityManager());
Chat instance = new ChatServer();
Naming.rebind("rmi://localhost/Chat", instance);
System.out.println("Server has been bound!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -