package com.jmeeting.chatserver;
/*
*
* Jmeeting文字聊天服务器
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class Sever {
private ArrayList<PrintWriter> clientOutputStreams;
private static ArrayList<String> list = new ArrayList<String>();
public class ClientHandler implements Runnable {
BufferedReader reader;
Socket sock;
public ClientHandler(Socket clientSocket) {
try {
/* java.lang.Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
tellEveryone("##★★系统: 系统关闭★★");
}
});*/
sock = clientSocket;
InputStreamReader isReader = new InputStreamReader(sock
.getInputStream());
reader = new BufferedReader(isReader);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void run() {
String message;
try {
while ((message = reader.readLine()) != null) {
System.out.println("##服务器消息: " + message);
if (message.endsWith("LOGIN")) {
System.out.println("LOGIN 上线通知! " + message);
String[] userString = message.split("&");
String s = "";
list.add(message);
Iterator<String> useriIterator = list.iterator();
while (useriIterator.hasNext()) {
s = s + useriIterator.next() + ">>";
}
tellEveryone("%%" + s);
tellEveryone("##" + "★★系统:" + userString[0]
+ " 登录!★★"+"<<");
}
if (message.endsWith("LEAVE")) {
System.out.println("LEAVE 离线通知!" + message);
String[] userString = message.split("&");
System.out.print(message + "离开");
String s = "";
message = message.replaceAll("LEAVE", "LOGIN");
System.out.println("离开测试" + message);
list.remove(message);
Iterator<String> useriIterator = list.iterator();
while (useriIterator.hasNext()) {
s = s + useriIterator.next() + ">>";
}
tellEveryone("%%" + s);
tellEveryone("##" + "★★系统: " + userString[0]
+ "离开!★★"+"##");
}
if (message.endsWith("MSSAGE")) {
System.out.println("MSSAGE 客户端文字信息!" + message);
message = message.replaceAll("MSSAGE", "");
tellEveryone(message);
}
if (message.endsWith("VIDEO")) {
System.out.println("VIDEO 客户端发送了视屏会议通知!" + message);
String[] userString = message.split("&");
tellEveryone("##" + "★★★★系统信息: " + userString[0]
+ " 在 ip: " + userString[2] + " 端口PORT:"
+ userString[3] + " 开始了视频会议!★★★★");
}
}
} catch (Exception ex) {
// ex.printStackTrace();
System.out.println("ClientHandler异常");
}
}
}
public static void main(String[] args) {
new Sever().go();
}
public void go() {
clientOutputStreams = new ArrayList<PrintWriter>();
try {
ServerSocket serverSock = new ServerSocket(9091);
System.out.println("服务器启动成功");
while (true) {
Socket clientSocket = serverSock.accept();
System.out.println(clientSocket.getLocalAddress() + "连接到服务器!");
PrintWriter writer = new PrintWriter(clientSocket
.getOutputStream());
clientOutputStreams.add(writer);
Thread t1 = new Thread(new ClientHandler(clientSocket));
t1.start();
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("服务器启动失败 go函数异常");
}
}
public void tellEveryone(String message) {
Iterator<PrintWriter> it = clientOutputStreams.iterator();
while (it.hasNext()) {
try {
PrintWriter writer = it.next();
writer.println(message);
System.out.println(message);
writer.flush();
} catch (Exception ex) {
ex.printStackTrace();
System.out.print("tellEveryone函数异常");
}
}
}
}