Android的插座InputStream读(后跟一个EPIPE) [英] Android socket inputstream read (followed by an EPIPE)

查看:200
本文介绍了Android的插座InputStream读(后跟一个EPIPE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了一些客户端套接字软件来读取从服务器的事件。 (例如,流股票);

I am creating some client-side socket software to read events from a server. (For example, streaming stock quotes).

问题: .read(B);在返回的立即 1 的值。这将导致一个infinte循环和手机会变得非常热。此外,所有的检查,以s.isConnected(),isOpen会(),一个isBound()返回true。本质上,插座的看起来连接的。 (这是一个错误的情况下,所以.setSoTimeout的任何值(x)没有任何影响。第12分钟,或保留为空。该.read(B)总是返回-1立即)。

PROBLEM: .read(b); is returning immediately with a value of -1. This causes an infinte loop and the phone becomes very hot. Additionally, all checks to s.isConnected(), isOpen(), isBound() return true. Essentially the socket looks connected. (This is an error scenario, so any value of .setSoTimeout(x) has no effect. 12 minutes, or leave empty. The .read(b) always returns -1 immediately).

当我写它以后,通过的getOutputStream(),我收到一个异常EPIPE(破管道)。

When I write to it later, via the getOutputStream(), I receive an exception EPIPE (broken pipe).

下面是核心code(日志不再赘述报表/值检查)。

Here's the core code (log statements/value checks omitted for brevity).

s.connect(new InetSocketAddress(host, port), CONNECT_TIMEOUT_MILLIS);

byte[] b = new byte[1024];
while (s.isConnected()) {
    int bytesToRead = s.getInputStream().read(b);
    if (bytesToRead <= 0) {
        LOGGER.debug("no bytes read? trying again.");
        continue;
    }
    processFrame(b);
}

如果我周期在手机上的3G,它工作正常。有时它进入这个奇怪的状态。

If I cycle the 3g on the phone, it works fine. Sometimes it gets into this weird state.

我是不是做错了什么?这是的有望的行为?是否有现有code我可以看看显示的右键的办法做到在Android socket编程?

Am I doing something wrong? Is this the expected behavior? Is there existing code I could look at to show the right way to do socket programming on Android?

推荐答案

如果读(字节[],...)收益&LT; 0,对方已经关闭了连接,所以你必须关闭套接字,退出循环。绝不会有任何更多的数据。

If read(byte[], ...) returns < 0, the peer has closed the connection, so you must close the socket and exit the loop. There will never be any more data.

输入流被阻断,因此的唯一办法读取(字节[],...)可以返回零如果指定了零长度的缓冲区或长度为零,这取决于超载你打电话。当你不这样做,它永远不会返回零。

Input streams are blocking, so the only way read(byte[], ...) can return zero is if you specify a zero length buffer or a zero length, depending on which overload you call. As you aren't doing that, it will never return zero.

相反,它会做正是它说在Javadoc:要么返回-1意为EOS,或阻塞,直到数据中的至少一个字节可用

Instead it will do exactly what it says in the Javadoc: either return -1 meaning EOS, or block until at least one byte of data is available.

我是不是做错了什么?

几乎所有的东西。为&LT你的循环测试; = 0 是完全没有意义的,完全不正确。所以,正在测试 isConnected()。只告诉您是否曾经连这个插座。它不与连接的状态发生变化。 -1返回code告诉你。你的循环应该是:

Almost everything. Your loop testing for <= 0 is completely pointless and completely incorrect. And so is testing isConnected(). That only tells you whether you ever connected this Socket. It doesn't change with the state of the connection. The return code of -1 tells you that. Your loop should read:

while ((bytesToRead = s.getInputStream().read(b)) > 0)
{
    // do something with b[0..bytesToRead -1].
}
s.close();

这篇关于Android的插座InputStream读(后跟一个EPIPE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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