1、import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import .*; public class ChatClie
2、nt extends Frame { private boolean connected = false; private DataOutputStream dos = null; private DataInputStream dis = null; private static final long serialVersionUID = 1L; private Socket s = null; private TextField tfWords = null; private TextArea taShow = null; public static v
3、oid main(String args[]) { ChatClient cc = new ChatClient(); cc.launchFrame(); cc.Listen(); } public void launchFrame() { try { s = new Socket(InetAddress.getByName("localhost"), 8888); connected = true; dos = new DataOutputStream(s.getOutputStream()); dis = new Data
4、InputStream(s.getInputStream()); } catch (IOException ioe) { System.out.println("服务器连接失败!"); ioe.printStackTrace(); } this.tfWords = new TextField(); this.taShow = new TextArea(); this.setTitle("Chatroom Client"); this.setLocation(700, 50); this.add(tfWords, BorderLayout
5、SOUTH); this.add(taShow, BorderLayout.NORTH); this.setSize(200, 250); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { connected = false; dos.writeUTF("###Exit###"); dos.flush(); close(); System.exit(0);
6、 } catch (IOException ioe) { System.out.println("消息发送出错!"); ioe.printStackTrace(); } } }); this.setVisible(true); this.tfWords.addActionListener(new TFWordsListener()); } public void Listen() { new Thread(new ListenThread()).start(); } public void close
7、) { try { if (dos != null) dos.close(); if (s != null) s.close(); } catch (IOException ioe) { ioe.printStackTrace(); System.out.println("客户端断开时出错!"); System.exit(-1); } } public class ListenThread implements Runnable{ public void run(){ while (conne
8、cted) { try { String back = dis.readUTF(); //阻塞IO taShow.append(back); }catch(SocketException se){ System.out.println("谢谢使用!"); }catch (IOException e) { System.out.println("数据丢失!"); e.printStackTrace(); } } } } private class T
9、FWordsListener implements ActionListener { public void actionPerformed(ActionEvent e) { String words = tfWords.getText(); tfWords.setText(""); try { dos.writeUTF(words+ "\n"); } catch (IOException ioe) { System.out.println("发送出错!"); ioe.printStackTrace(); }
10、
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import .*;
import java.util.List;
import java.util.ArrayList;
public class ChatServer {
private List
11、); private boolean started = false; private ServerSocket ss = null; private Client c = null; public ChatServer() { try { ss = new ServerSocket(8888); started = true; } catch (IOException ioe) { System.out.println("对不起,服务器不能启动!"); ioe.printStackTrace(); System.exit
12、1); } } public void start() { try { Socket s = this.getSs().accept(); System.out.println("Here comes a client! "); if (s != null) { c = this.new Client(s, true); this.clients.add(c); new Thread(c).start(); // 线程始终没能停止!!!? c = null; } } catch (IOEx
13、ception e) { System.out.println("连接错误!"); e.printStackTrace(); } } public boolean isStarted() { return started; } public void setStarted(boolean started) { this.started = started; } public ServerSocket getSs() { return ss; } public void setSs(ServerSocket
14、ss) {
this.ss = ss;
}
public List
15、 } } private class Client implements Runnable { private boolean connected = false; private Socket s = null; private DataInputStream dis = null; private DataOutputStream dos = null; public Client(Socket s, boolean connected) { this.s = s; this.connected = connected;
16、 } public void run() { InetAddress ip = null; int port = 9999; try { if (connected) { dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); ip = s.getInetAddress(); port = s.getPort(); } while (connect
17、ed) { String line = dis.readUTF(); if (line.equals("###Exit###")) { getClients().remove(this); // 当接收到推出的消息,移除客户端记录 line = "Bye-Bye!"; connected = false; } System.out.println("From: (" + ip.toString() + ":" + port + "): " + line); for (int i
18、 = 0; i < getClients().size(); i++) { if (this.equals(getClients().get(i))) { continue; } else { getClients().get(i).dos.writeUTF("From: (" + ip.toString() + ":" + port + "): " + line + "/n"); } } } } catch (IOException ioe) { System.out.println("客户端连接出错!"); ioe.printStackTrace(); } } } }






