3

I want to share object of type Stuff which contains ( String name,address,title, ... and byte[] picture ) when connection is established after that my code hangs on objectinputstream readObject() function . No streaming occurs. Can anyone please figure out where i am doing wrong thing.

private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        private ObjectOutputStream oos = null;
        private ObjectInputStream ois = null;

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

            // Get the BluetoothSocket input and output streams
            try {
                Log.d("connected thread constructor  before inputstream", "");
                tmpIn = socket.getInputStream();
                Log.d("connected thread constructor  inputstream",
                        tmpIn.toString());
                tmpOut = socket.getOutputStream();
                Log.d("connected thread constructor outputstream",
                        tmpOut.toString());
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;

            final BufferedOutputStream bufo = new BufferedOutputStream(
                    mmOutStream);
            final BufferedInputStream bufi = new BufferedInputStream(mmInStream);

            Log.d(TAG, "attempting to create OOS");

            // ********* ObjectOutputStream **********

            try {
                oos = new ObjectOutputStream(bufo);

            } catch (StreamCorruptedException e) {
                Log.d(TAG, "Caught Corrupted Stream Exception");
                Log.w(TAG, e);

            } catch (IOException e) {
                Log.d(TAG, "Caught IOException");
                Log.w(TAG, e);
            }

            // ********* ObjectInputStream **********

            Thread s = new Thread() {
                public void run() {
                    Log.d(TAG, "attempting to create OIS");
                    try {
                        ois = new ObjectInputStream(bufi);
                    } catch (StreamCorruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Log.d(TAG, "completed OIS");
                    if (ois == null) {
                        Log.d(TAG, "OIS is null");
                    }
                }

            };
            s.start();

        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");

My code hang at this point , never move forward .

        `// Keep listening to the InputStream while connected
        while (true) { 
            try {
                Log.d("Connected thread run ", "start while");

                try {

                    Stuff obj_rcv = (Stuff) ois.readObject();
                    Log.d("BTS", "rcv object " + obj_rcv.getName());

                    Message msg2 = mHandler
                            .obtainMessage(RemoteBusinessCard.MESSAGE_READ);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("person", obj_rcv);
                    msg2.setData(bundle);
                    mHandler.sendMessage(msg2);

                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }

    /**
     * Write to the connected OutStream.
     * 
     * @param buffer
     *            The bytes to write
     */
    public void write(Stuff object) {
        try {
            Log.d("BTS", "inside write before" + object.getName());
            oos.writeObject(object);
            Log.d("BTS", "inside write after" + object.getName());
            oos.flush();
            oos.close();

        } 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);
        }
    }
}`
PC.
  • 6,870
  • 5
  • 36
  • 71
Ahmed
  • 814
  • 2
  • 19
  • 38

3 Answers3

1

When you create an ObjectOutputStream you must ensure that the ObjectInputStream at the other end of the socket is also appropriately created because the outputStream always sends a confirmation packet to the inputStream and blocks until it gets an answer.

j0ntech
  • 1,158
  • 3
  • 13
  • 27
  • If ObjectOutputStream is first executed on both master and slave devices then how is that possible ObjectInputStream will recieve the data , as it is not initialized yet !! – Ahmed Feb 06 '12 at 08:16
  • You have to create them in turns. For example the InputStream on the client and OutputStream on the server first and then the OutputStream on the client and InputStream on the server. As in the opposite streams at the same time. – j0ntech Feb 06 '12 at 19:05
1

this is not the way java works. You MUST create two separate threads for input and output streams for your logic to work.

PC.
  • 6,870
  • 5
  • 36
  • 71
0
outStream = new ObjectOutputStream(socket.getOutputStream());
outStream.flush();
inStream = new ObjectInputStream(socket.getInputStream());

create outputstream first, flush and inputstream both in server and client

user2541581
  • 111
  • 1
  • 4