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;
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.2cto.com/kf/201204/129610.html
摘自 Hongten