日期:2014-05-20 浏览次数:20853 次
import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; public class ChatClient extends Frame { TextField tf = new TextField(); TextArea ta = new TextArea(); Socket s = null; DataOutputStream dos = null; DataInputStream dis = null; private boolean bool1 = false; public static void main(String[] args) { new ChatClient().launchFrame(); } public void launchFrame() { this.setSize(300, 300); this.setLocation(400, 300); this.add(ta, BorderLayout.NORTH); this.add(tf, BorderLayout.SOUTH); pack(); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.exit(0); } });// 匿名类,摁右上角关闭窗口功能; tf.addActionListener(new TFListener()); this.setVisible(true); connect();// 调用connect(); new Thread(new Get()).start(); } public void connect() { try { s = new Socket("127.0.0.1", 8888); System.out.println("I am connecting to the server..."); dos = new DataOutputStream(s.getOutputStream()); dis = new DataInputStream(s.getInputStream()); bool1 = true; } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void disconnect() { try { dos.close(); dis.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } } private class Get implements Runnable { public void run() { try { while(bool1) { String str = dis.readUTF(); ta.setText(str); } } catch (IOException e) { e.printStackTrace(); } } } private class TFListener implements ActionListener { public void actionPerformed(ActionEvent arg0) { String str = tf.getText(); ta.setText(str); tf.setText(""); try { dos.writeUTF(str); dos.flush(); } catch (IOException e) { e.printStackTrace(); } } } }
import java.net.*; import java.io.*; import java.util.*; public class ChatServer { List<Client>clients = new ArrayList<Client>(); public static void main(String[] args) { new ChatServer().started(); } public void started() { boolean started1 = false; ServerSocket ss = null; Socket s = null; try { ss = new ServerSocket(8888); } catch(BindException e) { System.out.println("The port has been in used,try another..."); System.exit(0); } catch(Exception e) { e.printStackTrace(); } try{ started1 = true;//建立连接后started1变为true; while(started1) { s = ss.accept(); Client c = new Client(s); System.out.println("A client is connecting to me..."); new Thread(c).start(); } } catch (IOException e) { e.printStackTrace(); } finally { try { ss.close(); } catch (IOException e1) { e1.printStackTrace(); } } } public class Client implements Runnable { private Socket s; private DataInputStream dis; private DataOutputStream dos; private boolean started2 = false; Client(Socket s) { this.s = s; started2 = true; try { dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } public void send(String str) { try { dos.writeUTF(str); dos.flush(); } catch (IOException e) { e.printStackTrace(); } } public void run() { try { while(started2) { String str; str = dis.readUTF(); System.out.println(str); for(int i = 0; i < clients.size(); i++) { Client c = clients.get(i); c.send(str); } /*for(Iterator<Client>it = clients.iterator(); it.hasNext();) { Client c = it.next(); c.send(str); }*/ /*Iterator<Client>it = clients.iterator(); while(it.hasNext()) { Client c = it.next(); c.send(str); }*/ } } catch (IOException e) { System.out.println("A client has been out..."); } finally { try { if(dis != null) dis.close(); if(dos != null) dos.close(); if(s != null) s.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } }