java - TCP 套接字无法接收数据包

标签 java android runnable

我使用下面的代码从服务器接收消息,但它只收到一条消息,如“你好”,但第二条消息是“你好吗?”未被应用程序检测到。我试图修复它,但无法修复。

但是,没有任何错误。

这是我的代码:

new Thread(new Runnable() {
                    @Override
                    public void run() {
                        int available = 0;
                        while (true) {
                            try {
                                available = ClientInPutStream.available();
                                if (available > 0) {
                                    break;
                                }
                            } catch (IOException e) {
                                msg = e.toString();
                                showMSG();
                            }
                        }
                        try {
                            char[] ServerMSG = new char[available];
                            Reader.read(ServerMSG, 0, available);
                            StringBuilder sb = new StringBuilder();
                            sb.append(ServerMSG, 0, available);
                            msg = sb.toString();
                            showMSG();
                        } catch (IOException e) {
                            msg = e.toString();
                            showMSG();
                        }
                    }
                }).start();

提前致谢!

编辑:

我尝试了下面的代码,我发现需要通过更新 TextView 的按钮手动调用这个线程,您对此有什么解决方案吗? 为了使其自动化。

 new Thread(new Runnable() {
                    @Override
                    public void run() {
                        byte[] buffer = new byte[128];  // buffer store for the stream
                        int bytes; // bytes returned from read()
                        try {
                            bytes = ClientInPutStream.read(buffer);
                            byte[] readBuf =  buffer;
                            String strIncom = new String(readBuf, 0, bytes);                 
                            msg2+=strIncom;
                            showmsg();
                        }catch (Exception e){msg=e.toString();showError();}
                        }

                }).start();

最佳答案

它会做它被告知的事情。您的代码告诉您只接收一条消息。在 showMSG() 之后尝试 new Thread().start();。希望这会有所帮助。

解决方案

 new Thread(new Runnable() {

          @Override
          public void run() {

               byte[] buffer = new byte[128];  // buffer store for the stream
               int bytes; // bytes returned from read()

               try {
                     while (true){ // continuously read buffer
                          bytes = ClientInPutStream.read(buffer);
                          byte[] readBuf = buffer;
                          String strIncom = new String(readBuf, 0, bytes);                 // create string from bytes array
                          msg2 += strIncom;
                          showmsg();
                     }
                }catch (Exception e){msg=e.toString();showError();}
          }

 }).start();

关于java - TCP 套接字无法接收数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42938679/

相关文章:

Android VM 不允许我们分配 xx 字节

java - 相同的 Runnable 类被两次传递给同时运行两个/多个线程的 ExecutorService

java - 为 DataInputStream 手动设置超时

java - 如何否定包含范围的正则表达式?

java - 适合 2D 平台游戏的关卡表示/数据结构?

javascript - 为什么 PhoneGap 的性能 WebGL 与 firefox 或 chrome 不同?

java - 通过 Groovy 脚本设置 Jenkins 环境变量

android - 在 Jellybean 中,相机 Intent 在使用 FileProvider 时返回 RESULT.CANCELLED

java - 你应该同步运行方法吗?为什么或者为什么不?

java - 线程创建现象和启动线程之间的区别