No further communication possible after ConnectionNotFoundEcxeption | Thales IoT Developer Community
February 7, 2017 - 9:24am, 2171 views
Hello!
I'm using a EHS5T-RS485 to send data in ~5 minute intervals to a server. After some hours, this Exception occurs when closing the output stream:
- javax.microedition.io.ConnectionNotFoundException: ConnectionNotFound error in socket::open : error = -137\n
- - com.sun.midp.io.j2me.https.Protocol.connect(), bci=351
- - com.sun.midp.io.j2me.http.Protocol.streamConnect(), bci=108
- - com.sun.midp.io.j2me.http.Protocol.startRequest(), bci=7
- - com.sun.midp.io.j2me.http.Protocol.sendRequest(), bci=33
- - com.sun.midp.io.j2me.http.Protocol.sendRequest(), bci=3
- - com.sun.midp.io.j2me.http.Protocol.closeOutputStream(), bci=1
- - com.sun.midp.io.BaseOutputStream.close(), bci=5
- - java.io.DataOutputStream.close(), bci=9
- - xx.yyy.zzzzz.postViaHttpConnection(zzzzz.java:69)
- ...
- - java.lang.Thread.run(), bci=5
This exception occurs with every following message which is sent to the server, so no further communication is possible.
ATI1 Output:
- Cinterion
- EHS5-E
- REVISION 03.001
- A-REVISION 00.000.42
Here the code skeleton. The "send-in-separate-thread" based design is chosen help against hanging connections.
protected synchronized void postViaHttpConnection() throws Exception { Transmitter trans = new Transmitter(); Thread t = new Thread(trans); t.start(); // ************* Transmit *************** final int SLEEPINTERVAL = 100; // ************* Close transmission after ***. 60 seconds *************** for (int i = 0; i < 60000; i += SLEEPINTERVAL) { if (trans.done) { break; } sleep(SLEEPINTERVAL); } if (trans.inputstream != null) { try { sleep(100); trans.inputstream.close(); } catch (Exception ex) { ex.printStackTrace(); } } if (trans.outputstream != null) { try { sleep(100); trans.outputstream.close(); } catch (Exception ex) { ex.printStackTrace(); } } if (trans.connection != null) { try { sleep(100); trans.connection.close(); } catch (Exception ex) { ex.printStackTrace(); } } if (trans.exception != null) { throw trans.exception; } } private class Transmitter implements Runnable { volatile boolean done = false; volatile HttpConnection connection = null; volatile DataInputStream inputstream = null; volatile DataOutputStream outputstream = null; volatile Exception exception = null; public void run() { int rc; try { if (!url.startsWith("https:")) { connection = (HttpConnection) Connector.open(...); } else { connection = (HttpsConnection) Connector.open(...); } Thread.sleep(100); // Set the request method and headers connection.setRequestMethod(HttpConnection.POST); connection.setRequestProperty("Content-Type", ...); connection.setRequestProperty(...); connection.setRequestProperty(...); outputstream = connection.openDataOutputStream(); outputstream.write(...); outputstream.flush(); Thread.sleep(100); inputstream = connection.openDataInputStream(); rc = connection.getResponseCode(); if (rc != HttpConnection.HTTP_OK) { throw new IOException("HTTP response code: " + rc); } Thread.sleep(100); int len = (int) connection.getLength(); if (len > 0) { int actual = 0; int bytesread = 0; byte[] data = new byte[len]; while ((bytesread != len) && (actual != -1)) { actual = inputstream.read(data, bytesread, len - bytesread); bytesread += actual; } process(data); } else { int ch; ByteArrayOutputStream b = new ByteArrayOutputStream(); while ((ch = inputstream.read()) != -1) { b.write((byte) ch); } process(b.toByteArray()); } } catch (Exception ex) { this.exception = ex; } done = true; } }
Hello,
The ConnectionNotFoundException might suggest that there are no connection to the data carrier. Do you monitor the network connection and data connection state in your application? Is it possible that the network might have detached the device in your scenario? Do you pass the APN and other parameters while calling Connector.open() or do you use SJNET command?
I can see in the comment that the transmission should be closed in ***. 60 seconds while according to the code it's 100 milliseconds multiplied by 60000 which give 6000 seconds. In case of http thread has stuck it could take 100 minutes to closing the connection - during this time the network detach could occur. Do you have a log with timestamps to verify this? But if the connection parameters are passed to the Connector.open() the attach should be made again if the cellular network is available.
Regards,
Bartłomiej
Hello!
>Do you monitor the network connection and data connection state in your application?
No, how can I accomplish that?
>Is it possible that the network might have detached the device in your scenario?
Maybe. How can I detect detached network? Is there a way to force a re-attach with Java? BTW, the orange LED of the terminal flahes once in ~4 seconds, so I think it's not detached.
>Do you pass the APN and other parameters while calling Connector.open() or do you use SJNET command?
I pass APN and other parameters to Connector.open(). For example, "https://server.com/path;bearer_type=gprs;access_point=***.yyy"
>I can see in the comment that the transmission should be closed in ***. 60 seconds while according to the code it's 100 milliseconds multiplied by 60000 which give 6000 seconds.
Be aware that the loop variable increases by 100, so this results in 600 sleeps á 100ms, which gives 600*0,1s = 60 seconds
In case of http thread has stuck it could take 100 minutes to closing the connection - during this time the network detach could occur.
My observation is that when the ConnectionNotFoundException occurs, its not possible to send any more messages, even when the program runs several days.
>Do you have a log with timestamps to verify this?
I have a server-side log. I started EHS5T on 3.2.16:42 and it should send Heartbeats every 5 Minutes. But it stopped sending on 4.2. 13:21. I disconnected power on EHS5T on 7.2. 9:00
Since 4.2. 13:21 until 7.2. 9:00, EHS5T throws ConnectionNotFoundException as described in original post. Then I disconnected power of EHS5T.
Hello,
The network registration can be verified by the CREG URC for example. You can configure AT+CREG=1 or 2 and listen for URC's. For the bearer monitoring there is API available - please see BearerControl class and BearerControlListener interface.
But if you pass the network access parameters to the Connector.open() the connections should be established. Even though I'd add the BearerControlListener to monitor the bearer state.
You are right about the 60 seconds of course - I didn't notice somehow.
The problem with no possibility to establish another connection after ConnectionNotFoundException is really a serious thing - and some debugging is needed here to determine the cause of that problem - is it in the application and there is a way avoid it or not. The last instance would be to restart the module but it is not the desired solution of course.
The log from mobile application would be more helpful to clarify what went wrong.
Regards,
Bartłomiej