当应该有可用数据时,DataInputStream.available返回0

时间:2014-10-23 12:19:32

标签: java android sockets

我正忙着使用谷歌眼镜制作内部导航应用程序。 我正在使用DataInputStream / DataOutputstream将方向数据从玻璃发送到Android手机。

客户端/

     protected void onHandleIntent(Intent intent) {
    try {
        Socket mClientSocket = new Socket(serverIP, PORT);
        Log.d("ODSA","Client is connected");
        InputStream inputStream = mClientSocket.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        isServiceRunning = true;
        long lastTime = System.currentTimeMillis();
        while (isServiceRunning) {
                int bytesAvailabe = dataInputStream.available();
                Log.d("ODSA","Waiting for bytesAvailable: Real bytes available: " + bytesAvailabe);
                if(bytesAvailabe > 4) {
                    if (bytesAvailabe > 8)
                    {
                        long bytesToSkip = bytesAvailabe - 8;
                        Log.d("ODSA","BytesSkipped: " + bytesToSkip);
                        inputStream.skip(bytesToSkip);
                    }
                    Log.d("ODSA","Trying to read Azimuth");
                    float azimuth = dataInputStream.readFloat();
                    Log.d("ODSA","Trying to read Pitch");
                    float pitch = dataInputStream.readFloat();
                    String resultTxt = "Received from server - Azimuth: " + azimuth + " | Pitch: " + pitch;
                    Log.d("ODSA", resultTxt);
                    Log.d("ODSA","Sende broadcast intent");
                    Intent broadcastIntent = new Intent();
                    //deze wil ik eigenlijk niet hier maar krijg de reference niet te pakken in orientation service doe ik iets fout ?
                    broadcastIntent.setAction("com.odca.intent.action.MESSAGE_PROCESSED");
                    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
                    broadcastIntent.putExtra(PARAM_OUT_MSG, resultTxt);
                    sendBroadcast(broadcastIntent);
                    Log.d("ODSA","Done sending broadcast intent");
                }
                else{
                    Thread.sleep(1000);
                }
        }
    } catch (IOException e) {
        e.printStackTrace();
        //TODO SEND ERROR MESSAGE TO ACTIVITY
    }
    catch (InterruptedException e) {
        e.printStackTrace();
        //TODO SEND ERROR MESSAGE TO ACTIVITY
    }

}

