retrofit.RetrofitError: sendto failed: ECONNRESET (Connection reset by peer)

Created on 24 Aug 2015  Â·  16Comments  Â·  Source: square/retrofit

I am uploading images with retrofit multipart and continuously getting the following exception :

Caused by: retrofit.RetrofitError: sendto failed: ECONNRESET (Connection reset by peer)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at java.lang.reflect.Proxy.invoke(Proxy.java:397)
at $Proxy2.uploadImage(Unknown Source)
at gaadi.evaluator.com.retrofit.RetrofitRequest.makeImageUploadRequest(RetrofitRequest.java:26)
at gaadi.evaluator.com.service.ImageUploadServiceRevamp.onHandleIntent(ImageUploadServiceRevamp.java:73)
... 4 more
Caused by: java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)
at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:546)
at libcore.io.IoBridge.sendto(IoBridge.java:515)
at java.net.PlainSocketImpl.write(PlainSocketImpl.java:504)
at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:37)
at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
at okio.Okio$1.write(Okio.java:78)
at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
at okio.RealBufferedSink.write(RealBufferedSink.java:46)
at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:302)
at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
at okio.RealBufferedSink$1.write(RealBufferedSink.java:198)
at retrofit.mime.TypedFile.writeTo(TypedFile.java:78)
at retrofit.mime.MultipartTypedOutput$MimePart.writeTo(MultipartTypedOutput.java:54)
at retrofit.mime.MultipartTypedOutput.writeTo(MultipartTypedOutput.java:144)
at retrofit.client.OkClient$1.writeTo(OkClient.java:88)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:898)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:760)
at com.squareup.okhttp.Call.getResponse(Call.java:274)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:230)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:201)
at com.squareup.okhttp.Call.execute(Call.java:81)
at retrofit.client.OkClient.execute(OkClient.java:53)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
... 9 more
Caused by: android.system.ErrnoException: sendto failed: ECONNRESET (Connection reset by peer)
at libcore.io.Posix.sendtoBytes(Native Method)
at libcore.io.Posix.sendto(Posix.java:176)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278)
at libcore.io.IoBridge.sendto(IoBridge.java:513)
... 31 more

The adapter i am using is :

public static RestAdapter getRestAdapter(Context context){
String url = Utils.getWebServiceURL(context);
String urlBase = url.substring(0, url.lastIndexOf("/"));

    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
    okHttpClient.setWriteTimeout(60, TimeUnit.SECONDS);
    okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
    okHttpClient.setConnectionPool(new ConnectionPool(0, 5 * 60 * 1000));

    return new RestAdapter.Builder()
            .setEndpoint(urlBase)
            .setLogLevel(RestAdapter.LogLevel.BASIC)
            .setClient(new OkClient(okHttpClient))
            .build();
}

Interface method used for making request is as follows :
@Multipart
@POST("/{path}")
public ImageUploadResponse uploadImage(@Path("path") String path, @Part(Constants.EVALUATION_DATA) String map, @Part("certImg") TypedFile file);

Call made to this interface method is as follows :
TypedFile file = new TypedFile("multipart/form-data", new File(imagePath));
String url = Utils.getWebServiceURL(UILApplication.getInstance());
return requestInterface.uploadDocumentImage(path, jsonParams, file);

Could not find a solution to this on stackoverflow.

Most helpful comment

The server you are connecting to has closed the connection for whatever reason. You need to speak to the people who run the server and see what's happening. Could be an invalid request, broken server, invalid format, etc. This isn't a problem with Retrofit itself.

All 16 comments

The server you are connecting to has closed the connection for whatever reason. You need to speak to the people who run the server and see what's happening. Could be an invalid request, broken server, invalid format, etc. This isn't a problem with Retrofit itself.

Actually it is something in Retrofit also, I had the same issue, which is happening only when there is Post requests.

I have button for sending the request to server and get the response as row string.
just right before this request i uploaded image. I had the same error like 2 to 3 times, if i try to send the request 3 or more times, i get successful response!

So guessing that maybe the retry issue

i am getting same error while uploading the 25 sec duration video.. if i upload 5 to 10 sec video, it is working fine...

is it issue related size of content? How to fix it...?

Server side issue!

Hi This is ravi,
I got an error like recvfrom failed econnreset (connection reset by peer)

Please help to fix this error.

i have the same issue on android, but ios is ok, please help me

I have the same issue!

Once You Check Your Server Connections and API Calls.

It's a problem in server side. i think API Problem

have somebody fix it? this code System.setProperty("http.keepAlive", "false"); is not work for me!

@gzfgeh yes! I post my answer here: stackoverflow

@HamedGh not work for me,
OkHttpClient okHttpClient = okHttpBuilder
.connectTimeout(200, TimeUnit.SECONDS)
.readTimeout(200, TimeUnit.SECONDS)
.writeTimeout(200, TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
.addInterceptor(httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
.addInterceptor(headerInterceptor)
.addInterceptor(cacheInterceptor)
.cache(cache)
.build();

@gzfgeh Does your api method works fine with postman?

i have the same problem with retrofit 2, no one has a solution?

User-Agent: okhttp/3.6
My question is /, it is a special character
for reference only

It may be caused by the long connection maintenance time of the client is longer than that of the server.
Two solutions:

// Set request header
 "Connection" :"close"

or

// Set request header
 "Connection", "keep-alive"
 //set keep-Alive duration 
OkHttpClient client = new OkHttpClient.Builder() 
         ...
         //keep-Alive duration : 50s
        .connectionPool(new ConnectionPool(5, 50, TimeUnit.SECONDS)) 
        .build();

I have the same problem with retrofit 2,
Solution:-

OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(50, TimeUnit.SECONDS)
.readTimeout(50, TimeUnit.SECONDS)
.writeTimeout(50, TimeUnit.SECONDS)
.connectionPool(new ConnectionPool(5, 50, TimeUnit.SECONDS)).build();
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}

Was this page helpful?
0 / 5 - 0 ratings