1

I used code below for receiving messages from a server but it only gets one message like "Hello," but the second message which is "How are you?" is not detected by the application. I tried to fix it but I was not able to.

However, there is not any error.

Here is my code:

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();

Thanks in advance!

EDIT:

I tried code below and I got that need to invoke this thread manually by a button which update text view do you have any solution for this? in order to automate it.

 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();
A Farmanbar
  • 4,381
  • 5
  • 24
  • 42
  • 1
    A not, not a solution: You are using ``available()`` to allocate a buffer, you shouldn't do that, take a look at [the method's javadoc](https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available()). – f1sh Mar 21 '17 at 21:50
  • @f1sh,thanks i will check it out. – A Farmanbar Mar 21 '17 at 21:50

1 Answers1

1

It does what it is told to. Your code tells to receive only one message. try new Thread().start(); after showMSG(). Hope this helps.

solution

 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();
Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
  • lemme try your solution. – A Farmanbar Mar 21 '17 at 21:59
  • Your Solution was somehow correct,it keep listening for new msg but doesn't show whole received msgs.using available() is incorrect.by the way i voted you up. – A Farmanbar Mar 21 '17 at 22:08
  • Thank you very much."doesn't show whole received " can you elaborate ? – Bertram Gilfoyle Mar 21 '17 at 22:17
  • it only shows new message like "how are you?" while i do re initialize socket and re connect to host . – A Farmanbar Mar 21 '17 at 22:18
  • I must used another solution instead of using available() method .because it does not works always correct. – A Farmanbar Mar 21 '17 at 22:21
  • I don't understand. You said the second message was just "How are you?" and you are receiving it. Can you please let me know what you are supposed to be received ? – Bertram Gilfoyle Mar 21 '17 at 22:24
  • I mean next message which is "how are you?" only be shown when i re connect to host and doesn't appear in first connection.i don't know why !!! – A Farmanbar Mar 21 '17 at 22:27
  • Check out this thread [link](http://stackoverflow.com/questions/3038176/httpurlconnection-does-not-read-the-whole-response). Also please consider marking the answer as accepted as your major issue is solved.Thanks. :-) – Bertram Gilfoyle Mar 21 '17 at 22:33