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 ChatClient extends Frame pri
2、vate 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 void main(String args) ChatClient cc = new ChatC
3、lient();cc.launchFrame();cc.Listen();public void launchFrame() try s = new Socket(InetAddress.getByName(localhost), 8888);connected = true;dos = new DataOutputStream(s.getOutputStream();dis = new DataInputStream(s.getInputStream(); catch (IOException ioe) System.out.println(服务器连接失败!);ioe.printStackT
4、race();this.tfWords = new TextField();this.taShow = new TextArea();this.setTitle(Chatroom Client);this.setLocation(700, 50);this.add(tfWords, BorderLayout.SOUTH);this.add(taShow, BorderLayout.NORTH);this.setSize(200, 250);this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEve
5、nt e) try connected = false;dos.writeUTF(#Exit#);dos.flush();close();System.exit(0); catch (IOException ioe) System.out.println(消息发送出错!);ioe.printStackTrace(););this.setVisible(true);this.tfWords.addActionListener(new TFWordsListener();public void Listen() new Thread(new ListenThread().start();publi
6、c void close() 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 Runnablepublic void run()while (connected) try String back = dis.readUTF(); /阻塞IOtaShow.append(back
7、);catch(SocketException se)System.out.println(谢谢使用!);catch (IOException e) System.out.println(数据丢失!);e.printStackTrace();private class TFWordsListener implements ActionListener public void actionPerformed(ActionEvent e) String words = tfWords.getText();tfWords.setText();try dos.writeUTF(words+ n); c
8、atch (IOException ioe) System.out.println(发送出错!);ioe.printStackTrace();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 clients = new ArrayList();private boolean s
9、tarted = 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(-1);public void start() try Socket s = this.getSs().accept();System.out
10、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 (IOException e) System.out.println(连接错误!);e.printStackTrace();public boolean isStarted() return started;public void setStarted(boolean started) this.star
11、ted = started;public ServerSocket getSs() return ss;public void setSs(ServerSocket ss) this.ss = ss;public List getClients() return clients;public void setClients(List clients) this.clients = clients;public static void main(String args) ChatServer cs = new ChatServer();while (cs.isStarted() cs.start
12、);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;public void run() InetAddress ip = null;int po
13、rt = 9999;try if (connected) dis = new DataInputStream(s.getInputStream();dos = new DataOutputStream(s.getOutputStream();ip = s.getInetAddress();port = s.getPort();while (connected) String line = dis.readUTF();if (line.equals(#Exit#) getClients().remove(this); / 当接收到推出的消息,移除客户端记录line = Bye-Bye!;conn
14、ected = false;System.out.println(From: ( + ip.toString() + : + port+ ): + line);for (int i = 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();