B4J Question java.net.SocketException: Connection reset by peer: socket write error

schimanski

Well-Known Member
Licensed User
Longtime User
Sometimes, I got this exception when I try to upload a list of files from one server to another. What could be the reason for this exception? I doesn't get it always and it seems, that it is only releated to one of the files. Perhaps is it better to upload one by one?

B4X:
java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at sun.security.ssl.OutputRecord.writeBuffer(OutputRecord.java:431)
    at sun.security.ssl.OutputRecord.write(OutputRecord.java:417)
    at sun.security.ssl.SSLSocketImpl.writeRecordInternal(SSLSocketImpl.java:876)
    at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:847)
    at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
    at okio.Okio$1.write(Okio.java:78)
    at okio.AsyncTimeout$1.write(AsyncTimeout.java:179)
    at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
    at okio.RealBufferedSink.write(RealBufferedSink.java:41)
    at okhttp3.internal.http1.Http1Codec$FixedLengthSink.write(Http1Codec.java:287)
    at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
    at okio.RealBufferedSink.write(RealBufferedSink.java:85)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$PostPayload.writeTo(OkHttpClientWrapper.java:538)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:48)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
    at okhttp3.RealCall.execute(RealCall.java:63)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:156)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:153)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:201)
    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:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
2017-10-07 16:05:12,483 [main] INFO   - 12 Dokumente im Rahmen einer Uebernahme erfolgreich gesendet!
2017-10-07 16:05:12,483 [main] WARN   - 1 Dokumente konnten nicht uebermittelt werden!

This is my code:

B4X:
  Dim Files As List
  Files=File.ListFiles(Main.Dateipfad & "Konferenzen/" & konf)
                      
  For e=0 To Files.Size-1
                     
      Dim Pfad As String
      Pfad=Main.Dateipfad & "Konferenzen/" & konf & "/" & Files.Get(e)
                                                  
      If File.Exists(File.DirApp, Pfad) Then
           Dim FremdServerUebernahme As HttpJob
           FremdServerUebernahme.Initialize("FremdServer_fileuebernahme_senden", Me)
           FremdServerUebernahme.Tag=Pfad
           FremdServerUebernahme.PostFile(serverpfad & "/" & Main.AppName & "?absender=" & Main.config.Get("Standort") & "&passwort=" & suURL.EncodeUrl(Serverdaten.Get(1), "UTF8") & "&filter=server&&type=fileuebernahme&verzeichnis=konferenzen&konferenz=" & konf & "&dok=" & Files.Get(e) & "&absender=" & absender, File.DirApp, Pfad)
           wait for (FremdServerUebernahme) JobDone(FremdServerUebernahme As HttpJob)
           If FremdServerUebernahme.Success Then
                 Nachrichtgesendet.add(FremdServerUebernahme.Tag)
            Else
                 Nachrichtnichtgesendet.add(FremdServerUebernahme.Tag)
             End If
             FremdServerUebernahme.Release
        End If
  Next
                      
  If Nachrichtgesendet.Size > 0 Then Main.Logger.logsINFO(Nachrichtgesendet.Size & " Dokumente im Rahmen einer Uebernahme erfolgreich gesendet!")
  If Nachrichtnichtgesendet.Size>0 Then Main.Logger.logsWARN(Nachrichtnichtgesendet.Size & " Dokumente konnten nicht uebermittelt werden!")
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Perhaps is it better to upload one by one?
Your code uploads the files one after another.

You should expect network errors to happen. Maybe the server is rejecting it for some reason.

Try it with this code:
B4X:
Sub UploadWithRetry(url As String, Dir As String, FileName As String) As ResumableSub
   Dim Retries As Int = 3
   For i = 1 To Retries
     Dim j As HttpJob
     j.Initialize("", Me)
     j.PostFile(url, Dir, FileName)
     Wait For (j) JobDone (j As HttpJob)
     j.Release
     If j.Success Then Return True
     Sleep(2000 * i)
   Next
   Return False
End Sub

Call it with:
B4X:
Wait For (UploadWithRetry(...)) Complete (Result As Boolean)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…