java编程_socket_套接字_网络编程_简易的GUI多线程聊天程序
运行效果:
===============================================================
服务器端代码:
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 }
有关于socket的简单编程基础,大家可以参考:
http://www.cnblogs.com/hongten/archive/2012/04/29/java_socket.html