未知昵称 2019-09-29 18:59 采纳率: 66.7%
浏览 390
已采纳

java tcp连接服务端,每隔一秒发送一条消息,双击button开启,但是这时候界面就卡死了。怎么样才能不卡死

btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseDoubleClick(MouseEvent e) {

                String ip = "192.168.1.25"; // 服务器端ip地址
                int port = 8899;
                //Socket socketSend = null;
                try {
                    socket = new Socket(ip, port);
                    //connectStatus = true;
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                //OutputStream outputStream = null;
                try {
                    outputStream = socket.getOutputStream();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                //InputStream inputStream = null;
                try {
                    inputStream = socket.getInputStream();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                while (true) {
                        try {
                            outputStream.write(buff);
                            outputStream.flush();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }           

                        byte revBuff[] = new byte[1024];

                        try {
                            inputStream.read(revBuff);


                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        System.out.println(new String(revBuff));                
                }

            }
        });
  • 写回答

3条回答 默认 最新

  • 空白如空 2019-09-29 19:19
    关注

    要是用多线程,你可以去网上找个tcp多线程客户端来,把你代码里面的socket改为开一个新的线程来完成

    class ClientThread implements Runnable {
            private Socket client;
    
            public ClientThread(Socket client) {
                this.client = client;
            }
    
            @Override
            public void run() {
                try {
                                                outputStream = client.getOutputStream();
                                        } catch (IOException e1) {
                                                // TODO Auto-generated catch block
                                                e1.printStackTrace();
                                        }
                                        //InputStream inputStream = null;
                                        try {
                                                inputStream = client.getInputStream();
                                        } catch (IOException e1) {
                                                // TODO Auto-generated catch block
                                                e1.printStackTrace();
                                        }
    
                                        while (true) {
                                                        try {
                                                                outputStream.write(buff);
                                                                outputStream.flush();
                                                        } catch (IOException e1) {
                                                                // TODO Auto-generated catch block
                                                                e1.printStackTrace();
                                                        }
    
                                                        try {
                                                                Thread.sleep(1000);
                                                        } catch (InterruptedException e1) {
                                                                // TODO Auto-generated catch block
                                                                e1.printStackTrace();
                                                        }           
    
                                                        byte revBuff[] = new byte[1024];
    
                                                        try {
                                                                inputStream.read(revBuff);
    
    
                                                        } catch (IOException e1) {
                                                                // TODO Auto-generated catch block
                                                                e1.printStackTrace();
                                                        }
                                                        System.out.println(new String(revBuff));                
                                        }
                }
        }
    
    
    btnNewButton.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseDoubleClick(MouseEvent e) {
    
                    String ip = "192.168.1.25"; // 服务器端ip地址
                    int port = 8899;
                    //Socket socketSend = null;
                    try {
                        socket = new Socket(ip, port);
                        //connectStatus = true;
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                                 new Thread(new ClientThread(socket)).start();
                }
    }
    

    电脑没Java 环境,没测试过,是在你代码基础上改了下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境