├── resourse ├── log.txt └── user.txt ├── .gitattributes ├── material ├── ku.gif ├── fanu.gif ├── keai.gif ├── lenghan.gif ├── weixiao.gif └── zaijian.gif ├── bin ├── Client │ ├── States.class │ ├── ClientFrame.class │ ├── ClientTools.class │ ├── MiniQQClient.class │ ├── CaptureScreen.class │ ├── ClientFrame$1.class │ ├── ClientFrame$10.class │ ├── ClientFrame$11.class │ ├── ClientFrame$12.class │ ├── ClientFrame$13.class │ ├── ClientFrame$2.class │ ├── ClientFrame$3.class │ ├── ClientFrame$4.class │ ├── ClientFrame$5.class │ ├── ClientFrame$6.class │ ├── ClientFrame$7.class │ ├── ClientFrame$8.class │ ├── ClientFrame$9.class │ ├── MainchatThread.class │ ├── CaptureScreen$Temp.class │ ├── CaptureScreen$PicPanel.class │ ├── CaptureScreen$BMPfilter.class │ ├── CaptureScreen$GIFfilter.class │ ├── CaptureScreen$JPGfilter.class │ ├── CaptureScreen$PNGfilter.class │ └── ClientFrame$ClientThread.class └── Server │ ├── MiniQQServer.class │ ├── ServerFrame.class │ ├── ServerFrame$1.class │ ├── ServerFrame$2.class │ └── ServerProcess.class ├── src ├── Client │ ├── CaptureScreen.java │ ├── ClientFrame.java │ ├── ClientTools.java │ ├── MiniQQClient.java │ └── MainchatThread.java └── Server │ ├── MiniQQServer.java │ ├── ServerFrame.java │ └── ServerProcess.java ├── README.md ├── .settings ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── .classpath └── .project /resourse/log.txt: -------------------------------------------------------------------------------- 1 | 服务器已经启动... -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /resourse/user.txt: -------------------------------------------------------------------------------- 1 | 123|123 2 | 1234|1234 3 | user1|123 4 | user2|123 5 | user3|123 -------------------------------------------------------------------------------- /material/ku.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/material/ku.gif -------------------------------------------------------------------------------- /material/fanu.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/material/fanu.gif -------------------------------------------------------------------------------- /material/keai.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/material/keai.gif -------------------------------------------------------------------------------- /material/lenghan.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/material/lenghan.gif -------------------------------------------------------------------------------- /material/weixiao.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/material/weixiao.gif -------------------------------------------------------------------------------- /material/zaijian.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/material/zaijian.gif -------------------------------------------------------------------------------- /bin/Client/States.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/States.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame.class -------------------------------------------------------------------------------- /bin/Client/ClientTools.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientTools.class -------------------------------------------------------------------------------- /bin/Client/MiniQQClient.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/MiniQQClient.class -------------------------------------------------------------------------------- /bin/Server/MiniQQServer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Server/MiniQQServer.class -------------------------------------------------------------------------------- /bin/Server/ServerFrame.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Server/ServerFrame.class -------------------------------------------------------------------------------- /src/Client/CaptureScreen.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/src/Client/CaptureScreen.java -------------------------------------------------------------------------------- /src/Client/ClientFrame.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/src/Client/ClientFrame.java -------------------------------------------------------------------------------- /src/Client/ClientTools.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/src/Client/ClientTools.java -------------------------------------------------------------------------------- /src/Client/MiniQQClient.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/src/Client/MiniQQClient.java -------------------------------------------------------------------------------- /bin/Client/CaptureScreen.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/CaptureScreen.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$1.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$10.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$10.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$11.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$11.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$12.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$12.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$13.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$13.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$2.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$3.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$4.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$5.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$5.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$6.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$6.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$7.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$7.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$8.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$8.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$9.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$9.class -------------------------------------------------------------------------------- /bin/Client/MainchatThread.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/MainchatThread.class -------------------------------------------------------------------------------- /bin/Server/ServerFrame$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Server/ServerFrame$1.class -------------------------------------------------------------------------------- /bin/Server/ServerFrame$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Server/ServerFrame$2.class -------------------------------------------------------------------------------- /bin/Server/ServerProcess.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Server/ServerProcess.class -------------------------------------------------------------------------------- /src/Client/MainchatThread.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/src/Client/MainchatThread.java -------------------------------------------------------------------------------- /bin/Client/CaptureScreen$Temp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/CaptureScreen$Temp.class -------------------------------------------------------------------------------- /bin/Client/CaptureScreen$PicPanel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/CaptureScreen$PicPanel.class -------------------------------------------------------------------------------- /bin/Client/CaptureScreen$BMPfilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/CaptureScreen$BMPfilter.class -------------------------------------------------------------------------------- /bin/Client/CaptureScreen$GIFfilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/CaptureScreen$GIFfilter.class -------------------------------------------------------------------------------- /bin/Client/CaptureScreen$JPGfilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/CaptureScreen$JPGfilter.class -------------------------------------------------------------------------------- /bin/Client/CaptureScreen$PNGfilter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/CaptureScreen$PNGfilter.class -------------------------------------------------------------------------------- /bin/Client/ClientFrame$ClientThread.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shadowings-zy/javaWebChatroom/HEAD/bin/Client/ClientFrame$ClientThread.class -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # javaWebChatroom 2 | 3 | you may need these two packages: 4 | com.sun.image.codec.jpeg.JPEGCodec; 5 | com.sun.image.codec.jpeg.JPEGImageEncoder; 6 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/Server/MiniQQServer.java=UTF-8 3 | encoding//src/Server/ServerFrame.java=UTF-8 4 | encoding//src/Server/ServerProcess.java=UTF-8 5 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | javaWebChat 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /src/Server/MiniQQServer.java: -------------------------------------------------------------------------------- 1 | package Server; 2 | 3 | /* 4 | * MiniQQ服务器启动程序 5 | */ 6 | 7 | import java.io.*; 8 | import java.net.*; 9 | 10 | public class MiniQQServer extends Thread { 11 | ServerFrame serverFrame = null; 12 | ServerSocket serverSocket = null; // 创建服务器端套接字 13 | 14 | public boolean bServerIsRunning = false; 15 | private final int SERVER_PORT = 8888;// 定义服务器端口号 16 | 17 | public MiniQQServer() { 18 | try { 19 | serverSocket = new ServerSocket(SERVER_PORT); // 启动服务 20 | bServerIsRunning = true; 21 | 22 | serverFrame = new ServerFrame(); 23 | getServerIP(); // 得到并显示服务器端IP 24 | System.out.println("Server port is:" + SERVER_PORT); 25 | serverFrame.taLog.setText("服务器已经启动..."); 26 | while (true) { 27 | Socket socket = serverSocket.accept(); // 监听客户端的连接请求,并返回客户端socket 28 | new ServerProcess(socket, serverFrame); // 创建一个新线程来处理与该客户的通讯 29 | } 30 | } catch (BindException e) { 31 | System.out.println("端口使用中...."); 32 | System.out.println("请关掉相关程序并重新运行服务器!"); 33 | System.exit(0); 34 | } catch (IOException e) { 35 | System.out.println("[ERROR] Cound not start server." + e); 36 | } 37 | 38 | this.start(); // 启动线程 39 | } 40 | 41 | // 获取服务器的主机名和IP地址 42 | public void getServerIP() { 43 | try { 44 | InetAddress serverAddress = InetAddress.getLocalHost(); 45 | byte[] ipAddress = serverAddress.getAddress(); 46 | 47 | serverFrame.txtServerName.setText(serverAddress.getHostName()); 48 | serverFrame.txtIP.setText(serverAddress.getHostAddress()); 49 | serverFrame.txtPort.setText("8888"); 50 | 51 | System.out.println("Server IP is:" + (ipAddress[0] & 0xff) + "." 52 | + (ipAddress[1] & 0xff) + "." + (ipAddress[2] & 0xff) + "." 53 | + (ipAddress[3] & 0xff)); 54 | } catch (Exception e) { 55 | System.out.println("###Cound not get Server IP." + e); 56 | } 57 | } 58 | 59 | // main方法,实例化服务器端程序 60 | public static void main(String args[]) { 61 | new MiniQQServer(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/Server/ServerFrame.java: -------------------------------------------------------------------------------- 1 | package Server; 2 | 3 | /* 4 | * 类名:ServerFrame 5 | * 描述:服务器窗口 6 | */ 7 | import java.awt.*; 8 | import java.awt.event.*; 9 | import java.io.*; 10 | import javax.swing.*; 11 | 12 | // The Server UI. 13 | public class ServerFrame extends JFrame implements ActionListener { 14 | /** 15 | * 16 | */ 17 | private static final long serialVersionUID = 1L; 18 | 19 | JTabbedPane tpServer; 20 | 21 | // 服务器信息面板 22 | JPanel pnlServer, pnlServerInfo; 23 | JLabel lblNumber, lblServerName, lblIP, lblPort, lblLog; 24 | JTextField txtNumber, txtServerName, txtIP, txtPort; 25 | JButton btnStop, btnSaveLog; 26 | TextArea taLog; 27 | 28 | // 用户信息面板 29 | JPanel pnlUser; 30 | JLabel lblUser; 31 | @SuppressWarnings("rawtypes") 32 | JList lstUser; 33 | JScrollPane spUser; 34 | 35 | // 关于本软件 36 | JPanel pnlAbout; 37 | JLabel lblVersionNo, lblAbout; 38 | 39 | @SuppressWarnings("rawtypes") 40 | public ServerFrame() { 41 | super("WebChat 聊天服务器"); 42 | setTitle("MiniQQ 服务器"); 43 | setSize(510, 510); 44 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 45 | setResizable(false); 46 | Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();// 在屏幕居中显示 47 | Dimension fra = this.getSize(); 48 | if (fra.width > scr.width) { 49 | fra.width = scr.width; 50 | } 51 | if (fra.height > scr.height) { 52 | fra.height = scr.height; 53 | } 54 | this.setLocation((scr.width - fra.width) / 2, 55 | (scr.height - fra.height) / 2); 56 | 57 | btnStop = new JButton("关闭服务器(C)"); 58 | btnStop.addActionListener(new ActionListener() { 59 | public void actionPerformed(ActionEvent arg0) { 60 | closeServer(); 61 | } 62 | }); 63 | btnStop.setBackground(Color.ORANGE); 64 | btnStop.setFont(new Font("宋体", 0, 12)); 65 | 66 | // 服务器面板 67 | pnlServer = new JPanel(); 68 | pnlServer.setLayout(null); 69 | pnlServer.setBackground(new Color(106, 90, 205)); 70 | 71 | lblLog = new JLabel("[服务器日志]"); 72 | lblLog.setForeground(new Color(255, 255, 0)); 73 | lblLog.setFont(new Font("微软雅黑", Font.BOLD, 15)); 74 | taLog = new TextArea(20, 50); 75 | taLog.setFont(new Font("宋体", 0, 12)); 76 | 77 | btnSaveLog = new JButton("保存日志(S)"); 78 | btnSaveLog.addActionListener(new ActionListener() { 79 | public void actionPerformed(ActionEvent arg0) { 80 | saveLog(); 81 | } 82 | }); 83 | btnSaveLog.setBackground(Color.ORANGE); 84 | btnSaveLog.setFont(new Font("宋体", 0, 12)); 85 | 86 | lblLog.setBounds(202, 10, 87, 30); 87 | taLog.setBounds(22, 41, 454, 364); 88 | btnStop.setBounds(22, 411, 120, 30); 89 | btnSaveLog.setBounds(356, 411, 120, 30); 90 | pnlServer.add(lblLog); 91 | pnlServer.add(taLog); 92 | pnlServer.add(btnStop); 93 | pnlServer.add(btnSaveLog); 94 | 95 | lstUser = new JList(); 96 | lstUser.setFont(new Font("宋体", 0, 12)); 97 | lstUser.setVisibleRowCount(17); 98 | lstUser.setFixedCellWidth(180); 99 | lstUser.setFixedCellHeight(18); 100 | 101 | // 软件信息 102 | pnlAbout = new JPanel(); 103 | pnlAbout.setLayout(null); 104 | pnlAbout.setBackground(new Color(106, 90, 205)); 105 | pnlAbout.setFont(new Font("宋体", 0, 14)); 106 | 107 | lblVersionNo = new JLabel("MiniQQ created by ZY"); 108 | lblVersionNo.setFont(new Font("微软雅黑", Font.BOLD, 30)); 109 | lblVersionNo.setForeground(Color.YELLOW); 110 | 111 | lblAbout = new JLabel(); 112 | lblAbout.setFont(new Font("微软雅黑", Font.BOLD, 30)); 113 | lblAbout.setText("and thanks to long.luo!"); 114 | lblAbout.setForeground(Color.YELLOW); 115 | 116 | lblVersionNo.setBounds(69, 136, 346, 79); 117 | lblAbout.setBounds(69, 211, 358, 50); 118 | 119 | pnlAbout.add(lblVersionNo); 120 | pnlAbout.add(lblAbout); 121 | 122 | // 主标签面板 123 | tpServer = new JTabbedPane(JTabbedPane.TOP); 124 | tpServer.setBackground(new Color(255, 250, 250)); 125 | tpServer.setFont(new Font("宋体", 0, 14)); 126 | 127 | // 用户面板 128 | pnlUser = new JPanel(); 129 | pnlUser.setLayout(null); 130 | pnlUser.setBackground(new Color(106, 90, 205)); 131 | pnlUser.setBorder(BorderFactory.createCompoundBorder( 132 | BorderFactory.createTitledBorder(""), 133 | BorderFactory.createEmptyBorder(1, 1, 1, 1))); 134 | 135 | lblUser = new JLabel("[在线用户列表]"); 136 | lblUser.setFont(new Font("宋体", Font.BOLD, 22)); 137 | lblUser.setForeground(Color.YELLOW); 138 | 139 | spUser = new JScrollPane(); 140 | spUser.setBackground(new Color(106, 90, 205)); 141 | spUser.setFont(new Font("宋体", 0, 12)); 142 | spUser.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 143 | spUser.getViewport().setView(lstUser); 144 | 145 | pnlUser.setBounds(50, 5, 300, 400); 146 | lblUser.setBounds(169, 22, 198, 33); 147 | spUser.setBounds(169, 65, 298, 362); 148 | 149 | // 服务器信息 150 | pnlServerInfo = new JPanel(new GridLayout(12, 1)); 151 | pnlServerInfo.setBounds(22, 20, 119, 407); 152 | pnlUser.add(pnlServerInfo); 153 | pnlServerInfo.setBackground(new Color(106, 90, 205)); 154 | pnlServerInfo.setFont(new Font("宋体", 0, 12)); 155 | 156 | lblNumber = new JLabel("当前在线人数:"); 157 | lblNumber.setForeground(new Color(255, 255, 0)); 158 | lblNumber.setFont(new Font("宋体", Font.BOLD, 15)); 159 | txtNumber = new JTextField("0 人", 10); 160 | txtNumber.setBackground(Color.decode("#d6f4f2")); 161 | txtNumber.setFont(new Font("宋体", Font.PLAIN, 15)); 162 | txtNumber.setEditable(false); 163 | 164 | lblServerName = new JLabel("服务器名称:"); 165 | lblServerName.setForeground(new Color(255, 255, 0)); 166 | lblServerName.setFont(new Font("宋体", Font.BOLD, 15)); 167 | txtServerName = new JTextField(10); 168 | txtServerName.setBackground(Color.decode("#d6f4f2")); 169 | txtServerName.setFont(new Font("宋体", Font.PLAIN, 15)); 170 | txtServerName.setEditable(false); 171 | 172 | lblIP = new JLabel("服务器IP:"); 173 | lblIP.setForeground(new Color(255, 255, 0)); 174 | lblIP.setFont(new Font("宋体", Font.BOLD, 15)); 175 | txtIP = new JTextField(10); 176 | txtIP.setBackground(Color.decode("#d6f4f2")); 177 | txtIP.setFont(new Font("宋体", Font.PLAIN, 15)); 178 | txtIP.setEditable(false); 179 | 180 | lblPort = new JLabel("服务器端口:"); 181 | lblPort.setForeground(new Color(255, 255, 0)); 182 | lblPort.setFont(new Font("宋体", Font.BOLD, 15)); 183 | txtPort = new JTextField("8888", 10); 184 | txtPort.setBackground(Color.decode("#d6f4f2")); 185 | txtPort.setFont(new Font("宋体", Font.PLAIN, 15)); 186 | txtPort.setEditable(false); 187 | pnlServerInfo.add(lblNumber); 188 | pnlServerInfo.add(txtNumber); 189 | pnlServerInfo.add(lblServerName); 190 | pnlServerInfo.add(txtServerName); 191 | pnlServerInfo.add(lblIP); 192 | pnlServerInfo.add(txtIP); 193 | pnlServerInfo.add(lblPort); 194 | pnlServerInfo.add(txtPort); 195 | 196 | pnlUser.add(lblUser); 197 | pnlUser.add(spUser); 198 | tpServer.add("在线用户", pnlUser); 199 | 200 | tpServer.add("服务器信息", pnlServer); 201 | tpServer.add("关于本软件", pnlAbout); 202 | 203 | this.getContentPane().add(tpServer); 204 | setVisible(true); 205 | } 206 | 207 | protected void closeServer() { 208 | this.dispose(); 209 | } 210 | 211 | protected void saveLog() { 212 | try { 213 | FileOutputStream fileoutput = new FileOutputStream("resourse\\log.txt", true); 214 | String temp = taLog.getText(); 215 | fileoutput.write(temp.getBytes()); 216 | fileoutput.close(); 217 | JOptionPane.showMessageDialog(null, "记录保存在log.txt"); 218 | } catch (Exception e) { 219 | System.out.println(e); 220 | } 221 | } 222 | 223 | public void actionPerformed(ActionEvent evt) { 224 | } 225 | 226 | // 服务器窗口 227 | public static void main(String[] args) { 228 | new ServerFrame(); 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /src/Server/ServerProcess.java: -------------------------------------------------------------------------------- 1 | package Server; 2 | 3 | /* 4 | * 类名:ServerProcess 5 | * 描述:接收到客户端socket发来的信息后进行解析、处理、转发。 6 | */ 7 | import java.io.*; 8 | import java.net.*; 9 | import java.util.*; 10 | 11 | class ServerProcess extends Thread { 12 | private Socket socket = null;// 定义客户端套接字 13 | 14 | private BufferedReader in;// 定义输入流 15 | private PrintWriter out;// 定义输出流 16 | 17 | @SuppressWarnings("rawtypes") 18 | private static Vector onlineUser = new Vector(10, 5); 19 | @SuppressWarnings("rawtypes") 20 | private static Vector socketUser = new Vector(10, 5); 21 | 22 | private String strReceive, strKey; 23 | private StringTokenizer st; 24 | 25 | private final String USERLIST_FILE = "resourse\\user.txt"; // 设定存放用户信息的文件 26 | private ServerFrame sFrame = null; 27 | 28 | public ServerProcess(Socket client, ServerFrame frame) throws IOException { 29 | socket = client; 30 | sFrame = frame; 31 | 32 | in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); // 客户端接收 33 | out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8")), true);// 客户端输出 34 | this.start(); 35 | } 36 | 37 | public void run() { 38 | try { 39 | while (true) { 40 | strReceive = in.readLine();// 从服务器端接收一条信息后拆分、解析,并执行相应操作 41 | st = new StringTokenizer(strReceive, "|"); 42 | strKey = st.nextToken(); 43 | if (strKey.equals("login")) { 44 | login(); 45 | } else if (strKey.equals("talk")) { 46 | talk(); 47 | } else if (strKey.equals("init")) { 48 | freshClientsOnline(); 49 | } else if (strKey.equals("reg")) { 50 | register(); 51 | } else if (strKey.equals("expression")) { 52 | sendExpression(); 53 | } else if (strKey.equals("picture")) { 54 | sendPicture(); 55 | } else if (strKey.equals("printPort")) { 56 | print(); 57 | } else if (strKey.equals("sendPort")) { 58 | sendFile(); 59 | } 60 | 61 | } 62 | } catch (IOException e) { // 用户关闭客户端造成此异常,关闭该用户套接字。 63 | String leaveUser = closeSocket(); 64 | log("用户 " + leaveUser + " 已经退出。" ); 65 | try { 66 | freshClientsOnline(); 67 | } catch (IOException e1) { 68 | e1.printStackTrace(); 69 | } 70 | System.out.println("[SYSTEM] " + leaveUser + " leave chatroom!"); 71 | sendAll("talk|[系统]" + leaveUser + "离开了聊天室"); 72 | } 73 | } 74 | 75 | // 判断是否有该注册用户 76 | @SuppressWarnings({ "resource", "deprecation" }) 77 | private boolean isExistUser(String name) { 78 | String strRead; 79 | try { 80 | FileInputStream inputfile = new FileInputStream(USERLIST_FILE); 81 | DataInputStream inputdata = new DataInputStream(inputfile); 82 | while ((strRead = inputdata.readLine()) != null) { 83 | StringTokenizer stUser = new StringTokenizer(strRead, "|"); 84 | if (stUser.nextToken().equals(name)) { 85 | return true; 86 | } 87 | } 88 | } catch (FileNotFoundException fn) { 89 | System.out.println("[ERROR] User File has not exist!" + fn); 90 | out.println("warning|读写文件时出错!"); 91 | } catch (IOException ie) { 92 | System.out.println("[ERROR] " + ie); 93 | out.println("warning|读写文件时出错!"); 94 | } 95 | return false; 96 | } 97 | 98 | // 判断用户的用户名密码是否正确 99 | @SuppressWarnings("deprecation") 100 | private boolean isUserLogin(String name, String password) { 101 | String strRead; 102 | try { 103 | FileInputStream inputfile = new FileInputStream(USERLIST_FILE); 104 | @SuppressWarnings("resource") 105 | DataInputStream inputdata = new DataInputStream(inputfile); 106 | while ((strRead = inputdata.readLine()) != null) { 107 | if (strRead.equals(name + "|" + password)) { 108 | return true; 109 | } 110 | } 111 | } catch (FileNotFoundException fn) { 112 | System.out.println("[ERROR] User File has not exist!" + fn); 113 | out.println("warning|读写文件时出错!"); 114 | } catch (IOException ie) { 115 | System.out.println("[ERROR] " + ie); 116 | out.println("warning|读写文件时出错!"); 117 | } 118 | return false; 119 | } 120 | 121 | // 用户注册 122 | private void register() throws IOException { 123 | String name = st.nextToken(); // 得到用户名称 124 | String password = st.nextToken().trim();// 得到用户密码 125 | 126 | if (isExistUser(name)) { 127 | System.out.println("[ERROR] " + name + " Register fail!"); 128 | out.println("warning|该用户已存在,请改名!"); 129 | } else { 130 | @SuppressWarnings("resource") 131 | RandomAccessFile userFile = new RandomAccessFile(USERLIST_FILE, "rw"); 132 | userFile.seek(userFile.length()); // 在文件尾部加入新用户信息 133 | userFile.writeBytes(name + "|" + password + "\r\n"); 134 | log("用户 " + name + " 注册成功。" ); 135 | userLoginSuccess(name); // 自动登陆聊天室 136 | } 137 | } 138 | 139 | // 用户登陆(从登陆框直接登陆) 140 | private void login() throws IOException { 141 | String name = st.nextToken(); // 得到用户名称 142 | String password = st.nextToken().trim();// 得到用户密码 143 | boolean succeed = false; 144 | 145 | log("用户 " + name + " 正在登陆..." + "\n" + "密码 : " + password ); 146 | System.out.println("[USER LOGIN] " + name + ":" + password + ":" + socket); 147 | 148 | for (int i = 0; i < onlineUser.size(); i++) { 149 | if (onlineUser.elementAt(i).equals(name)) { 150 | System.out.println("[ERROR] " + name + " is logined!"); 151 | out.println("warning|" + name + "已经登陆聊天室"); 152 | } 153 | } 154 | if (isUserLogin(name, password)) { // 判断用户名和密码 155 | userLoginSuccess(name); 156 | succeed = true; 157 | } 158 | if (!succeed) { 159 | out.println("warning|" + name + "登陆失败,请检查您的输入!"); 160 | log("用户 " + name + " 登陆失败!"); 161 | System.out.println("[SYSTEM] " + name + " login fail!"); 162 | } 163 | } 164 | 165 | // 用户登陆 166 | @SuppressWarnings({ "unchecked", "deprecation" }) 167 | private void userLoginSuccess(String name) throws IOException { 168 | Date t = new Date(); 169 | out.println("login|succeed"); 170 | sendAll("online|" + name); 171 | 172 | onlineUser.addElement(name); 173 | socketUser.addElement(socket); 174 | 175 | log("用户 " + name + " 登录成功, " + "\n登录时间:" + t.toLocaleString()); 176 | 177 | freshClientsOnline(); 178 | sendAll("talk|[系统]欢迎" + name + "来到聊天室"); 179 | System.out.println("[SYSTEM] " + name + " login succeed!"); 180 | } 181 | 182 | // 聊天信息处理 183 | private void talk() throws IOException { 184 | String strTalkInfo = st.nextToken(); // 得到聊天内容; 185 | String strSender = st.nextToken(); // 得到发消息人 186 | String strReceiver = st.nextToken(); // 得到接收人 187 | System.out.println("[TALK_" + strReceiver + "] " + strTalkInfo); 188 | Socket socketSend; 189 | PrintWriter outSend; 190 | 191 | // 得到当前时间 192 | String strTime = getTime(); 193 | 194 | log("用户 " + strSender + " 对 " + strReceiver + " 说: " + strTalkInfo); 195 | 196 | if (strReceiver.equals("All")) { 197 | sendAll("talk|" + strSender + " " + strTime + " : " + strTalkInfo); 198 | } else { 199 | if (strSender.equals(strReceiver)) { 200 | out.println("talk|[系统]不可以自言自语!"); 201 | } else { 202 | for (int i = 0; i < onlineUser.size(); i++) { 203 | if (strReceiver.equals(onlineUser.elementAt(i))) { 204 | socketSend = (Socket) socketUser.elementAt(i); 205 | outSend = new PrintWriter( 206 | new BufferedWriter(new OutputStreamWriter(socketSend.getOutputStream())), true); 207 | outSend.println("talk|[私聊]" + strSender + " " + strTime + ":" + strTalkInfo); 208 | } else if (strSender.equals(onlineUser.elementAt(i))) { 209 | socketSend = (Socket) socketUser.elementAt(i); 210 | outSend = new PrintWriter( 211 | new BufferedWriter(new OutputStreamWriter(socketSend.getOutputStream())), true); 212 | outSend.println("talk|[私聊]" + strReceiver + " " + strTime + ":" + strTalkInfo); 213 | } 214 | } 215 | } 216 | } 217 | } 218 | 219 | //获取时间 220 | private String getTime(){ 221 | GregorianCalendar calendar = new GregorianCalendar(); 222 | int hour = calendar.get(Calendar.HOUR); 223 | int min = calendar.get(Calendar.MINUTE); 224 | int sec = calendar.get(Calendar.SECOND); 225 | 226 | String strHour = "",strMin = "",strSec = ""; 227 | 228 | if(hour>0&&hour<10){ 229 | strHour = "0" + hour; 230 | }else if(hour==0){ 231 | hour = hour +12; 232 | strHour = "" + hour; 233 | } 234 | 235 | if(min<10){ 236 | strMin = "0" + min; 237 | }else{ 238 | strMin = min + ""; 239 | } 240 | 241 | if(sec<10){ 242 | strSec = "0" + sec; 243 | }else{ 244 | strSec = sec + ""; 245 | } 246 | String strTime = "(" + strHour + ":" + strMin + ":" + strSec + ")"; 247 | return strTime; 248 | } 249 | 250 | // 发送表情 251 | private void sendExpression() throws IOException { 252 | String strExpression = st.nextToken(); // 得到聊天内容; 253 | String strSender = st.nextToken(); // 得到发消息人 254 | String strReceiver = st.nextToken(); // 得到接收人 255 | System.out.println("[SendExpression_" + strReceiver + "] " + strExpression); 256 | Socket socketSend; 257 | PrintWriter outSend; 258 | new Date(); 259 | 260 | // 得到当前时间 261 | String strTime = getTime(); 262 | 263 | log("用户 " + strSender + " 对 " + strReceiver + " 说: " + strExpression); 264 | 265 | if (strReceiver.equals("All")) { 266 | sendAll("expression|" + strSender + " " + strTime + " : " + "|" + strExpression + "|"); 267 | } else { 268 | if (strSender.equals(strReceiver)) { 269 | out.println("talk|[系统]不可以自言自语!"); 270 | } else { 271 | for (int i = 0; i < onlineUser.size(); i++) { 272 | if (strReceiver.equals(onlineUser.elementAt(i))) { 273 | socketSend = (Socket) socketUser.elementAt(i); 274 | outSend = new PrintWriter( 275 | new BufferedWriter(new OutputStreamWriter(socketSend.getOutputStream())), true); 276 | outSend.println( 277 | "expression|[私聊]" + strSender + " " + strTime + ":" + "|" + strExpression + "|"); 278 | } else if (strSender.equals(onlineUser.elementAt(i))) { 279 | socketSend = (Socket) socketUser.elementAt(i); 280 | outSend = new PrintWriter( 281 | new BufferedWriter(new OutputStreamWriter(socketSend.getOutputStream())), true); 282 | outSend.println( 283 | "expression|[私聊]" + strReceiver + " " + strTime + ":" + "|" + strExpression + "|"); 284 | } 285 | } 286 | } 287 | } 288 | } 289 | 290 | // 发送图片 291 | private void sendPicture() throws IOException { 292 | String strPicture = st.nextToken(); // 得到聊天内容; 293 | String strSender = st.nextToken(); // 得到发消息人 294 | String strReceiver = st.nextToken(); // 得到接收人 295 | System.out.println("[SendPicture_" + strReceiver + "] 发送图片"); 296 | Socket socketSend; 297 | PrintWriter outSend; 298 | new Date(); 299 | 300 | // 得到当前时间 301 | String strTime = getTime(); 302 | 303 | log("用户 " + strSender + " 对 " + strReceiver + " 发送了图片 "); 304 | 305 | if (strReceiver.equals("All")) { 306 | sendAll("picture|" + strSender + " " + strTime + " : " + "|" + strPicture + "|"); 307 | } else { 308 | if (strSender.equals(strReceiver)) { 309 | out.println("talk|[系统]不可以自言自语!"); 310 | } else { 311 | for (int i = 0; i < onlineUser.size(); i++) { 312 | if (strReceiver.equals(onlineUser.elementAt(i))) { 313 | socketSend = (Socket) socketUser.elementAt(i); 314 | outSend = new PrintWriter( 315 | new BufferedWriter(new OutputStreamWriter(socketSend.getOutputStream())), true); 316 | outSend.println("picture|[私聊]" + strSender + " " + strTime + ":" + "|" + strPicture + "|"); 317 | } else if (strSender.equals(onlineUser.elementAt(i))) { 318 | socketSend = (Socket) socketUser.elementAt(i); 319 | outSend = new PrintWriter( 320 | new BufferedWriter(new OutputStreamWriter(socketSend.getOutputStream())), true); 321 | outSend.println("picture|[私聊]" + strReceiver + " " + strTime + ":" + "|" + strPicture + "|"); 322 | } 323 | } 324 | } 325 | } 326 | } 327 | 328 | // 在线用户列表 329 | @SuppressWarnings("unchecked") 330 | private void freshClientsOnline() throws IOException { 331 | String strOnline = "online"; 332 | String[] userList = new String[20]; 333 | String useName = null; 334 | 335 | for (int i = 0; i < onlineUser.size(); i++) { 336 | strOnline += "|" + onlineUser.elementAt(i); 337 | useName = " " + onlineUser.elementAt(i); 338 | userList[i] = useName; 339 | } 340 | 341 | sFrame.txtNumber.setText("" + onlineUser.size()); 342 | sFrame.lstUser.setListData(userList); 343 | System.out.println(strOnline); 344 | out.println(strOnline); 345 | } 346 | 347 | // 信息群发 348 | private void sendAll(String strSend) { 349 | Socket socketSend; 350 | PrintWriter outSend; 351 | try { 352 | for (int i = 0; i < socketUser.size(); i++) { 353 | socketSend = (Socket) socketUser.elementAt(i); 354 | outSend = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socketSend.getOutputStream())), 355 | true); 356 | outSend.println(strSend); 357 | } 358 | } catch (IOException e) { 359 | System.out.println("[ERROR] send all fail!"); 360 | } 361 | } 362 | 363 | public void log(String log) { 364 | String newlog = sFrame.taLog.getText() + "\n" + log; 365 | sFrame.taLog.setText(newlog); 366 | } 367 | 368 | // 即时画图端口信息处理 369 | private void print() throws IOException { 370 | String strTalkInfo = st.nextToken(); // 得到聊天内容; 371 | String strSender = st.nextToken(); // 得到发消息人 372 | String strReceiver = st.nextToken(); // 得到接收人 373 | System.out.println("[PRINT_" + strReceiver + "] " + strTalkInfo); 374 | log("用户 " + strSender + " 对 " + strReceiver + " 说: " + strTalkInfo); 375 | 376 | if (strReceiver.equals("All")) { 377 | sendAll("printPort|" + strTalkInfo); 378 | } else { 379 | out.println("talk|[系统]即时画图出现错误!"); 380 | } 381 | } 382 | 383 | // 传送文件端口信息处理 384 | private void sendFile() throws IOException { 385 | String strTalkInfo = st.nextToken(); // 得到聊天内容; 386 | String strSender = st.nextToken(); // 得到发消息人 387 | String strReceiver = st.nextToken(); // 得到接收人 388 | System.out.println("[PRINT_" + strReceiver + "] " + strTalkInfo); 389 | log("用户 " + strSender + " 对 " + strReceiver + " 说: " + strTalkInfo); 390 | 391 | if (strReceiver.equals("All")) { 392 | sendAll("sendPort|" + strTalkInfo); 393 | } else { 394 | out.println("talk|[系统]即时画图出现错误!"); 395 | } 396 | } 397 | 398 | // 关闭套接字,并将用户信息从在线列表中删除 399 | private String closeSocket() { 400 | String strUser = ""; 401 | for (int i = 0; i < socketUser.size(); i++) { 402 | if (socket.equals((Socket) socketUser.elementAt(i))) { 403 | strUser = onlineUser.elementAt(i).toString(); 404 | socketUser.removeElementAt(i); 405 | onlineUser.removeElementAt(i); 406 | try { 407 | freshClientsOnline(); 408 | } catch (IOException e) { 409 | e.printStackTrace(); 410 | } 411 | sendAll("remove|" + strUser); 412 | } 413 | } 414 | try { 415 | in.close(); 416 | out.close(); 417 | socket.close(); 418 | } catch (IOException e) { 419 | System.out.println("[ERROR] " + e); 420 | } 421 | 422 | return strUser; 423 | } 424 | } --------------------------------------------------------------------------------