📄 lserver.java
字号:
try{ you = server.accept(); InetAddress address = you.getInetAddress(); jTextArea1.append("有新用户登录,IP:" + address + "\n"); i++; } catch (IOException ex) { Logger.getLogger(LServer.class.getName()).log(Level.SEVERE, null, ex); }catch(NullPointerException e) { Message("指定端口无法开启服务,请关闭服务器重新设定!"); System.exit(0); } if(you != null) { Server_thread peopleThread = new Server_thread(you,peopleList); peopleThread.start(); } else{ continue; } } } }).start(); }//GEN-LAST:event_jButton2ActionPerformed private void jTextArea2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea2KeyPressed // TODO add your handling code here: if ((evt.getKeyCode()) == (KeyEvent.VK_ENTER)) { Enumeration ren = peopleList.elements(); while(ren.hasMoreElements()) { try { Server_thread th = (Server_thread) ren.nextElement(); th.out.writeUTF("系统消息:" + jTextArea2.getText()); jTextArea2.setText(""); } catch (IOException ex) { Logger.getLogger(LServer.class.getName()).log(Level.SEVERE, null, ex); } } } }//GEN-LAST:event_jTextArea2KeyPressed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new LServer().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JComboBox jComboBox1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextArea jTextArea2; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables static Hashtable peopleList = null; class Server_thread extends Thread { String name = null; String sex = null; Socket socket = null; DataOutputStream out = null; DataInputStream in = null; Server_thread(Socket t,Hashtable list) { peopleList = list; socket = t; try{ out = new DataOutputStream(socket.getOutputStream()); in = new DataInputStream(socket.getInputStream()); }catch(IOException e) { } } @Override public void run() { while(true) { String s = null; try{ s = in.readUTF(); if(s.startsWith("当前登陆:")) { name = s.substring(s.indexOf(":") + 1); peopleList.put(name, this);//添加用户到散列表 //通知其它在线用户 Enumeration ren = peopleList.elements(); while(ren.hasMoreElements()) { Server_thread th = (Server_thread)ren.nextElement(); th.out.writeUTF("当前登录:" + name); if(th != this) //将其他在线用户告诉新进来用户 { out.writeUTF("当前登录:" + th.name); } } } else if(s.startsWith("文件传送!")) { String toPeople = s.substring(s.indexOf("*") + 1, s.lastIndexOf("*")); Server_thread toThread = (Server_thread)peopleList.get(toPeople); if(toThread != this){ toThread.out.writeUTF("注意!" + s.substring(s.indexOf("!") + 1, s.indexOf("*")) + "传送文件:" + s.substring(s.lastIndexOf("\\") + 1,s.lastIndexOf(">")) +" 给你。" + "\n"); toThread.out.writeUTF("文件来源:" + s.substring(s.lastIndexOf(">") + 1)); }else{ out.writeUTF("传输错误"); } } else if(s.startsWith("发送完成!")) { String toPeople = s.substring(s.indexOf("!") + 1); Server_thread toThread = (Server_thread)peopleList.get(toPeople); toThread.out.writeUTF("文件传输完成!"); } else if(s.startsWith("拒绝接收!")) { String toPeople = s.substring(s.indexOf("!") + 1); Server_thread toThread = (Server_thread)peopleList.get(toPeople); toThread.out.writeUTF("对方拒绝接收!"); } else if(s.startsWith("开始接收!")) { String toPeople = s.substring(s.indexOf("!") + 1); Server_thread toThread = (Server_thread)peopleList.get(toPeople); toThread.out.writeUTF("开始接收!"); } else if(s.startsWith("公共聊天:")) { name = s.substring(s.indexOf(":") + 1, s.indexOf(">")); String message = s.substring(s.indexOf(">") + 1); Enumeration ren = peopleList.elements(); while(ren.hasMoreElements()) { Server_thread th = (Server_thread)ren.nextElement(); th.out.writeUTF("公共聊天:" + name + "对大家说:" + message); } } else if(s.startsWith("私人聊天:")) { name = s.substring(s.indexOf(":") + 1, s.indexOf(">")); String toPeople = s.substring(s.indexOf("*") + 1); String message = s.substring(s.indexOf(">") + 1, s.indexOf("*")); Server_thread toThread = (Server_thread)peopleList.get(toPeople);//取得需要联系的用户的线程 if(toThread != null) { toThread.out.writeUTF("私人聊天:" + name + ">" + message); } else { out.writeUTF("私人聊天:" + toPeople + "已经离开"); } } else if(s.startsWith("用户离开:")) { Enumeration ren = peopleList.elements(); while(ren.hasMoreElements()) { try { name = s.substring(s.indexOf(":") + 1); Server_thread th = (Server_thread)ren.nextElement(); if(th!=this && th.isAlive()) { th.out.writeUTF("用户离开:" + name); } }catch(IOException e) { } } peopleList.remove(name); socket.close(); jTextArea1.append(name + "用户离开" + "\n"); i--; jTextField1.setText("当前在线用户数为:" + i + "人!"); break; } }catch(IOException ee) //关闭浏览器 { try { Enumeration ren = peopleList.elements(); while (ren.hasMoreElements()) { try { Server_thread th = (Server_thread) ren.nextElement(); if (th != this && th.isAlive()) { th.out.writeUTF("用户离开:" + name); } } catch (IOException e) { } } peopleList.remove(name); socket.close(); jTextArea1.append(name + "用户离开" + "\n"); i--; jTextField1.setText("当前在线用户数为:" + i + "人!"); break; } catch (IOException ex) { Logger.getLogger(LServer.class.getName()).log(Level.SEVERE, null, ex); } } } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -