Java:区分InputStream中的输入

时间:2011-11-24 04:58:40

标签: java networking

我有一个InputStream,它在一个线程上运行并读取通过网络传递的任何数据。我的问题是 - 如何区分InputStream对象收到的字节?例如如果接收到的字节指向Car对象,则执行某些操作,如果接收到的字节指向Person对象,则执行其他操作。

感谢。

编辑:这是我的代码片段。看起来好吗?抱歉,我是网络编程新手。

private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final ObjectOutputStream mmObjOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            mmSocket = socket;
            InputStream tmpIn = null;
            ObjectOutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = new ObjectOutputStream(socket.getOutputStream());
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmObjOutStream = tmpOut;
        }

        public void run() {
            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);
                    Log.i(TAG, "PERFORMING MESSAGE READ");
                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(GameboardResourceActivity.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
        }

        /**
         * Write to the connected OutStream.
         * @param buffer  The bytes to write
         */
        public void write(CardResource buffer) {
            try {
                mmObjOutStream.writeObject(buffer);
                System.out.println("Reached here at least........");
                // Share the sent message back to the UI Activity
                mHandler.obtainMessage(GameboardResourceActivity.MESSAGE_WRITE, -1, -1, buffer)
                        .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);
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

你必须知道你的应用程序协议是什么来理解它。听起来好像另一端正在使用序列化,您需要阅读。请参阅Javadoc for ObjectOutputStream和ObjectInputStream。你需要的是ObjectInputStream.readObject(),如果这个假设是正确的。如果不是,您只需要找出他们发送给您的内容并进行相应的操作,可能需要使用DataInputStream来处理各种数据类型。

答案 1 :(得分:1)

您可以将yor套接字输入流直接传递给ObjectInputStream的构造函数:

ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());

while ((obj = inputStream.readObject()) != null) {
    if (obj instanceof Person) {   
        System.out.println(((Person)obj).toString());
    }
}

修改

如注释所述,如果将null值传递到另一端的ObjectOutputStream,则会过早退出。最好是防范这种情况,空值永远不会是一个意外接收的好东西。