未知昵称 2019-09-29 18:59 采纳率: 50%
浏览 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条)

报告相同问题?

悬赏问题

  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R