博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java_socket套接字网络编程_实现多线程聊天
阅读量:6348 次
发布时间:2019-06-22

本文共 6370 字,大约阅读时间需要 21 分钟。

运行效果:

===============================================================

服务器端代码:

1 package com.b510.socket1706.gui.freeechatroom;  2   3 import java.io.*; 4 import java.net.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 8 /** 9 * 服务器端程序 10 * 11 * @author Hongten 12 * 13 * @time 2012-4-29 2012 14 */ 15 public class TestServer { 16 private ServerSocket ss; 17 private Socket s; 18 private DataInputStream dis; 19 private DataOutputStream dos; 20 private TextArea ta; 21 private TextField tf; 22 23 public static void main(String args[]) { 24 TestServer ts = new TestServer(); 25 ts.createUI(); 26 ts.connect(); 27 ts.createThread(); 28 } 29 30 public void connect() { 31 try { 32 ss = new ServerSocket(8888); 33 s = ss.accept(); 34 dis = new DataInputStream(s.getInputStream()); 35 dos = new DataOutputStream(s.getOutputStream()); 36 } catch (IOException e) { 37 e.printStackTrace(); 38 // 13701303436 39 40 } 41 } 42 43 public void createUI() { 44 Frame f = new Frame("Server"); 45 ta = new TextArea(); 46 tf = new TextField(); 47 Button send = new Button("send"); 48 Panel p = new Panel(); 49 p.setLayout(new BorderLayout()); 50 p.add(tf, "Center"); 51 p.add(send, "East"); 52 f.add(ta, "Center"); 53 f.add(p, "South"); 54 MyServerListener listener = new MyServerListener(this); 55 send.addActionListener(listener); 56 tf.addActionListener(listener); 57 f.addWindowListener(new WindowAdapter() { 58 public void windowClosing(WindowEvent e) { 59 System.exit(0); 60 } 61 }); 62 f.setSize(400, 400); 63 f.setVisible(true); 64 } 65 66 public void createThread() { 67 MyServerReader reader = new MyServerReader(this); 68 reader.start(); 69 } 70 71 public void close() { 72 try { 73 dis.close(); 74 dos.close(); 75 s.close(); 76 ss.close(); 77 } catch (IOException e) { 78 e.printStackTrace(); 79 } 80 } 81 82 public DataInputStream getDataInputStream() { 83 return dis; 84 } 85 86 public DataOutputStream getDataOutputStream() { 87 return dos; 88 } 89 90 public TextArea getTextArea() { 91 return ta; 92 } 93 94 public TextField getTextField() { 95 return tf; 96 } 97 } 98 99 class MyServerListener implements ActionListener { 100 private TestServer server; 101 102 public MyServerListener(TestServer server) { 103 this.server = server; 104 } 105 106 public void actionPerformed(ActionEvent e) { 107 TextField tf = server.getTextField(); 108 String info = tf.getText(); 109 server.getTextArea().append("自己说: " + info + "\n"); 110 try { 111 server.getDataOutputStream().writeUTF(info); 112 } catch (IOException e1) { 113 e1.printStackTrace(); 114 } 115 if (info.equals("bye")) { 116 server.close(); 117 System.exit(0); 118 } 119 tf.setText(""); 120 tf.requestFocus(); 121 } 122 } 123 124 class MyServerReader extends Thread { 125 private TestServer server; 126 127 public MyServerReader(TestServer server) { 128 this.server = server; 129 } 130 131 public void run() { 132 String info; 133 DataInputStream dis = server.getDataInputStream(); 134 TextArea ta = server.getTextArea(); 135 try { 136 while (true) { 137 info = dis.readUTF(); 138 ta.append("对方说: " + info + "\n"); 139 if (info.equals("bye")) { 140 server.close(); 141 System.exit(0); 142 } 143 } 144 } catch (IOException e) { 145 e.printStackTrace(); 146 } 147 } 148 }

客户端代码:

1 package com.b510.socket1706.gui.freeechatroom;  2   3 import java.io.*; 4 import java.net.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 /** 8 * 客户端程序 9 * @author Hongten 10 * 11 * @time 2012-4-29 2012 12 */ 13 public class TestClient { 14 private Socket s; 15 private DataInputStream dis; 16 private DataOutputStream dos; 17 private TextArea ta; 18 private TextField tf; 19 20 public static void main(String args[]) { 21 TestClient tc = new TestClient(); 22 tc.createUI(); 23 tc.connect(); 24 tc.createThread(); 25 } 26 27 public void connect() { 28 try { 29 s = new Socket("127.0.0.1", 8888); 30 dos = new DataOutputStream(s.getOutputStream()); 31 dis = new DataInputStream(s.getInputStream()); 32 } catch (IOException e) { 33 e.printStackTrace(); 34 } 35 } 36 37 public void createUI() { 38 Frame f = new Frame("Client"); 39 ta = new TextArea(); 40 tf = new TextField(); 41 Button send = new Button("send"); 42 Panel p = new Panel(); 43 p.setLayout(new BorderLayout()); 44 p.add(tf, "Center"); 45 p.add(send, "East"); 46 f.add(ta, "Center"); 47 f.add(p, "South"); 48 MyClientListener listener = new MyClientListener(this); 49 send.addActionListener(listener); 50 tf.addActionListener(listener); 51 f.addWindowListener(new WindowAdapter() { 52 public void windowClosing(WindowEvent e) { 53 System.exit(0); 54 } 55 }); 56 f.setSize(400, 400); 57 f.setLocation(600, 0); 58 f.setVisible(true); 59 } 60 61 public void createThread() { 62 MyClientReader reader = new MyClientReader(this); 63 reader.start(); 64 } 65 66 public void close() { 67 try { 68 dis.close(); 69 dos.close(); 70 s.close(); 71 } catch (IOException e) { 72 e.printStackTrace(); 73 } 74 } 75 76 public DataInputStream getDataInputStream() { 77 return dis; 78 } 79 80 public DataOutputStream getDataOutputStream() { 81 return dos; 82 } 83 84 public TextArea getTextArea() { 85 return ta; 86 } 87 88 public TextField getTextField() { 89 return tf; 90 } 91 } 92 93 class MyClientListener implements ActionListener { 94 95 private TestClient client; 96 97 public MyClientListener(TestClient client) { 98 this.client = client; 99 } 100 101 public void actionPerformed(ActionEvent e) { 102 TextField tf = client.getTextField(); 103 String info = tf.getText(); 104 client.getTextArea().append("自己说: " + info + "\n"); 105 try { 106 client.getDataOutputStream().writeUTF(info); 107 } catch (IOException e1) { 108 e1.printStackTrace(); 109 } 110 if (info.equals("bye")) { 111 client.close(); 112 System.exit(0); 113 } 114 tf.setText(""); 115 tf.requestFocus(); 116 } 117 } 118 119 class MyClientReader extends Thread { 120 private TestClient client; 121 122 public MyClientReader(TestClient client) { 123 this.client = client; 124 } 125 126 public void run() { 127 String info; 128 DataInputStream dis = client.getDataInputStream(); 129 TextArea ta = client.getTextArea(); 130 try { 131 while (true) { 132 info = dis.readUTF(); 133 ta.append("对方说: " + info + "\n"); 134 if (info.equals("bye")) { 135 client.close(); 136 System.exit(0); 137 } 138 } 139 } catch (IOException e) { 140 e.printStackTrace(); 141 } 142 } 143 }

转载于:https://www.cnblogs.com/zyn1990/p/4024836.html

你可能感兴趣的文章
在开发中处理海量数据的方法 思路
查看>>
datatable 获取最大值
查看>>
sqlserver2012一直显示正在还原(Restoring)和从单用户转换成多用户模式(单用户连接中)...
查看>>
spark复习总结02
查看>>
李瑞红201771010111《第九周学习总结》
查看>>
[译]ZOOKEEPER RECIPES-Barriers
查看>>
navicat下载安装和激活一分钟完成
查看>>
6_5 一些有用网址
查看>>
NFC 鏈表操作
查看>>
pymongo模块
查看>>
第0次作业
查看>>
思维导图五个关键秘诀
查看>>
Ubuntu里设置python默认版本为python3(转载)
查看>>
快排+折半查找
查看>>
c# GC 新典型
查看>>
ssh bash 通配符
查看>>
seajs在jquery多个版本下引用jquery的插件的方案
查看>>
关于网络上java,php和.net的“口角之争“的一点想法 !
查看>>
python 第二周(第十三天) 我的python成长记 一个月搞定python数据挖掘!(21) -正则表达式re...
查看>>
[POI2011]SEJ-Strongbox
查看>>