📄 simplemultichatserverdemo.java
字号:
package onlyfun.caterpillar.netapp;
import onlyfun.caterpillar.simplenet.multichat.*;
import java.io.*;
import java.util.*;
public class SimpleMultiChatServerDemo {
public static void main(String[] args) throws IOException {
// Launch the multi chat server.
final SimpleMultiChatServer multiChatServer =
new SimpleMultiChatServer(Integer.parseInt(args[0]));
Thread thread;
// Start to wait for clients
thread = new Thread(new Runnable() {
public void run() {
try {
while(true) {
multiChatServer.waitForNextClient();
}
}
catch(IOException e) {
System.out.println("multi chat server: " + e.toString());
}
}
});
thread.setDaemon(true);
thread.start();
// Rember, you should check every connection is alive in a regular time.
// Remove the connection if it's dead.
thread = new Thread(new Runnable() {
public void run() {
try {
while(true) {
Thread.sleep(30000);
multiChatServer.removeDeadConnections();
}
}
catch(InterruptedException e) {
System.out.println("server: " + e.toString());
}
}
});
thread.setDaemon(true);
thread.start();
// Console management for the administrator.
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String adminMessage;
while((adminMessage = buf.readLine()) != null) {
if(adminMessage.equals("shutdown")) {
// Shutdown the multi chat server.
multiChatServer.shutdown();
break;
}
else if(adminMessage.equals("list")) {
// List all clients.
Iterator iterator = multiChatServer.getClientList();
for(int i = 0; iterator.hasNext(); i++) {
System.out.println(i + ": " + ((SimpleMultiChatSubServer) iterator.next()).getName());
}
}
else if(adminMessage.startsWith("remove")) {
// Remove the client according client numbers of input.
String[] arguments = adminMessage.split(" ");
for(int i = 1; i < arguments.length; i++) {
multiChatServer.removeClientAccordingTo(Integer.parseInt(arguments[i]));
}
}
else {
// It's just a administrator's broadcast message.
multiChatServer.broadCastToClient(adminMessage);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -