sockets - Android - ObjectInputStream keeps on reading the previous value even with .reset() -
i'm building application sending files between 2 android phones, have listactivity retrieves sdcard , lists files, when listactivity first starts on 2 devices serversocket set , listening .accept()
...
this thread starts when listactivity starts :
receivefilesendrequestthread receivefilesendrequestthread = new receivefilesendrequestthread(); receivefilesendrequestthread.start();
and here full thread class:
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(); } } }
and when onlongclick on item (to send file) following thread starts:
public boolean onitemlongclick(adapterview<?> 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; }
sendfile thread:
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(); } } }
now, when launch listactivity , longclick on item, the file name sent second device, second device pops alertdialog asking user if accept file or not, (accepting file still not ready now) if user presses on cancel (on receivefilesendrequestthread) string sent first device "notosend"
, first user receives string "notosend"
, depending on thread closes socket , re invoke thread listen second peer if wants send file , , pops alertdialog telling first user file refused received ... >>>> totally works >>>> when first device attempts send file (long press again on file [item on list] ) second user receives new file name selected first user , alertdialog pops if accept or cancel first user receives file send refused ... without having second user pressing on cancel button !!! ... don't know why tosendornot = (string) requestfileinclient.readobject();
keeps on taking previous value without waiting second device write new object.
i appreciate if me , many thanks.
after hours of debugging >>> found bug! in thread receivefilesendrequestthread()
boolean
variable oktosend
null when first receiving request second device, when second device cancels request oktosend
set notosend
, whenever second device attempts send file variable oktosend
has assigned value. added oktosend = null;
before while
loop, , working perfectly.
Comments
Post a Comment