//=======================================; if (msg.charCodeAt(0) == 13 && msg.charCodeAt(1) == 10) { msg = msg.substr(2); } return msg.split(paramObj.CommandDelimiters); } |
//上面与XMLSocket有关的主要代码,显示方面自己添加相关组件就行了!
//有一个注意点,在flash向服务端发送的命令的最后一定要加上“\r”,否则服务端无法收到消息(我的服务端是用Java开发的)
//=======================================; // 服务端代码(我用java开发的,其他版本自行研究); // ChatServer.java //=======================================; package com.klstudio.socket.chat; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.Vector; //import com.klstudio.util.Logger; /** * @author kinglong * * TODO 要更改此生成的类型注释的模板,请转至窗口-首选项- Java -代码样式-代码模板 */ public class ChatServer { //private Logger logger; private static Vector clients = new Vector(); private static ServerSocket server = null; private static Socket socket = null; public static String CommandDelimiters = "-@@##@@-"; public static String PeopleDelimiters = "-@#@-"; public ChatServer() { } public static void notifyRoom() { StringBuffer people = new StringBuffer("PEOPLE"+CommandDelimiters+"所有的人"); for (int i = 0; i < clients.size(); i++) { Client client = (Client) clients.elementAt(i); people.append(PeopleDelimiters+client.getClientName()); } sendClients(people); } public staticboolean checkName(Client newClient){ for(int i=0;i<clients.size();i++){ Client client = (Client) clients.elementAt(i); if(client != newClient && client.getClientName().equals(newClient.getClientName())){ return false; } } return true; } public static void closeAll(){ while(clients.size()>0){ Client client = (Client) clients.firstElement(); try { client.getClientSocket().close(); } catch (IOException e) { // TODO 自动生成 catch 块 //Logger logger = new Logger(System.out); //logger.log("错误-" + e.toString()); } finally { clients.removeElement(client); } } } public static synchronized void disconnect(Client client) { client.send(new StringBuffer("QUIT")); try { client.getClientSocket().close(); } catch (IOException e) { // TODO 自动生成 catch 块 //Logger logger = new Logger(System.out); //logger.log("错误-" + e.toString()); } finally{ clients.removeElement(client); } } public static synchronized void sendClients(StringBuffer sb) { for(int i=0;i<clients.size();i++){ Client client = (Client) clients.elementAt(i); client.send(sb); } } public static synchronized void sendClients(StringBuffer sb,String ownerName,String toName) { for(int i=0;i<clients.size();i++){ Client client = (Client) clients.elementAt(i); if(toName.equals(client.getClientName()) || toName.equals("所有的人") || ownerName.equals(client.getClientName())){ client.send(sb); } } } public static synchronized void sendClients(Client ownerClient) { for(int i=0;i<clients.size();i++){ Client client = (Client) clients.elementAt(i); if(client.getClientName().equals(ownerClient.getClientName())){ client.send(new StringBuffer("MSG"+CommandDelimiters+"系统信息>欢迎你进入!")); }else{ client.send(new StringBuffer("MSG"+CommandDelimiters+"系统信息>["+ownerClient.getClientName()+"]用户进入!")); } } } public static void main(String[] args) { int port = 8888; if(args.length>0){ port = Integer.parseInt(args[0]); } //Logger logger = new Logger(System.out); //logger.log("信息-ChatServer["+port+"]服务正在启动..."); try { server = new ServerSocket(port); } catch (IOException e) { // TODO 自动生成 catch 块 //logger.log("错误-"+e.toString()); } while(true){ if(clients.size()<5){ try { socket = server.accept(); if(socket != null){ //logger.log("信息-"+socket.toString()+"连接"); } } catch (IOException e) { // TODO 自动生成 catch 块 //logger.log("错误-"+e.toString()); } int i=0; do{ Client client = new Client(socket); if(client.getClientName() != null){ clients.addElement(client); if(checkName(client)){ //logger.log("信息-"+"目前有["+clients.size()+"]个用户已连接"); sendClients(client); client.start(); notifyRoom(); }else{ client.send(new StringBuffer("TAKEN")); disconnect(client); } i++; } break; }while(i<clients.size()); }else{ try { Thread.sleep(200); } catch (InterruptedException e) { // TODO 自动生成 catch 块 //logger.log("错误-"+e.toString()); } } } } } |
//=======================================; // Client.java //=======================================; /* * 创建日期2005-10-10 * * TODO 要更改此生成的文件的模板,请转至 * 窗口-首选项- Java -代码样式-代码模板 */ package com.klstudio.socket.chat; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; //import com.klstudio.util.Logger; /** * @author kinglong * * TODO 要更改此生成的类型注释的模板,请转至窗口-首选项- Java -代码样式-代码模板 */ public class Client extends Thread { private Socket clientSocket; private String clientName; private String clientIp; private BufferedReader br; private PrintStream ps; //private Logger logger; private ChatServer server; public Client(Socket socket) { //this.logger = new Logger(System.out); this.clientSocket = socket; try { this.br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8")); this.ps = new PrintStream(socket.getOutputStream(),true,"utf-8"); String info = this.br.readLine(); if(info!=null){ String[] info_arr = info.split(ChatServer.CommandDelimiters); if(info_arr.length>1){ this.clientName = info_arr[1]; } this.clientIp = socket.getRemoteSocketAddress().toString(); }else{ socket.close(); } } catch (IOException e) { // TODO 自动生成 catch 块 //this.logger.log("错误-" + e.toString()); } } /** * @return 返回 ip。 */ public String getClientIp() { return clientIp; } /** * @return 返回 name。 */ public String getClientName() { return clientName; } /** * @return 返回 socket。 */ public Socket getClientSocket() { return clientSocket; } public void send(StringBuffer msg){ this.ps.println(msg.toString()+"\0"); //this.ps.flush(); } public void run() { while (true) { String line = null; try { line = this.br.readLine(); } catch (IOException e) { // TODO 自动生成 catch 块 //this.logger.log("错误-" + e.toString()); ChatServer.disconnect(this); ChatServer.notifyRoom(); return; } if (line == null) { //this.logger.log("信息-[" + this.clientName + this.clientIp + "]用户离开!"); ChatServer.disconnect(this); ChatServer.notifyRoom(); if(this.clientName != null){ ChatServer.sendClients(new StringBuffer("MSG"+ChatServer.CommandDelimiters+"系统信息>[" + this.clientName + "]用户离开!")); } return; } //this.logger.log("信息-"+line); String[] cmd_arr = line.split(ChatServer.CommandDelimiters); String keyword = cmd_arr[0]; keyword = keyword.substring(1); if(keyword.equals("MSG")){ StringBuffer msg = new StringBuffer("MSG"+ChatServer.CommandDelimiters); msg.append(this.clientName+">"); msg.append(cmd_arr[1]); ChatServer.sendClients(msg,this.clientName,cmd_arr[2]); }else if(keyword.equals("QUIT")){ //this.logger.log("信息-[" + this.clientName + this.clientIp + "]用户离开!"); ChatServer.disconnect(this); ChatServer.notifyRoom(); ChatServer.sendClients(new StringBuffer("MSG"+ChatServer.CommandDelimiters+"系统信息>[" + this.clientName + "]用户离开!")); this.stop(); return; } } } } |