功能不是很多,只是实现了所有在线用户的通话。点对点的通话已经基本想好怎么写了,但是还没有实现
下边是主要代码,希望大家一起探讨。
- /**
- * 用来设置字体属性
- *
- * @author 龚伟涛
- */
- class MyAttribute extends SimpleAttributeSet {
- private static final long serialVersionUID = -6771680451132779539L;
- String config;
- public MyAttribute(String config) {
- this.config = config;
- setAttribute();
- }
- /*
- * 设置属性
- */
- void setAttribute() {
- String[] configs = config.split(" ");
- if (!configs[0].equals("默认"))// 字体种族
- StyleConstants.FontConstants.setFontFamily(this, configs[0]);
- if ("斜体".equals(configs[1]))// 字体类型
- StyleConstants.FontConstants.setItalic(this, true);
- else if ("粗体".equals(configs[1]))
- StyleConstants.FontConstants.setBold(this, true);
- else if ("粗斜体".equals(configs[1])) {
- StyleConstants.FontConstants.setBold(this, true);
- StyleConstants.FontConstants.setItalic(this, true);
- }
- if (configs[2].equals("默认"))// 字体大小
- StyleConstants.FontConstants.setFontSize(this, 12);
- else
- StyleConstants.FontConstants.setFontSize(this, Integer
- .parseInt(configs[2]));
- if (configs[3].equals("默认"))// 字体颜色
- StyleConstants.FontConstants.setForeground(this, Color.BLACK);
- else {
- if ("1".equals(configs[3]))
- StyleConstants.FontConstants.setForeground(this, Color.RED);
- else if ("2".equals(configs[3]))
- StyleConstants.FontConstants.setForeground(this, Color.BLUE);
- else if ("3".equals(configs[3]))
- StyleConstants.FontConstants.setForeground(this, Color.YELLOW);
- else {
- String[] s = configs[3].split(",");
- StyleConstants.FontConstants.setForeground(this, new Color(
- Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer
- .parseInt(s[2])));
- }
- }
- }
- }
- txtNews.addKeyListener(new KeyAdapter() {//按回车提交信息
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.getKeyChar() == KeyEvent.VK_ENTER)
- btnConfirm.doClick();
- }
- });
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == btnConfirm) {
- String getNews = txtNews.getText().trim();
- if (getNews != null) {
- config = configFont + " " + configStyle + " " + configSize
- + " " + configColor;
- try {
- if (out != null)
- out.writeUTF(getNews + "+" + config);
- } catch (IOException e1) {
- }
- txtNews.setText("");
- }
- } else if (e.getSource() == btnClear) {
- txpNews.setText("");
- } else if (e.getSource() == cbxFont) {
- configFont = cbxFont.getSelectedItem().toString().trim();
- } else if (e.getSource() == cbxSize) {
- configSize = cbxSize.getSelectedItem().toString().trim();
- } else if (e.getSource() == cbxStyle) {
- configStyle = cbxStyle.getSelectedItem().toString().trim();
- } else if (e.getSource() == cbxColor) {
- String s = cbxColor.getSelectedItem().toString().trim();
- if (("红色".equals(s))) {
- configColor = "1";
- } else if (("蓝色".equals(s))) {
- configColor = "2";
- } else if (("黄色".equals(s))) {
- configColor = "3";
- } else if (("自定义".equals(s))) {
- Color c = JColorChooser.showDialog(this, "自定义字体颜色", null);
- configColor = c.getRed() + "," + c.getGreen() + ","
- + c.getBlue();
- } else {
- configColor = "默认";
- }
- } else if (e.getSource() == btnStart) {
- if (((JButton) e.getSource()).getText().equals("连接")) {
- IP = JOptionPane.showInputDialog("请设置IP地址", IP);// 设置IP
- String regex = "((2[0-4]//d)|(25[0-5])|(1//d{2})|([1-9]//d)|(//d)).((2[0-4]//d)|(25[0-5])|(1//d{2})|([1-9]//d)|(//d)).((2[0-4]//d)|(25[0-5])|(1//d{2})|([1-9]//d)|(//d)).((2[0-4]//d)|(25[0-5])|(1//d{2})|([1-9]//d)|(//d))";
- if (!verEx(IP, regex)) {
- JOptionPane.showMessageDialog(this, "IP设置不符合规范");
- return;
- }
- String s = JOptionPane.showInputDialog("请设置IP地址", port);// 设置端口
- if (s.length() > 5) {
- s = s.substring(0, 6);
- }
- if (verEx(s, "//d*")) {
- port = Integer.parseInt(s.trim());
- if (port > 65535 || port < 1) {
- JOptionPane.showMessageDialog(this,"端口号超出范围,请输入0~65535之间的整数。");
- return;
- }
- } else {
- JOptionPane.showMessageDialog(this,"端口号应该为整数,请输入0~65535之间的整数。");
- return;
- }
- start = true;
- } else {
- exit();
- }
- } else if (e.getSource() == btnStop) {
- exit();
- }
- }
- /*
- * 其他支持方法
- */
- protected void exit() {// 退出系统
- try {
- socket.close();
- } catch (Exception e) {
- } finally {
- System.exit(0);
- }
- }
- protected boolean verEx(String strVer, String strExp) {// 正则判断
- Pattern p = Pattern.compile(strExp);
- Matcher m = p.matcher(strVer);
- return m.matches();
- }
- /*
- * 重写run()方法
- */
- @Override
- public void run() {
- String news = null;
- try {
- socket = new Socket(IP, port);// 实例化套接字
- in = new DataInputStream(socket.getInputStream());// 获取输入流
- out = new DataOutputStream(socket.getOutputStream());// 获取输出流
- btnStart.setText("已登录");
- btnStart.setEnabled(false);
- } catch (IOException e) {
- btnStart.setText("退出");
- txpNews.setText("未发现服务器。");
- btnConfirm.setEnabled(false);
- return;
- }
- while (true) {
- try {
- news = in.readUTF();
- int m = news.lastIndexOf("+");
- int n = news.lastIndexOf(":");
- String s = news.substring(m + 1);
- doc = txpNews.getStyledDocument();
- myAtt = new MyAttribute(s);
- doc.insertString(doc.getLength(), news.substring(0, n) + " : ",
- null);
- doc.insertString(doc.getLength(), news.substring(n + 1, m)
- + "/n", myAtt);
- txpNews.setCaretPosition(doc.getLength());
- } catch (IOException e) {
- txpNews.setText("与服务器已经断开。");
- break;
- } catch (BadLocationException e) {
- e.printStackTrace();
- }
- }
- }
- /*
- * main方法
- */
- public static void main(String[] args) {
- MyClient mc = new MyClient();
- mc.setVisible(true);
- while (mc.start == false) {// 等待客户设置服务器IP和端口
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- Thread thread = new Thread(mc);
- thread.start();
- }
- /**
- * 服务器线程类
- */
- class ServerThread extends Thread {
- Server server;
- Socket client;
- DataOutputStream out = null;
- DataInputStream in = null;
- Hashtable<String, ServerThread> clientList = null;
- String name = null;
- ServerThread(Socket client, Hashtable<String, ServerThread> clientList,
- Server frame, int count) {
- server = frame;
- this.clientList = clientList;
- this.client = client;
- name = "游客" + count;
- try {
- in = new DataInputStream(this.client.getInputStream());
- out = new DataOutputStream(this.client.getOutputStream());
- } catch (IOException e) {
- }
- }
- public void run() {
- while (true) {
- String str;
- try {
- str = in.readUTF();// 读取到该线程发送的消息,这里可以实现某些词句的屏蔽,可以使用replace方法
- this.out.writeUTF(name + ":" + str);
- // Set<Entry<String, ServerThread>> s = clientList.entrySet();
- // Iterator<Entry<String, ServerThread>> client1 = s.iterator();
- // while (client1.hasNext()) {//将该线程发送的消息转发给其他在线的人
- // ServerThread nextThread = client1.next().getValue();
- // if (nextThread.name != this.name)
- // nextThread.out.writeUTF(name + ":" + str);
- // }
- Enumeration<ServerThread> client = clientList.elements();
- while (client.hasMoreElements()) {// 将该线程发送的消息转发给其他在线的人
- ServerThread nextThread = client.nextElement();
- if (nextThread.name != this.name)
- nextThread.out.writeUTF(name + ":" + str);
- }
- } catch (IOException e) {
- server.txtConnNews.append("IP: "
- + client.getInetAddress().toString().substring(1)
- + " 已断开服务器。 " + GetDate.getDate() + "/n");
- clientList.remove(name);
- try {// 关闭流
- if (null != out)
- out.close();
- } catch (IOException e1) {
- } finally {
- try {
- if (null != in)
- in.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- } finally {
- try {
- if (null != this.client)
- this.client.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- break;
- }
- }
- }
- }