private void gross_read (){ ... System.out.println("Connect to ip:" +ip+" port:"+port); s = new Socket (ip, port); // читаем ответ buf = new byte[128*2048]; r = s.getInputStream().read(buf); String data = new String(buf, 0, r); // в коде это строка 61 System.out.println("Принято от сервера сообщение"); ... } public void run(){ while (flag){ try{ gross_read(); ... }catch(...){ ... } }
The server sends data without ceasing. The client gets everything beautifully displayed. But if the stream in which the socket is listening is interrupted, and again I try to receive data from the server, I always get that the stream is empty. Throws away the exemptone like this:
Exception in thread "Thread-6" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.checkBounds(Unknown Source) at java.lang.String.<init>(Unknown Source) at vogon_ves.GrossVis.gross_read(GrossVis.java:61) at vogon_ves.GrossVis.run(GrossVis.java:134)
What is wrong with me?
Thank you for the answers. but still the problem is relevant for me. The bottom line is graying. A server is a device, a scale. In the documentation for them, I found that they send the message to the specified port with the weight value at the specified port number xxxxx. What is the value I need to get all the time, it was clear that the dynamics of the appearance of weight on the platform. That's why I re-create a connection to the server. So I need to constantly receive messages. And this situation develops, if I do not receive a message for a short period of time, about two minutes, I can not always read the stream. And if the interval between receiving messages seconds 20 then everything is fine. about the buffer, I was initially confused by the error of going beyond the array, so I did it for more. then I found a tutorial that I read that meton gives me for ekshepshon, and I understood that it was not about the size of the buffer, but I did not reduce it back.