一个Java实现的简单的多个客户端聊天程序

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

客户端

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.net.*;

public class HeartClient extends Frame {

    /*
     *  成员方法出场...
     */
    private TextField tfText;
    private TextArea taContent;
    private Socket s;
    private DataOutputStream dos;
    private DataInputStream dis;

    /**
     * 注意,入口... ^^
     * @param args
     */
    public static void main(String[] args) {
        new HeartClient().launchFrame();

    }

    /**
     * Loading GU
     */
    public void launchFrame(){
        tfText = new TextField();
        taContent = new TextArea();
        this.setSize(300,300);
        this.setLocation(300,300);
        this.tfText.addActionListener(new TFListener());
        this.add(tfText,BorderLayout.SOUTH);
        this.add(taContent,BorderLayout.NORTH);
        this.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }});
        this.pack();
        this.connect();
        this.setVisible(true);
    }

    /**
     * 我在努力地连接服务器中...
     */
    public void connect() {
        try {
            s = new Socket("127.0.0.1",1720);
            dos = new DataOutputStream(s.getOutputStream());
            dis = new DataInputStream(s.getInputStream());
            new Thread(new SendThread()).start();
//          dos.writeUTF("Hello,i find u!");
        } catch (UnknownHostException e) {
            System.out.println("UnknownHostException");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOException");
            e.printStackTrace();
        }finally{
            //关闭啥尼...
        }

    }

    /**
     * 额不会傻等着tfText(TextField tfText的监听器类)
     */
    class TFListener implements ActionListener{
        private String str;
        @Override
        public void actionPerformed(ActionEvent e) {
            str = tfText.getText().trim();
            tfText.setText("");
            try {
                dos.writeUTF(str);
            } catch (IOException e1) {
                System.out.println("IOException");
                e1.printStackTrace();
            }
        }

    }

    /**
     * 客户端接收消息的线程呦...
     *
     */
    class SendThread implements Runnable{
        private String str;
        private boolean iConnect = false;

        public void run(){
            iConnect = true;
            recMsg();

        }

        /**
         * 消息,看招,哪里跑...(客户端接收消息的实现)
         * @throws IOException 
         */
        public void recMsg() {
            try {
                while(iConnect){
                    str = dis.readUTF();
                    taContent.setText(str);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

}
                                服务器端
import java.io.*;
import java.net.*;
import java.util.*;

public class HeartServer {

    /*
     * 成员变量闪亮登场
     */
    List<ClientThread> clients = new ArrayList<ClientThread>();

    /**
     * 这系入口啊,向这里看齐...
     * @param args
     */
    public static void main(String[] args) {
        new HeartServer().start();
    }

    /**
     * 启动服务器中...
     *
     */
    public void start(){
        try {
            boolean iConnect = false;
            ServerSocket ss = new ServerSocket(1720);
            iConnect = true;
            while(iConnect){
System.out.println("绑定服务器端口成功!");
                Socket s = ss.accept();
                ClientThread currentClient = new ClientThread(s);//创建线程引用
System.out.println("发现客户端!");
                clients.add(currentClient);//把当前客户端加入集合
                new Thread(currentClient).start();
System.out.println("客户端进程已经启动!");
            }
        } catch (IOException e) {
            System.out.println("IOException");
            e.printStackTrace();
        }
    }

    /**
     * 给每个客户端留个家(客户端的进程)
     *
     */
    class ClientThread implements Runnable {
        /*
         * 成员变量又来啦...
         */
        private Socket s;
        private DataInputStream dis;
        private DataOutputStream dos;
        private String str;
        private boolean iConnect = false;

        /**
         * 小构一下
         */
        ClientThread(Socket s){
            this.s = s;
            iConnect = true;
        }

        public void run(){
System.out.println("run方法启动了!");
            try {

                while(iConnect){
System.out.println("RUN方法中的while循环启动,正在等待客户端的发送消息...");
                    dis = new DataInputStream(s.getInputStream());
                    str = dis.readUTF();
System.out.println(str);
                    for(int i=0; i<clients.size(); i++){
System.out.println("转发消息中..."+i);
                        ClientThread c = clients.get(i);
                        c.sendMsg(str);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        /**
         * 转发消息,我做主...
         * 将送至服务器的消息发送给每个连接到的客户端
         */
        public void sendMsg(String str){
            try {
System.out.println("创建输出管道!");
                dos = new DataOutputStream(this.s.getOutputStream());
System.out.println("正在向客户端写消息!");
                dos.writeUTF(str);
System.out.println("正在向客户端写消息成功!");         
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

}

标签: 服务器 服务器端

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:C++算法之二叉树深度遍历

下一篇:jquery 获取浏览器窗口的可视区域高度 宽度 滚动条高