SERVER /

 protected void onHandleIntent(Intent intent) {
    try {
        //Create connection to orientation sensor
        Context startActivityContext = getBaseContext();
        this.orientationSensorManager = new OrientationSensorManager(startActivityContext);

        //Start orientation server
        ServerSocket mServerSocket = new ServerSocket();
        mServerSocket.setReuseAddress(true);
        mServerSocket.bind(new InetSocketAddress(PORT));
        Log.d("ODSA","Waiting for connection on server");
        Socket mClientSocket = mServerSocket.accept();
        //TODO SEND MESSAGE TO ACTIVITY (GLASS IS CONNECTED)
        Log.d("ODSA","Server is connected");
        OutputStream oStream = mClientSocket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(oStream);
        isServiceRunning = true;
        long lastTime = System.currentTimeMillis();
        while (isServiceRunning) {
            long endTime = System.currentTimeMillis();
            long difference = endTime - lastTime;
            if(difference > 100) {
                float[] results = orientationSensorManager.getOrientationData();
                dataOutputStream.writeFloat(results[0]);
                dataOutputStream.writeFloat(results[1]);
                lastTime = System.currentTimeMillis();
                //TODO Send orientation data to activity
                Log.d("ODSA","Data Send to client - Azimuth: " + String.valueOf(results[0] + " | Pitch: " + results[1]));
            }
        }
        if (mClientSocket != null) mClientSocket.close();
        if (mServerSocket != null) mServerSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

服务器发送数据就好了。并且在测试时运行完美(以100毫秒发布数据)。

问题在于客户端代码。由于某种原因,当流应该有明显数据时,datainputstream.available返回0。为什么会这样?

我将包含一些logcat输出,以便更好地理解我的问题。在logcat中很容易看到问题(这是以1000毫秒的间隔运行)。欢迎任何建议。

10-23 13:48:48.452    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 8
10-23 13:48:48.452    2897-3214/com.odca D/ODSA﹕ Trying to read Azimuth
10-23 13:48:48.452    2897-3214/com.odca D/ODSA﹕ Trying to read Pitch
10-23 13:48:48.452    2897-3214/com.odca D/ODSA﹕ Received from server - Azimuth: 33.915466 | Pitch: 82.41343
10-23 13:48:48.452    2897-3214/com.odca D/ODSA﹕ Sende broadcast intent
10-23 13:48:48.462    2897-3214/com.odca D/ODSA﹕ Done sending broadcast intent
10-23 13:48:48.462    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:48:49.472    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 24
10-23 13:48:49.472    2897-3214/com.odca D/ODSA﹕ BytesSkipped: 16
10-23 13:48:49.472    2897-3214/com.odca D/ODSA﹕ Trying to read Azimuth
10-23 13:48:49.472    2897-3214/com.odca D/ODSA﹕ Trying to read Pitch
10-23 13:48:49.482    2897-3214/com.odca D/ODSA﹕ Received from server - Azimuth: 34.72676 | Pitch: 82.230484
10-23 13:48:49.482    2897-3214/com.odca D/ODSA﹕ Sende broadcast intent
10-23 13:48:49.502    2897-3214/com.odca D/ODSA﹕ Done sending broadcast intent
10-23 13:48:49.502    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:48:50.502    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:48:51.512    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:48:52.502    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:48:53.502    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:48:54.512    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:48:55.512    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:48:56.512    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:48:57.512    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:48:58.512    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 776
10-23 13:48:58.512    2897-3214/com.odca D/ODSA﹕ BytesSkipped: 768
10-23 13:48:58.512    2897-3214/com.odca D/ODSA﹕ Trying to read Azimuth
10-23 13:48:58.512    2897-3214/com.odca D/ODSA﹕ Trying to read Pitch
10-23 13:48:58.512    2897-3214/com.odca D/ODSA﹕ Received from server - Azimuth: 33.074875 | Pitch: 82.22403
10-23 13:48:58.522    2897-3214/com.odca D/ODSA﹕ Sende broadcast intent
10-23 13:48:58.532    2897-3214/com.odca D/ODSA﹕ Done sending broadcast intent
10-23 13:48:58.532    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:48:59.542    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 64
10-23 13:48:59.542    2897-3214/com.odca D/ODSA﹕ BytesSkipped: 56
10-23 13:48:59.542    2897-3214/com.odca D/ODSA﹕ Trying to read Azimuth
10-23 13:48:59.542    2897-3214/com.odca D/ODSA﹕ Trying to read Pitch
10-23 13:48:59.542    2897-3214/com.odca D/ODSA﹕ Received from server - Azimuth: 32.31331 | Pitch: 81.64859
10-23 13:48:59.552    2897-3214/com.odca D/ODSA﹕ Sende broadcast intent
10-23 13:48:59.572    2897-3214/com.odca D/ODSA﹕ Done sending broadcast intent
10-23 13:48:59.572    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 16
10-23 13:48:59.572    2897-3214/com.odca D/ODSA﹕ BytesSkipped: 8
10-23 13:48:59.572    2897-3214/com.odca D/ODSA﹕ Trying to read Azimuth
10-23 13:48:59.572    2897-3214/com.odca D/ODSA﹕ Trying to read Pitch
10-23 13:48:59.572    2897-3214/com.odca D/ODSA﹕ Received from server - Azimuth: 32.404648 | Pitch: 82.11532
10-23 13:48:59.572    2897-3214/com.odca D/ODSA﹕ Sende broadcast intent
10-23 13:48:59.582    2897-3214/com.odca D/ODSA﹕ Done sending broadcast intent
10-23 13:48:59.582    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 0
10-23 13:49:00.592    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 72
10-23 13:49:00.592    2897-3214/com.odca D/ODSA﹕ BytesSkipped: 64
10-23 13:49:00.592    2897-3214/com.odca D/ODSA﹕ Trying to read Azimuth
10-23 13:49:00.592    2897-3214/com.odca D/ODSA﹕ Trying to read Pitch
10-23 13:49:00.592    2897-3214/com.odca D/ODSA﹕ Received from server - Azimuth: 32.3553 | Pitch: 82.41276
10-23 13:49:00.592    2897-3214/com.odca D/ODSA﹕ Sende broadcast intent
10-23 13:49:00.612    2897-3214/com.odca D/ODSA﹕ Done sending broadcast intent
10-23 13:49:00.612    2897-3214/com.odca D/ODSA﹕ Waiting for bytesAvailable: Real bytes available: 16

1 个答案:

答案 0 :(得分:0)

实际上并没有义务返回非零,而许多实现都没有。例如,仅接收到部分SSL记录的SSLSocket可能不知道数据将持续多长时间,并且多年来它总是返回零。

只需摆脱available()调用和跳过,读取所有内容,然后在read()方法中阻止,或者在没有输入的情况下使用readFloat()方法