- import java.io.IOException;
- import java.net.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.io.*;
- public class ChatServer {
- boolean started = false;
- ServerSocket ss = null;
- List<Client> clients = new ArrayList<Client>();
- public static void main(String[] args) {
- new ChatServer().start();
- }
- public void start() {
- try {
- ss = new ServerSocket(8888);
- started = true;
- } catch (IOException ex) {
- System.out.println("Socket连接失败!端口使用中");
- ex.printStackTrace();
- }
- try {
- while (started) {
- Socket socket = ss.accept();
- Client c = new Client(socket);//将对象引用传递给Client类
- clients.add(c);
- System.out.println("a client connected!");
- new Thread(c).start(); //启用独立的线程接收客户端的消息
- }
- } catch (IOException e) {
- System.out.println("网络连接失败!");
- } finally {
- try {
- ss.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- class Client implements Runnable {
- private Socket s = null;
- private DataInputStream dis = null;
- private DataOutputStream dos = null;
- private boolean bConnected = false;
- Client(Socket s) {
- this.s = s;
- try {
- dis = new DataInputStream(s.getInputStream());
- dos = new DataOutputStream(s.getOutputStream());
- bConnected = true;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void send(String str){
- try {
- dos.writeUTF(str);//发送消息
- } catch (IOException e) {
- clients.remove(this);
- //e.printStackTrace();
- }
- }
- public void run() {
- try {
- while (bConnected) {
- String str = dis.readUTF();//接收消息
- System.out.println(str);
- for(int i = 0;i < clients.size(); i++) {
- Client c = clients.get(i);
- c.send(str);
- }
- }
- } catch (EOFException e) {
- System.out.println("Client closed!");
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (dis != null) dis.close();
- if(dos != null) dos.close();
- if (s != null) s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
客户端
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.Socket;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- public class ChatClient extends Frame {
- private DataOutputStream dos = null;
- private DataInputStream dis = null;
- private boolean bConnected = false;
- Socket s = null;
- private TextField tfText = new TextField();
- private TextArea caContent = new TextArea();
- Thread tRecv = new Thread(new Receive());//接收消息的线程
- public static void main(String[] args) {
- new ChatClient().frameLaunch();
- }
- private void frameLaunch() {
- this.setLocation(500, 500);
- tfText.addActionListener(new TFListener());
- add(tfText, BorderLayout.SOUTH);
- add(caContent, BorderLayout.NORTH);
- // this.setSize(400,200); --与pack()同时使用时无效
- pack();
- this.addWindowListener(new WindowAdapter() {//关闭窗口
- public void windowClosing(WindowEvent arg0) {
- disConnected();//关闭窗口前先关闭连接
- System.exit(0);
- }
- });
- setVisible(true);
- connected();
- tRecv.start();
- }
- public void connected() {
- try {
- s = new Socket("127.0.0.1", 8888);
- dos = new DataOutputStream(s.getOutputStream());
- dis = new DataInputStream(s.getInputStream());
- bConnected = true;//标记已连接上
- System.out.println("connected successfully!");
- } catch (UnknownHostException e) {
- System.out.println("服务器未启动!");
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void disConnected() {
- /*
- try {
- bConnected = false;
- tRecv.join(); //在Receive中仍然可能陷入死循环
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- try {
- dos.close();
- dis.close();
- s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- */
- try {
- dos.close();
- dis.close();
- s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private class TFListener implements ActionListener {//内部类,不需要外部调用
- public void actionPerformed(ActionEvent e) {
- String str = tfText.getText().trim();
- tfText.setText("");
- sendMessage(str);
- }
- }
- private void sendMessage(String str) {
- try {
- dos.writeUTF(str);
- dos.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- class Receive implements Runnable {
- public void run() {
- try {
- while(bConnected){
- String str = dis.readUTF();
- System.out.println(str);
- caContent.setText(caContent.getText() + str + "\n");
- }
- } catch (SocketException e) {
- System.out.println("退出了,bye!");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
转载于:https://blog.51cto.com/huamm/1091016