You shouldn't do this: byte[] response = new byte[is.available()];
>From the javadocs for InputStream: https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available() > > Note that while some implementations of InputStream will return the total > number of bytes in the stream, many will not. It is never correct to use > the return value of this method to allocate a buffer intended to hold all > data in this stream. Steve On Fri, Jul 14, 2017 at 12:53 PM, <[email protected]> wrote: > > import ca.weblite.codename1.net.Socket; > import java.io.InputStream; > import java.io.OutputStream; > > public static int socketReadRetries = 3; > public static int socketTimeOut = 3000; > public static Socket mySocket = null; > > String CMD = "pingFromMobile~END~"; > Object obj = null; > mySocket = new Socket(host, port, socketTimeOut); > OutputStream os = mySocket.getOutputStream(); > os.write(frame(CMD.getBytes())); > > > for (int a = 0; a < socketReadRetries; a++) { > > is = mySocket.getInputStream(); > > byte[] response = new byte[is.available()]; > > if (response.length == 0) { > > Thread.sleep(200); > > continue; > > } > > try { > > for (int b = 0; b < response.length; b++) { > > response[b] = (byte) is.read(); > > } > > obj = response; > > } catch (Exception h) { > > h.printStackTrace(); > > if (mySocket != null) { > > mySocket.close(); > > } > > return null; > > } > > break; > > } > > if (is != null) { > > is.close(); > > } > > if (mySocket != null && !mySocket.isClosed()) { > > mySocket.close(); > > } > > > On Thursday, July 13, 2017 at 11:05:45 AM UTC-4, Steve Hannah wrote: >> >> Can you share a little bit more of your client source code. It's hard to >> comment without seeing more. >> >> On Thu, Jul 13, 2017 at 4:44 AM, <[email protected]> wrote: >> >>> My app opens a socket, sets a timeout does some transaction, (typically >>> request data from the server) closes the connection and processed the >>> request >>> I don't see an exposed method to set timeout in the builtin socket api >>> >>> The lib version (https://github.com/shannah/CN1Sockets) works very well >>> for my needs. >>> >>> Please advise >>> >>> On Thursday, July 13, 2017 at 12:30:56 AM UTC-4, Shai Almog wrote: >>> >>>> I don't know, I only use our builtin socket api and not the lib. >>>> Maybe Steve knows. >>>> >>>> Closing a socket after every message will make your connection very >>>> slow. You might as well use HTTPS which is generally better. I strongly >>>> recommend avoiding sockets. >>>> >>>> -- >>> You received this message because you are subscribed to the Google >>> Groups "CodenameOne Discussions" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> Visit this group at https://groups.google.com/grou >>> p/codenameone-discussions. >>> To view this discussion on the web visit https://groups.google.com/d/ms >>> gid/codenameone-discussions/52b4daf2-fa2d-4d86-94de-4cd53c1b >>> 0711%40googlegroups.com >>> <https://groups.google.com/d/msgid/codenameone-discussions/52b4daf2-fa2d-4d86-94de-4cd53c1b0711%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> >> >> -- >> Steve Hannah >> Software Developer >> Codename One >> http://www.codenameone.com >> > -- > You received this message because you are subscribed to the Google Groups > "CodenameOne Discussions" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > Visit this group at https://groups.google.com/ > group/codenameone-discussions. > To view this discussion on the web visit https://groups.google.com/d/ > msgid/codenameone-discussions/af805466-72b8-456f-b05f- > 94441161cc47%40googlegroups.com > <https://groups.google.com/d/msgid/codenameone-discussions/af805466-72b8-456f-b05f-94441161cc47%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- Steve Hannah Software Developer Codename One http://www.codenameone.com -- You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. Visit this group at https://groups.google.com/group/codenameone-discussions. To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/CAGOYrKXxV%3D-aY9eEZnEhZcWRCYioYcv%2B-iL2UP%2BR_Xwc8ws%2Bhw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
