在 Android 中获取“SocketException : Connection reset by peer”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11207394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:20:08  来源:igfitidea点击:

Getting "SocketException : Connection reset by peer" in Android

androidlocalhostsocketexceptionpeer

提问by android developer

My app needs to contact the same device it is working on, via http://127.0.0.1/...(a localhost url).

我的应用程序需要通过http://127.0.0.1/...(本地主机网址)联系它正在使用的同一设备。

For some reason, about 50% of the times (and maybe exactly 50%) when I reach a website there with JSON content, I get the exception:

出于某种原因,当我访问包含 JSON 内容的网站时,大约有 50% 的时间(可能正好是 50%),我得到了例外:

java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)

For the other 50%, I get perfectly good results. I've tried to do polls (and even large delay between polls), but I keep getting the same weird results.

对于另外 50%,我得到了非常好的结果。我试过进行民意调查(甚至在民意调查之间有很大的延迟),但我一直得到同样奇怪的结果。

I've searched the internet and also here, and I'm not sure why it occurs. Does the peer mean that the client has caused it? Why does it happen, and how should i handle it?

我已经在互联网和这里进行了搜索,但我不确定为什么会发生这种情况。对等点是否意味着客户端导致了它?为什么会发生,我应该如何处理?

Some websites say that it's a common thing, but I didn't find what's the best thing to do in such cases.

有些网站说这是很常见的事情,但我没有找到在这种情况下最好的做法。

采纳答案by android developer

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.

可能是 Android 保留了一个连接池并使用旧的连接池或类似的东西。

Anyway , now it works.

无论如何,现在它起作用了。



EDIT: according to the API of HttpURLConnection, this can be solved on the client side too:

编辑:根据 API HttpURLConnection,这也可以在客户端解决:

The input and output streams returned by this class are not buffered. Most callers should 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. Calls to 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.

此类返回的输入和输出流不会被缓冲。大多数调用者应该使用 BufferedInputStream 或 BufferedOutputStream 包装返回的流。只进行批量读取或写入的调用者可能会忽略缓冲。当向服务器传输大量数据或从服务器传输大量数据时,使用流来限制一次内存中的数据量。除非您需要将整个主体一次性存储在内存中,否则将其作为流处理(而不是将整个主体存储为单个字节数组或字符串)。

为了减少延迟,此类可能会为多个请求/响应对重用相同的底层 Socket。因此,HTTP 连接的打开时间可能会超过必要的时间。调用 disconnect() 可能会将套接字返回到连接的套接字池。在发出任何 HTTP 请求之前,可以通过将 http.keepAlive 系统属性设置为 false 来禁用此行为。http.maxConnections 属性可用于控制每个服务器将保持多少空闲连接。

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

取自: developer.android.com/reference/java/net/HttpURLConnection.html

回答by valerybodak

Try to set this property for your HttpURLConnectionbefore connecting:

在连接之前尝试为您的HttpURLConnection设置此属性:

conn.setRequestProperty("connection", "close");

This will disable "keep-alive" property which is on by default.

这将禁用默认开启的“ keep-alive”属性。

回答by Cucumen

This is an old thread i know. But this might help someone.

这是我知道的旧线程。但这可能对某人有所帮助。

In my case this error was caused by the .NET WCF (soap) service. One of the objects in the returning result had a DataMember with get{} property but no set{} property.

就我而言,此错误是由 .NET WCF (soap) 服务引起的。返回结果中的一个对象具有一个带有 get{} 属性但没有 set{} 属性的 DataMember。

For serialization to occur every DataMember should have both get{} & set{} available. I implemented an empty set{} (empty due to my business rules), and problem was solved.

要进行序列化,每个 DataMember 都应该同时使用 get{} 和 set{}。我实现了一个空集{}(由于我的业务规则为空),问题解决了。

My scenerio is a specific bad server implementation, but maybe it'll help someone saving time when troubleshooting.

我的场景是一个特定的糟糕服务器实现,但也许它会帮助某人在故障排除时节省时间。

回答by Sébastien

I was having a lot of these Connection reset by peerwhen I was visiting certain web pages or downloading files (from my app or the Android browser).

Connection reset by peer当我访问某些网页或下载文件(从我的应用程序或 Android 浏览器)时,我遇到了很多这样的问题。

Turned out it was my 3G carrier that blocked the connections (e.g. downloading an .exefile was forbidden).

原来是我的 3G 运营商阻止了连接(例如.exe,禁止下载文件)。

Do you have the same problem on Wifi ?

您在 Wifi 上有同样的问题吗?

回答by ahmad pj

in my situation the problem has been solved by cleaning the proxy address and port from APN which was produced by the operator. as I have tested, using IP address of remote server instead of domain name also can solve the problem.

在我的情况下,问题已通过从运营商生成的 APN 中清除代理地址和端口来解决。我已经测试过,使用远程服务器的IP地址代替域名也可以解决问题。

回答by j-j

In our case the issue is in the Server side (Application Pool configuration in IIS). I resolved it by setting Maximum Worker Processes to 1. Setting it with value more than 1 will enable Web Garden which seems to be the issue.

在我们的例子中,问题出在服务器端(IIS 中的应用程序池配置)。我通过将最大工作进程数设置为 1 来解决它。将其设置为大于 1 的值将启用 Web Garden,这似乎是问题所在。