首页 > 解决方案 > 我试图创建一个在java中发送和接收消息的线程但是我不确定如何解决显示的错误消息

问题描述

我试图通过创建一个允许客户端发送和接收消息的新线程来创建多线程连接。我面临的问题是我的代码中显示的所有错误,我不确定从哪里开始以及如何建立此代码以进行实际连接。感谢任何遇到并提供帮助的人。

打包发送消息;

线程 SendMessage = 新线程(新 Runnable(){

        @Override

        public void run() { 

            while (true) { 



                // read the message to deliver. 

                String msg = sc.nextLine(); 

                try { 



                    // write on the output stream 

                    dos.writeUTF(msg); 

                } catch (IOException e) { 

                    e.printStackTrace(); 

                } 

            } 

        } 

    }); 

包读消息;

线程 ReadMessage = new Thread(new Runnable() {

        @Override

        public void run() { 



            while (true) { 

                try { 



                    // read the message sent to this client 

                    String msg = dis.readUTF(); 

                    System.out.println(msg); 

                } catch (IOException e) { 



                    e.printStackTrace(); 

                } 

            } 

        } 

    }); 

标签: javamultithreadingconnection

解决方案


推荐阅读