ChatQQ

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.Iterator;
import java.util.List;

public class ChatServer {

    boolean started = 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);
            started = true;
        } catch (BindException e) {
            System.out.println("端口使用中......系统正忙!");
            System.out.println("请关掉相关程序并重新运行服务器!");
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            started = true;
            while (started) {
                boolean bConnected = false;
                Socket socket = serverSocket.accept();
                Client client = new Client(socket);
                System.out.println("a client connnected!");
                new Thread(client).start();
                clients.add(client);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    class Client implements Runnable {
        private Socket socket = null;
        private DataInputStream dataInputStream = null;
        private DataOutputStream dataOutputStream = null;
        private boolean bConnected = false;

        public Client(Socket socket) {
            this.socket = socket;
            try {
                dataInputStream = new DataInputStream(socket.getInputStream());
                dataOutputStream = new DataOutputStream(
                        socket.getOutputStream());
                bConnected = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void send(String str) throws IOException {
            dataOutputStream.writeUTF(str);
        }

        public void run() {
            Client client =  null;
            try {
                while (bConnected) {
                    String str = dataInputStream.readUTF();
System.out.println(str);
                    for(int i=0; i<clients.size(); i++){
                        client = clients.get(i);
                        client.send(str);
                    }
                }
            }catch(SocketException e){
                clients.remove(this);
                System.out.println("a client quit!");
            } catch (EOFException e) {
                System.out.println("Client closed!");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (dataInputStream != null)
                        dataInputStream.close();
                    if(dataOutputStream != null)
                        dataOutputStream.close();
                    if (socket != null){
                        socket.close();
                        socket = null;
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                
            }
        }

    }
}

==================================

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
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 dataOutputStream = null;
    DataInputStream dataInputStream = null;
    private boolean bConnected = false;
    
    TextField textField = new TextField();
    TextArea textArea = new TextArea();
    
    Thread tRecv = new Thread(new RecvThread());
    

    public static void main(String[] args) {
        new ChatClient().lauchFrame();
    }

    public void lauchFrame() {
        setLocation(400, 300);
        setSize(300, 300);
        add(textField, BorderLayout.SOUTH);
        add(textArea, BorderLayout.NORTH);
        pack();
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                disconnect();
                System.exit(0);
            }
        });
        textField.addActionListener(new TextFieldListener());
        setVisible(true);
        connect();
        
        tRecv.start();
    }
    
    
    public void connect(){
        try {
            socket = new Socket("127.0.0.1", 8888);
            dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataInputStream = new DataInputStream(socket.getInputStream());
            System.out.println("connected!");
            bConnected = true;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void disconnect(){
        try {
            dataOutputStream.close();
            dataInputStream.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private class TextFieldListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            String s = textField.getText().trim();
            textField.setText("");
            try {
                dataOutputStream.writeUTF(s);
                dataOutputStream.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        
    }
    
    private class RecvThread implements Runnable{
        public void run() {
            try {
                while(bConnected){
                    String str = dataInputStream.readUTF();
                    textArea.setText(textArea.getText() + str + "\n");
                }
            } catch (SocketException e) {
                System.out.println("退出来,bye!");
            }catch(EOFException e){
                System.out.println("退出了,bye - bye!");
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值