Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - ObjectInputStream keeps on reading the previous value even with .reset()
    primarykey
    data
    text
    <p>I'm building an application for sending files between two Android phones, now i have a ListActivity that retrieves the sdcard and lists the files, when the ListActivity first starts on the two devices a ServerSocket is set up and listening with <code>.accept()</code> ...</p> <p>this thread starts when the ListActivity starts :</p> <pre><code>ReceiveFileSendRequestThread ReceiveFileSendRequestThread = new ReceiveFileSendRequestThread(); ReceiveFileSendRequestThread.start(); </code></pre> <p>and here is the full thread class:</p> <pre><code>static public class ReceiveFileSendRequestThread extends Thread { public void run() { ServerSocket serverSocket; try { serverSocket = new ServerSocket(6789, 200); connectionServ = serverSocket.accept(); requestFileInServer = new ObjectInputStream( connectionServ.getInputStream()); requestFileString = (String) requestFileInServer.readObject(); handler.post(new AcceptFileSendAlertDialogRunnable()); while (okToSend == null) { } if (okToSend == true) { requestFileOutServer = new ObjectOutputStream( connectionServ.getOutputStream()); requestFileOutServer.writeObject("oktosend"); requestFileOutServer.flush(); serverSocket.close(); // // Receive File } else if (okToSend == false) { requestFileOutServer = new ObjectOutputStream( connectionServ.getOutputStream()); requestFileOutServer.reset(); requestFileOutServer.writeObject("notosend"); requestFileOutServer.flush(); serverSocket.close(); ReceiveFileSendRequestThread ReceiveFileSendRequestThread = new ReceiveFileSendRequestThread(); ReceiveFileSendRequestThread.start(); } } catch (IOException e) { Log.d("Connection Error:", "Error binding port!"); e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre> <p>And when onLongClick on an item (to send file) the following thread starts:</p> <pre><code>public boolean onItemLongClick(AdapterView&lt;?&gt; parentView, View childView, int position, long id) { // TODO Auto-generated method stub fileClickedName = (((TextView) childView).getText()).toString(); fileClickedPath = file.toString() + "/" + fileClickedName; fileClickedFile = new File(fileClickedPath); SendFileThread SendFileThread = new SendFileThread(); SendFileThread.start(); return true; } </code></pre> <p>SendFile Thread: </p> <pre><code>static public class SendFileThread extends Thread { public void run() { Socket socket = new Socket(); try { socket.connect(new InetSocketAddress(sharedIp, 6789), 200); requestFileOutClient = new ObjectOutputStream( socket.getOutputStream()); requestFileOutClient.writeObject(fileClickedName); requestFileOutClient.flush(); requestFileInClient = new ObjectInputStream( socket.getInputStream()); toSendOrNot = (String) requestFileInClient.readObject(); if (toSendOrNot.equals("oktosend")) { socket.close(); } else if (toSendOrNot.equals("notosend")) { socket.close(); ReceiveFileSendRequestThread ReceiveFileSendRequestThread = new ReceiveFileSendRequestThread(); ReceiveFileSendRequestThread.start(); handler.post(new ClientFileSendAlertDialogRunnable()); } // // } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre> <p>Now, when I launch the ListActivity and longClick on an item, the the file name is sent to the second device, the second device pops up an alertDialog asking the user if to accept the file or not, (accepting file is still not ready for now) if the user presses on CANCEL (on the ReceiveFileSendRequestThread) a string is sent to the first device <code>"notosend"</code>, the first user receives the string <code>"notosend"</code> and depending on that the thread closes the socket and re invoke the thread to listen to the second peer if he wants to send another file , AND pops up an alertDialog telling the first user that your file was refused to be received ... >>>> this is totally works >>>> but when the first device attempts to send another file (long press again on a file [item on the list] ) the second user receives the new file name selected by the first user successfully and alertDialog pops up if to accept or cancel BUT the first user receives that the file send was refused ... without having the second user pressing on the cancel button !!! ... i don't know why <code>toSendOrNot = (String) requestFileInClient.readObject();</code> keeps on taking the previous value without even waiting for the second device to write the new object.</p> <p>I would appreciate it if someone could help me with this , Many thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI have this problem too.. I read that ObjectInputStream and ObjectOutputStream have a cache memory.. the reset method should clear the cache, so the new data will pass through it.. in my case it works but it's when I send a new message it duplicate the message.. here is my post.. but no one answered.. mabye the code there will give some idea of how to fix your proble, http://stackoverflow.com/questions/16318526/reset-method-on-objectoutputstream-duplicate-the-message
      singulars
    2. CO@Elior , thank you for your contribution, I traced your code but unfortunately nothing helped me ... and yeah about the cache I also read about it, and could be reset by using: `.reset()` and its main purpose is to: Reset the state of the stream and to avoid cyclical reference, and the way i did is supposed to be working, but no chance ... am working on it, but so far I found no solution.
      singulars
    3. COToo much unrunnable code here. Reduce it to a standalone test case. That process may remove the bug, which would itself be instructive. I note that you're creating a new `ObjectOutputStream` per send in the `ReceiveFileSendRequestThread`. That alone can cause problems. You must use the same `ObjectInput/OutputStreams` for the life of the socket, at both ends.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload