📄 serverforchat.java
字号:
\\ServerForChat.java
import java.rmi.*;
import java.rmi.server.*;
import java.util.Vector;
import java.rmi.registry.*;
public class ServerForChat extends UnicastRemoteObject implements ServerForChatInterface
{
private static Vector clients = new Vector();
private int port = 1099;
public ServerForChat() throws RemoteException
{
super();
}
public ServerForChat(int port) throws RemoteException
{
super(port);
this.port = port;
}
public void regist(ChatViewerInterface client)
{
if(clients.contains(client)==false)
{
clients.add(client);
}
}
public void chat(String msg)
{
for(int i = 0;i<clients.size();i++)
{
try{
((ChatViewerInterface)(clients.elementAt(i))).appendChatContent(msg);
}catch(Exception e){
clients.remove(i);
continue;
}
}
}
public static void main(String args[])
{
int port = 1099;
if(args!=null&&args.length==1)
{
try{
port = Integer.parseInt(args[0]);
}catch(Exception e){}
}
try{
LocateRegistry.createRegistry(port);
ServerForChat server = new ServerForChat(port);
Naming.rebind("rmi://127.0.0.1:"+port+"/ChatServer",server);
}catch(Exception e){
System.out.println("start sever fail...");
System.exit(0);
}
System.out.println("server started on port: "+port);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -