Java.net. socketexception: recvfrom failed: econnreset

Source: Internet
Author: User
Tags connection reset
Package COM. ucaimils. util; import Java. io. bytearrayoutputstream; import Java. io. inputstream; import java.net. httpurlconnection; import java.net. URL; import Org. apache. HTTP. httpresponse; import Org. apache. HTTP. client. methods. httpget; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. apache. HTTP. util. entityutils; import android. util. log;/*** network connection class */public class httphelper {public static final Int time_out_mill = 10000; // timeout in milliseconds/*** httpurlconnection request method, directly returning string data */public static string gethttpurlconndata (string URL) throws exception {URL realurl = new URL (URL); httpurlconnection conn = (httpurlconnection) realurl. openconnection (); Conn. setrequestproperty ("accept", "*/*"); Conn. setrequestproperty ("connection", "keep-alive"); Conn. setrequestproperty ("User-Agent", "Mozilla/4.0 (compatible; m Sie 6.0; Windows NT 5.1; sv1) "); Conn. setdooutput (true); Conn. setdoinput (true); Conn. setconnecttimeout (time_out_mill); Conn. setreadtimeout (time_out_mill); inputstream is = Conn. getinputstream (); bytearrayoutputstream Dis = new bytearrayoutputstream (); int realread = 0; byte [] buff = new byte [1024]; while (realread = is. read (buff ))! =-1) {dis. write (buff, 0, realread);} string data = new string (DIS. tobytearray (), "UTF-8 "). trim (); // system. out. println (data); If (DIS! = NULL) {dis. Close () ;}if (Conn! = NULL) {conn. Disconnect (); // disconnect --- cause of the problem} return data ;}}

This error is reported today:

09-11 10:02:34. 849: W/system. Err (17963): java.net. socketexception: recvfrom failed: econnreset (Connection reset by peer)
09-11 10:02:34. 849: W/system. Err (17963): At libcore. Io. iobridge. maybethrowafterrecvfrom (iobridge. Java: 542)
09-11 10:02:34. 849: W/system. Err (17963): At libcore. Io. iobridge. recvfrom (iobridge. Java: 506)
09-11 10:02:34. 859: W/system. Err (17963): At java.net. plainsocketimpl. Read (plainsocketimpl. Java: 488)
09-11 10:02:34. 859: W/system. Err (17963): At java.net. plainsocketimpl. Access $000 (plainsocketimpl. Java: 46)
09-11 10:02:34. 859: W/system. Err (17963): At java.net. plainsocketimpl $ plainsocketinputstream. Read (plainsocketimpl. Java: 240)
09-11 10:02:34. 859: W/system. Err (17963): At java. Io. inputstream. Read (inputstream. Java: 163)
09-11 10:02:34. 869: W/system. Err (17963): At java. Io. bufferedinputstream. fillbuf (bufferedinputstream. Java: 142)
09-11 10:02:34. 869: W/system. Err (17963): At java. Io. bufferedinputstream. Read (bufferedinputstream. Java: 227)
09-11 10:02:34. 869: W/system. Err (17963): At libcore. Io. Streams. readasciiline (streams. Java: 201)
09-11 10:02:34. 869: W/system. Err (17963): At libcore.net. http. httpengine. readresponseheaders (httpengine. Java: 544)
09-11 10:02:34. 869: W/system. Err (17963): At libcore.net. http. httpengine. readresponse (httpengine. Java: 784)
09-11 10:02:34. 869: W/system. Err (17963): At libcore.net. http. httpurlconnectionimpl. getresponse (httpurlconnectionimpl. Java: 274)
09-11 10:02:34. 869: W/system. Err (17963): At libcore.net. http. httpurlconnectionimpl. getinputstream (httpurlconnectionimpl. Java: 168)
09-11 10:02:34. 869: W/system. Err (17963): At com. ucaimils. util. httphelper. gethttpurlconndata (httphelper. Java: 37)
09-11 10:02:34. 869: W/system. Err (17963): At com. ucaimils. util. querylocationtask. doinbackground (querylocationtask. Java: 49)
09-11 10:02:34. 869: W/system. Err (17963): At com. ucaimils. util. querylocationtask. doinbackground (querylocationtask. Java: 1)
09-11 10:02:34. 869: W/system. Err (17963): at Android. OS. asynctask $2. Call (asynctask. Java: 264)
09-11 10:02:34. 869: W/system. Err (17963): At java. util. Concurrent. futuretask $ sync. innerrun (futuretask. Java: 305)
09-11 10:02:34. 869: W/system. Err (17963): At java. util. Concurrent. futuretask. Run (futuretask. Java: 137)
09-11 10:02:34. 869: W/system. Err (17963): At java. util. Concurrent. threadpoolexecutor. runworker (threadpoolexecutor. Java: 1076)
09-11 10:02:34. 869: W/system. Err (17963): At java. util. Concurrent. threadpoolexecutor $ worker. Run (threadpoolexecutor. Java: 569)
09-11 10:02:34. 869: W/system. Err (17963): At java. Lang. thread. Run (thread. Java: 856)
09-11 10:02:34. 879: W/system. Err (17963): caused by: libcore. Io. errnoexception: recvfrom failed: econnreset (Connection reset by peer)
09-11 10:02:34. 889: W/system. Err (17963): At libcore. Io. POSIX. recvfrombytes (native method)
09-11 10:02:34. 889: W/system. Err (17963): At libcore. Io. POSIX. recvfrom (POSIX. Java: 131)
09-11 10:02:34. 889: W/system. Err (17963): At libcore. Io. blockguardos. recvfrom (blockguardos. Java: 164)
09-11 10:02:34. 889: W/system. Err (17963): At libcore. Io. iobridge. recvfrom (iobridge. Java: 503)
09-11 10:02:34. 889: W/system. Err (17963):... 20 more

I found an answer online (from a foreigner)

OK, the answer was that it's the server's fault-it had to close the connection after each request.

It might be that android keeps a pool of connections and use the old one or something like that.

Anyway, now it works.

Edit: According to the API of httpurlconnection, this can be solved on the client side too:

The input and output streams returned by this class are not buffered. most callers shoshould wrap the returned streams with bufferedinputstream or bufferedoutputstream. callers that do only bulk reads or writes may omit buffering. when transferring large amounts
Of data to or from a server, use streams to limit how much data is in memory at once. unless you need the entire body to be in memory at once, process it as a stream (rather than storing the complete body as a single byte array or string ).

To reduce latency, this class may reuse the same underlying socket for multiple request/response pairs. as a result, HTTP connections may be held open longer than necessary. callto disconnect () may return the socket to a pool of connected sockets. this behavior
Can be disabled by setting the HTTP. keepalive system property to false before issuing any HTTP requests. The HTTP. maxconnections property may be used to control how many idle connections to each server will be held.

Taken from: http // developer.android.com/reference/java/net/httpurlconnection.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.