获得" SocketException:连接由对等&QUOT复位; Android中 [英] Getting "SocketException : Connection reset by peer" in Android

查看:289
本文介绍了获得" SocketException:连接由对等&QUOT复位; Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要联系相同的设备也正在研究,通过的http://127.0.0.1 / ... (本地主机URL)

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

由于某些原因,约50%的时间(也许正是50%),当我到达使用JSON内容的网站上有,我得到异常:

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:

java.net.SocketException异常:recvfrom的失败:ECONNRESET(由体连接复位)

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

有关其他的50%,我得到非常好的效果。我一直试图做的民意调查(甚至投票之间的大延迟),但我不断收到奇怪的结果相同。

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.

我在网上搜索,也是在这里,我不知道为什么会发生。是否同行意味着客户端造成的吗?它为什么会发生,我应该怎么处理呢?

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.

推荐答案

确定,得到的答复是,这是服务器的故障 - 它不得不关闭,每个请求之后的连接。

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

这也许是因为Android的保持连接池,并使用旧的或类似的东西。

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

反正,现在的作品。

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

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

此类返回的输入和输出流不被缓冲。   大多数用户请换用的BufferedInputStream返回的流   或的BufferedOutputStream。来电者是只做批量读取或写入可   省略了缓冲。当传输大量数据,或从   服务器,使用流限制多少数据在内存中一次。   除非你需要整个身体是在内存中一次,处理它作为   流(而不是存储完整体作为一个单一的字节数组   或字符串)。

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).

要减少等待时间,这个类可以重复使用相同的基础套接字的   多个请求/响应对。其结果是,HTTP连接可以是   打开更长时间持有超过必要。呼叫断开()可能返回   插座连接套接字池。这种行为可以被禁用   由http.keepAlive系统属性设置为false发行前   任何HTTP请求。属性可被用于将http.maxConnections   控制有多少空闲连接到每个服务器将举行。

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.

来自: <一href="http://developer.android.com/reference/java/net/HttpURLConnection.html">developer.android.com/reference/java/net/HttpURLConnection.html

这篇关于获得&QUOT; SocketException:连接由对等&QUOT复位; Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