Gemalto is now part of the Thales Group, find out more.

You are here

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;
	}
}