package org.jzy.shudu; import org.jzy.shudu.Server; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.Vector; import java.util.logging.Level; import org.apache.log4j.Logger; public class ServerThread extends Thread { private static final Logger log = Logger.getLogger(ServerThread.class); Socket client; DataInputStream dis; boolean isRun = true; String uname = ""; int map[][] = new int[9][9]; boolean counterFlag=true; public ServerThread(Socket client) { try { this.client = client; dis = new DataInputStream(client.getInputStream()); } catch (Exception ex) { ex.printStackTrace(); } } @Override public void run() { while (isRun) { try { int command = dis.readInt(); log.info(command); switch (command) { //發送用戶名 case Flag.COMMAND_SEND_USERNAME: String clientName = dis.readUTF(); uname = clientName; DataOutputStream dos = new DataOutputStream( client.getOutputStream()); dos.writeInt(Flag.COMMAND_SEND_USERLIST); dos.writeInt(Server.clints.size()); for (int i = 0; i < Server.clints.size(); i++) { ServerThread st = (ServerThread) Server.clints .get(i); dos.writeUTF(st.uname); } for (int i = 0; i < Server.clints.size(); i++) { ServerThread st = (ServerThread) Server.clints .get(i); if (!uname.equals(st.uname)) { DataOutputStream dos1 = new DataOutputStream( st.client.getOutputStream()); dos1.writeInt(Flag.COMMAND_SEND_NEWUSER); dos1.writeUTF(uname); } } Server.sendToAll("用戶" + uname + "加入聊天室"); break; //私聊 case Flag.COMMAND_SEND_CHAT_PRIVATE: String toname = dis.readUTF(); String msg = dis.readUTF(); Server.sendToClient(toname, uname + "悄悄對你說: " + msg); break; //公聊 case Flag.COMMAND_SEND_CHAT_PUBLIC: msg = dis.readUTF(); log.info("公聊: "+msg); Server.sendToAll(uname + " 對你們說: " + msg); break; //遊戲大廳列表 case Flag.COMMAND_GAME_ROOM: msg = dis.readUTF(); for (int i = 0; i < Server.clints.size(); i++) { ServerThread st = (ServerThread) Server.clints .get(i); DataOutputStream dos2 = new DataOutputStream( st.client.getOutputStream()); dos2.writeInt(Flag.COMMAND_GAME_ROOM); dos2.writeUTF(msg); } break; //發送地圖 case Flag.COMMAND_GAME_CREATEMAP: for (int i = 0; i < map.length; i++) { //結收地圖 System.out.println(); for (int j = 0; j < map[i].length; j++) { map[i][j] = dis.readInt(); System.out.print(map[i][j] + "\t"); } } for (int k = 0; k < Server.clints.size(); k++) { ServerThread st = (ServerThread) Server.clints .get(k); DataOutputStream dos3 = new DataOutputStream( st.client.getOutputStream()); dos3.writeInt(Flag.COMMAND_GAME_CREATEMAP); for (int i = 0; i < map.length; i++) { //發送地圖 // System.out.println(); for (int j = 0; j < map[i].length; j++) { dos3.writeInt(map[i][j]); // System.out.print(map[i][j] + "\t"); } } } break; //退出遊戲大廳 case Flag.COMMAND_GAME_EXITROOM: String liveName = dis.readUTF(); for (int i = 0; i < Server.clints.size(); i++) { ServerThread st = (ServerThread) Server.clints .get(i); DataOutputStream dos2 = new DataOutputStream( st.client.getOutputStream()); dos2.writeInt(Flag.COMMAND_GAME_EXITROOM); dos2.writeUTF(liveName); } break; //開始遊戲 case Flag.COMMAND_GAME_GAMESTART: boolean play=dis.readBoolean(); for (int i = 0; i < Server.clints.size(); i++) { ServerThread st = (ServerThread) Server.clints .get(i); DataOutputStream dos2 = new DataOutputStream( st.client.getOutputStream()); dos2.writeInt(Flag.COMMAND_GAME_GAMESTART); dos2.writeBoolean(play); } break; case Flag.COMMAND_GAME_ANSER: String userName=dis.readUTF(); for (int i = 0; i < Server.clints.size(); i++) { ServerThread st = (ServerThread) Server.clints .get(i); DataOutputStream dos2 = new DataOutputStream( st.client.getOutputStream()); dos2.writeInt(Flag.COMMAND_GAME_ANSER); dos2.writeUTF(userName); } //遊戲結束 case Flag.COMMAND_GAME_GAMECOMPLETE: String outName=dis.readUTF(); counterFlag=dis.readBoolean(); //設置計時器終止標誌 for (int i = 0; i < Server.clints.size(); i++) { ServerThread st = (ServerThread) Server.clints .get(i); DataOutputStream dos2 = new DataOutputStream( st.client.getOutputStream()); dos2.writeInt(Flag.COMMAND_GAME_GAMECOMPLETE); dos2.writeUTF(outName); } break; //遊戲計時 case Flag.COMMAND_GAME_TIMECOUNT: int time = 0; while (counterFlag) { try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } time++; for (int i = 0; i < Server.clints.size(); i++) { ServerThread st = (ServerThread) Server.clints .get(i); DataOutputStream dos2 = new DataOutputStream( st.client.getOutputStream()); dos2.writeInt(Flag.COMMAND_GAME_TIMECOUNT); dos2.writeInt(time); } } break; } } catch (Exception ex) { ex.printStackTrace(); Server.clints.remove(this); isRun = false; for (int i = 0; i < Server.clints.size(); i++) { try { ServerThread st = (ServerThread) Server.clints .get(i); DataOutputStream dos1 = new DataOutputStream( st.client.getOutputStream()); dos1.writeInt(Flag.COMMAND_SEND_USERLOGOUT); dos1.writeUTF(uname); } catch (Exception e) { e.printStackTrace(); } } Server.sendToAll("用戶" + uname + "離開聊天室"); } } } }
package org.jzy.shudu; import java.awt.MenuItem; import org.jzy.shudu.Flag; import java.io.DataInputStream; import java.io.ObjectInputStream; import java.net.Socket; import java.util.Timer; import java.util.TimerTask; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import org.apache.log4j.Logger; public class ClientThread extends Thread { private static final Logger log = Logger.getLogger(ClientThread.class); public int[][] serverMap = new int[9][9]; Socket client; DataInputStream dis; JList onlineUserLIst; JList msgList; JList gameRoom; JLabel gameTimer; ShuDuJFrame sdf; MapPanel mapPanel; JButton createGame; public ClientThread(Socket client, JList onlineUserLIst, JList msgList, JList gameRoom, JLabel gameTimer, ShuDuJFrame sdf, MapPanel mapPanel, JButton createGame) { try { this.client = client; this.onlineUserLIst = onlineUserLIst; this.msgList = msgList; this.gameRoom = gameRoom; this.gameTimer = gameTimer; this.sdf = sdf; this.mapPanel = mapPanel; this.createGame = createGame; dis = new DataInputStream(client.getInputStream()); } catch (Exception ex) { ex.printStackTrace(); } } @Override public void run() { while (true) { try { int command = dis.readInt(); switch (command) { // 發送用戶列表 case Flag.COMMAND_SEND_USERLIST: int ucounter = dis.readInt(); for (int i = 0; i < ucounter; i++) { String name = dis.readUTF(); DefaultListModel model = (DefaultListModel) onlineUserLIst .getModel(); model.addElement(name); } break; //�聊天������������������ case Flag.COMMAND_CHAT: String msg = dis.readUTF(); DefaultListModel model = (DefaultListModel) msgList.getModel(); model.addElement(msg); break; //��新用戶 case Flag.COMMAND_SEND_NEWUSER: String newUName = dis.readUTF(); model = (DefaultListModel) onlineUserLIst.getModel(); model.addElement(newUName); break; //用戶離線 case Flag.COMMAND_SEND_USERLOGOUT: String logoutName = dis.readUTF(); model = (DefaultListModel) onlineUserLIst.getModel(); model.removeElement(logoutName); model = (DefaultListModel) gameRoom.getModel(); model.removeElement(logoutName); break; //遊戲大廳列表 case Flag.COMMAND_GAME_ROOM: String roomUser = dis.readUTF(); model = (DefaultListModel) gameRoom.getModel(); model.addElement(roomUser); createGame.setEnabled(false); break; //建立地圖 case Flag.COMMAND_GAME_CREATEMAP: for (int i = 0; i < serverMap.length; i++) { for (int j = 0; j < serverMap[i].length; j++) { serverMap[i][j] = dis.readInt(); } } mapPanel.map = serverMap; for (int i = 0; i < 9; i++) { //清空playmap中保存的上次地圖,以供下次使用(初始化) for (int j = 0; j < 9; j++) { mapPanel.playmap[i][j]=0; } } if (mapPanel.play) { //保護生成的地圖,使他不能被用戶改變 mapPanel.playsk = new ShuDu(); mapPanel.playmap = new int[9][9]; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { mapPanel.playmap[i][j] = mapPanel.map[i][j]; } } // mapPanel.playsk.initMap(mapPanel.playmap); } mapPanel.repaint(); break; //離開遊戲 case Flag.COMMAND_GAME_EXITROOM: String livetName = dis.readUTF(); model = (DefaultListModel) gameRoom.getModel(); model.removeElement(livetName); model = (DefaultListModel) msgList.getModel(); model.addElement(livetName + " 離開遊戲大廳!"); JOptionPane.showMessageDialog(mapPanel, livetName + " 離開遊戲大廳"); break; //遊戲計時 case Flag.COMMAND_GAME_TIMECOUNT: final int time = dis.readInt(); String timeCount = "用時:" + time + "秒"; gameTimer.setText(timeCount); break; //遊戲開始 case Flag.COMMAND_GAME_GAMESTART: boolean play = dis.readBoolean(); JOptionPane.showMessageDialog(mapPanel, "遊戲開始"); mapPanel.play = play; break; //遊戲結束 case Flag.COMMAND_GAME_GAMECOMPLETE: String userName = dis.readUTF(); JOptionPane.showMessageDialog(mapPanel, userName + " 完成遊戲"); break; case Flag.COMMAND_GAME_ANSER: String liveName = dis.readUTF(); JOptionPane.showMessageDialog(mapPanel, liveName + " 投降退出遊戲"); } } catch (Exception ex) { ex.printStackTrace(); } } } }
資源下載(源代碼)地址:http://down.51cto.com/data/871394java