具有读取()和readObject)问题( [英] Problems with read() and readObject()

查看:266
本文介绍了具有读取()和readObject)问题(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试开发一个纸牌游戏通过蓝牙播放1VS1。我已连接的设备,现在我有一个问题:我想送的对象抛出蓝牙。
如果我只作对象,它的工作原理,如果只是字符串,它的工作原理。
但是,如果我试图在同一时间,使这两个,我有问题。

I try to develop a card game to play 1vs1 via Bluetooth. I have connected the devices, and now I have a problem: I want to send Objects throw the Bluetooth. If I make only Objects, it works, if only Strings, it works. But if I try to make both at the same time, I have problems.

/**
 * This thread runs during a connection with a remote device.
 * It handles all incoming and outgoing transmissions.
 */
private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;

    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    // for Objects
    private final ObjectInputStream mObjectInStream;
    private final ObjectOutputStream mObjectOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        if (D) Log.d(TAG, "create ConnectedThread");
        mmSocket = socket;

        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        ObjectInputStream tmpObjIn = null;
        ObjectOutputStream tmpObjOut = null;

        // Get the BluetoothSocket input and output streams
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();

            tmpObjOut = new ObjectOutputStream(socket.getOutputStream());
            tmpObjOut.flush();
            tmpObjIn = new ObjectInputStream(socket.getInputStream());

        } catch (IOException e) {
            Log.e(TAG, "temp sockets not created", e);
        }


        mmInStream = tmpIn;
        mmOutStream = tmpOut;

        mObjectOutStream = tmpObjOut;
        mObjectInStream = tmpObjIn;
    }

    public void run() {
        if (D) Log.i(TAG, "BEGIN mConnectedThread");
        byte[] buffer = new byte[1024];
        int bytes;


        // Keep listening to the InputStream while connected
        while (true) {

                try {

                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);

                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();

                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    // Start the service over to restart listening mode
                    BluetoothService.this.start();
                    break;
                }

                try {

                    // Send the obtained Object to the UI Activity
                    mHandler.obtainMessage(Constants.MESSAGE_READ_OBJECT, -1, -1, mObjectInStream.readObject())
                            .sendToTarget();

                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    // Start the service over to restart listening mode
                    BluetoothService.this.start();
                    break;
                } catch (ClassNotFoundException cn) {
                    Log.e(TAG, "Class not found", cn);
                }
            }

    }


    /**
     * Write to the connected OutStream.
     *
     * @param buffer The bytes to write
     */

    public void writeString(byte[] buffer) {
        try {

            mmOutStream.write(buffer);

            // Share the sent message back to the UI Activity
            mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
                    .sendToTarget();
        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

    /**
     * Write an Object (Serializable) to the connected OutStream.
     *
     * @param object The object to write
     */
    public void writeObject(Object object) {
        try {

            mObjectOutStream.writeObject(object);

            // Share the sent message back to the UI Activity

            // TODO hier unterscheiden zwischen Player und UnoKarte?
            mHandler.obtainMessage(Constants.MESSAGE_WRITE_OBJECT, -1, -1, object)
                    .sendToTarget();
        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }


    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
        }
    }
}

错误:

06-21 14:18:44.580  10941-11034/? E/BluetoothService﹕ disconnected
java.io.StreamCorruptedException: Wrong format: 0
        at java.io.ObjectInputStream.corruptStream(ObjectInputStream.java:830)
        at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:943)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2262)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2217)
        at com.example.thm_wip1.uno.BluetoothService$ConnectedThread.run(BluetoothService.java:550)

行550: mHandler.obtainMessage(Constants.MESSAGE_READ_OBJECT,-1,-1,mObjectInStream.readObject()

虽然(真)我有两个的try-catch,第一我尝试读取字符串,并在第二个我的对象。我怎样才能在运行方法和字符串对象区分?
我是新来的插座,的InputStream 的OutputStream ..

In the While(true) I have two try-catch, in first I try to read string and in the second my Object. How can I differentiate between String and Objects in the run-method? I'm new to sockets, inputStream, outputStream..

如果您需要更多的细节,我编辑将编辑我的问题。

If you need more details, I edit will edit my question.

推荐答案

StreamCorruptedException当你在设备的内部一致性检查头读取不一致,你的情况的一致性检查失败,因为你要使用多个outputstreams和inputstreams发生中,第一批在tmpout和tmpin定义,然后再次尝试从不同的outputstreams和inputstreams创建ObjectOutputStreams和ObjectinputStreams。这些引起头不一致。

StreamCorruptedException happens when you have header reading inconsistency in the device's internal consistency checks, in your case the consistency check is failing because you are trying to use multiple outputstreams and inputstreams, the first ones are defined in tmpout and tmpin and then you try to create ObjectOutputStreams and ObjectinputStreams from different outputstreams and inputstreams again. These cause header inconsistency.

正确的code应该解决这个问题如下:

The correct code that should fix this is as follows

 try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();

        tmpObjOut = new ObjectOutputStream(tmpOut);
        tmpObjOut.flush();
        tmpObjIn = new ObjectInputStream(tmpIn);

    } catch (IOException e) {
        Log.e(TAG, "temp sockets not created", e);
    }

这篇关于具有读取()和readObject)问题(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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