import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.IOException; import java.net.BindException; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.ArrayList; import java.util.List; public class ChatServer { boolean bStarted = false; ServerSocket serverSocket = null; List<Client> clients = new ArrayList<Client>(); public static void main(String[] args) { new ChatServer().start(); } public void start() { try { serverSocket = new ServerSocket(8888); bStarted = true; } catch (BindException e) { System.out.println("端口使用中..."); System.out.println("请关闭相关程序再运行服务器!"); System.exit(0); } catch (IOException e) { e.printStackTrace(); } try { while (bStarted) { Socket socket = serverSocket.accept(); Client client = new Client(socket); System.out.println("a client connected!"); new Thread(client).start(); clients.add(client); // dInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } finally { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } class Client implements Runnable { private Socket socket; private DataInputStream dInputStream = null; private boolean bConnected = false; private DataOutputStream dOutputStream = null; public Client(Socket s) { this.socket = s; try { dInputStream = new DataInputStream(s.getInputStream()); dOutputStream = new DataOutputStream(s.getOutputStream()); bConnected = true; } catch (IOException e) { e.printStackTrace(); } } public void send(String str) { try { dOutputStream.writeUTF(str); } catch (IOException e) { clients.remove(this); System.out.println("this quit from list"); //e.printStackTrace(); } } public void run() { //Client client = null; try { while (bConnected) { String str = dInputStream.readUTF(); System.out.println(str); for (int i = 0; i < clients.size(); i++) { Client client = clients.get(i); client.send(str); } } } catch (EOFException e) { System.out.println("Client clossed!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (dInputStream != null) dInputStream.close(); if (dOutputStream != null) dOutputStream.close(); if (socket != null) socket.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } }
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.EOFException; import java.io.IOException; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; public class ChatClient extends Frame { Socket socket = null; DataOutputStream dOutputStream = null; DataInputStream dInputStream = null; private boolean bConnected = false; TextField tField = new TextField(); TextArea tContent = new TextArea(); Thread tRecv = new Thread(new RecvTread()); public static void main(String[] args) { new ChatClient().launchFrame(); } public void launchFrame() { this.setLocation(400, 300); this.setSize(300, 300); add(tField, BorderLayout.SOUTH); add(tContent, BorderLayout.NORTH); pack(); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { disConnect(); System.exit(0); } }); tField.addActionListener(new TextFieldListener()); this.setVisible(true); connect(); tRecv.start(); } public void connect() { try { socket = new Socket("127.0.0.1", 8888); dOutputStream = new DataOutputStream(socket.getOutputStream()); dInputStream = new DataInputStream(socket.getInputStream()); bConnected = true; System.out.println("connected"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void disConnect() { try { dOutputStream.close(); dInputStream.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } /* * try { bConnected = false; tRecv.join(); }catch (InterruptedException * e) { e.printStackTrace(); } finally{ try { dOutputStream.close(); * dInputStream.close(); socket.close(); } catch (IOException e) { * e.printStackTrace(); } } */ } private class TextFieldListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String str = tField.getText().trim(); // tContent.setText(str); tField.setText(""); try { dOutputStream.writeUTF(str); dOutputStream.flush(); } catch (IOException e1) { e1.printStackTrace(); } } } private class RecvTread implements Runnable { public void run() { try { while (bConnected) { String str = dInputStream.readUTF(); tContent.setText(tContent.getText() + str + "\n"); } } catch (SocketException e) { System.out.println("退出"); } catch (EOFException e) { System.out.println("退出了,8"); } catch (IOException e) { e.printStackTrace(); } } } }