刚写的聊天系统,拿来和大家探讨
来源:互联网 发布:搜狗输入法淘宝弹窗 编辑:程序博客网 时间:2024/05/01 02:06
功能不是很多,只是实现了所有在线用户的通话。点对点的通话已经基本想好怎么写了,但是还没有实现
下边是主要代码,希望大家一起探讨。
- /**
- * 用来设置字体属性
- *
- * @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;
- }
- }
- }
- }
- 刚写的聊天系统,拿来和大家探讨
- 成功是什么?——写的很不错的一篇文章,拿来和大家分享下.......
- 写了个连接池的类,和大家一起探讨
- java写的TCP局域网聊天系统
- 以前写的,拿来保存~
- 刚写的插入测试表的存储过程 欢迎大家测试
- session和application写的网页聊天
- 和大家探讨:轻量级笔记
- 刚写的 软件 求大家评论一下 看看 效果 后期抽空写个软件的教程
- 刚开通欢迎大家的到来
- 刚开通的博客 欢迎大家踩踩
- 自己写的博客系统,分享给大家
- 昨天刚注册的,很高兴能和大家一起进步。
- 毕业生如何从容应对校园招聘问与答(刚看到的,和大家分享下)
- 一个不过的素材网站(拿来与大家分…
- 和大家探讨一下“虚拟光驱”原理
- java自己刚学的时候做的学籍管理系统,写的很复杂
- 刚学JavaSE,自己写了一个学生的抽号小小系统。。。。
- XHTML+CSS兼容性解决方案集
- 学习ing一
- 程序员
- 一条指令结束IIS进程
- 个人日志
- 刚写的聊天系统,拿来和大家探讨
- MFC Feature Pack CMFCPropertyGridCtrl控件的应用
- 忙
- 网络学习
- 好高兴啊
- CMFCPropertySheet的使用及PROPSHEETHEADER结构体介绍
- 我的考试
- 通知
- 发的哈