Hi,
I have written a quick and (maybe) dirty program which connect to my 3D Printer to get some information.
My problem is that i sometimes get connection errors like time out or connection resets.
I have tried to catch those with try & Catch and with the Application_Error sub but without success can somebody point me in the right direction to catch these errors ?
and here an error
and a other error which i have to catch
I have written a quick and (maybe) dirty program which connect to my 3D Printer to get some information.
My problem is that i sometimes get connection errors like time out or connection resets.
I have tried to catch those with try & Catch and with the Application_Error sub but without success can somebody point me in the right direction to catch these errors ?
B4X:
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#End Region
Sub Process_Globals
Private srvr As Server
Dim lastLayer As Int = 0
Dim lastPrinterState As String
Dim LayerTimer As Timer
Dim PrinterStateTimer As Timer
End Sub
Sub AppStart (Args() As String)
srvr.Initialize("srvr")
' srvr.Port = 51042
' srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")
' srvr.AddHandler("/guessmynumber/guess", "GuessMyNumber", False)
' srvr.AddHandler("/guessmynumber/reset", "ResetMyNumber", False)
' srvr.AddWebSocket("/guessmynumber_ws/ws", "WSGuessMyNumber")
' srvr.Start
PrinterStateTimer.Initialize("status",500)
PrinterStateTimer.Enabled=True
LayerTimer.Initialize("Layer",250)
StartMessageLoop
End Sub
Sub layer_tick
' Try
Wait For (getlayer) complete (result As Int)
If result <> -1 Then
If lastLayer <> result Then
Log ("Layer: "&result)
lastLayer=result
End If
End If
' Catch
' Log("Layertick")
' End Try
End Sub
Sub status_tick
' Try
Dim j As HttpJob
j.Initialize("", Me)
j.Download("http://192.168.188.36/rr_model?key=state.status")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim parser As JSONParser
parser.Initialize(j.GetString)
Dim root As Map = parser.NextObject
Dim PrinterState As String = root.Get("result")
If lastPrinterState <> PrinterState Then
Log(PrinterState)
lastPrinterState=PrinterState
End If
Else
Log ("status error "&j.ErrorMessage)
End If
j.Release
If PrinterState <> "idle" And PrinterState <> "busy" And PrinterState <>"halted" Then 'processing
LayerTimer.Enabled=True
Else
LayerTimer.Enabled=False
End If
' Catch
' Log ("error can't get Printer status")
' End Try
End Sub
Sub getlayer As ResumableSub
' Try
Dim j As HttpJob
j.Initialize("", Me)
j.Download("http://192.168.188.36/rr_model?key=job.layer")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Dim parser As JSONParser
parser.Initialize(j.GetString)
Dim root As Map = parser.NextObject
Dim Layer As Int = root.Get("result")
j.Release
Return Layer
Else
Log ("getlayer error "&j.ErrorMessage)
End If
' Catch
' Log ("error can't get Layer")
' End Try
Return -1
End Sub
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Log ("app error")
' If srvr.CurrentThreadIndex = 0 Then 'main thread
' LogError(StackTrace)
' Return False
'' Else 'handlers threads
'' Return True
' End If
Log (Error)
Return False
End Sub
and here an error
B4X:
2021-02-08 21:48:59.048:INFO::main: Logging initialized @326ms to org.eclipse.jetty.util.log.StdErrLog
busy
idle
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at okio.Okio$2.read(Okio.java:140)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:358)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:230)
at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257)
at okhttp3.RealCall.execute(RealCall.java:93)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:173)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:170)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:218)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
ResponseError. Reason: java.net.SocketException: Connection reset, Response:
status error java.net.SocketException: Connection reset
and a other error which i have to catch
B4X:
java.net.ConnectException: Failed to connect to /192.168.188.36:80
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:249)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:167)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:258)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257)
at okhttp3.RealCall.execute(RealCall.java:93)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:173)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:170)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:218)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at okhttp3.internal.platform.Platform.connectSocket(Platform.java:129)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:247)
... 25 more
ResponseError. Reason: java.net.ConnectException: Failed to connect to /192.168.188.36:80, Response:
getlayer error java.net.ConnectException: Failed to connect to /192.168.188.36:80
Last edited: