java - How to implement thread in succession -
i implement transfer socket transfering pictures , check on server if picture ever loaded , if true not transfering picture change imageview.
i have client , server work transfer picture between 2 devices. want send hash of each pictures once @ each time before send them. dificulty on thread manager, have implement listener 1st thread (send / receive picture's hash on client / server) know when it's finished , start second thread (send / receive pictures on client / server). don't know if methodology ok because doesn't work (the server receive hash on start not other).
i hope clear, here parts of code concerned, can give me solution.
server
private class receiverhash extends thread {
private list<tasklistener> listeners = new arraylist<tasklistener>(); public void addlistener(tasklistener listener) { listeners.add(listener); } void gettaskstate(boolean state) { (tasklistener listener : listeners) { listener.gettaskstate(state); } } @override public void run() { try { serversocket serversocket = new serversocket(socketserverport); socket socket = serversocket.accept(); inputstream inputstream = socket.getinputstream(); bufferedreader br = new bufferedreader(new inputstreamreader(inputstream)); while (true) { string str = br.readline(); // lecture du message if (str.equals("end")) { serversocket.close(); gettaskstate(true); break; } hashlist.add(str); log.d("hash_receiver_log", str); // log debug hash } } catch (ioexception e) { e.printstacktrace(); } } } private class receiver extends thread { int pos = 0; int cmpt = 0; @override public void run() { // create serversocket using specified port try { serversocket serversocket = new serversocket(socketserverport); while (true) { //checkfileexist(); file f = new file(getfilesdir(), "img" + integer.tostring(pos) + ".png"); filelist.add(f); log.d("hash_receiver_log", "attente d'une réponse du client"); socket socket = serversocket.accept(); log.d("hash_receiver_log", "reception d'une réponse"); f.createnewfile(); inputstream inputstream = socket.getinputstream(); outputstream outputstream = new fileoutputstream(f); byte[] bytes = new byte[2 * 1024]; int count; while ((count = inputstream.read(bytes)) > 0) { log.d("receiver_cmpt", integer.tostring(cmpt)); outputstream.write(bytes, 0, count); cmpt = cmpt + count; } mybitmap = bitmapfactory.decodefile(f.getabsolutepath()); runonuithread(new runnable() { @override public void run() { imgdiapo.setimagebitmap(mybitmap); } }); } } catch (ioexception e) { e.printstacktrace(); } } }
how use listener on activity :
receiverhash receiverhash = new receiverhash(); receiverhash.start(); receiverhash.addlistener(this);
client
public class sender extends asynctask<void, void, void> { private string dstaddress; private int dstport; private string fpath; public sender(string address, int port, string path) { dstaddress = address; dstport = port; fpath = path; } @override protected void doinbackground(void... arg0) { socket socket = null; try { socket = new socket(dstaddress, dstport); fileinputstream inputstreamsend = new fileinputstream(fpath); outputstream outputstreamsend = socket.getoutputstream(); byte[] buffer = new byte[2 * 1024]; int count; while ((count = inputstreamsend.read(buffer)) > 0) { outputstreamsend.write(buffer, 0, count); } outputstreamsend.close(); inputstreamsend.close(); } catch (ioexception e) { e.printstacktrace(); } { try { if (socket != null) { socket.close(); } } catch (ioexception e) { e.printstacktrace(); } } return null; } public class hashsender extends asynctask<void, void, void> { private string dstaddress; private int dstport; string hash; //private arraylist <string> listhash; private list<tasklistener> listeners = new arraylist<tasklistener>(); public hashsender(string address, int port, string path) throws ioexception, nosuchalgorithmexception { dstaddress = address; dstport = port; hash = sha1(new file(path)); //methode encryption in sha1 } public void addlistener(tasklistener listener) { listeners.add(listener); } void gettaskstate(boolean state) { (tasklistener listener : listeners) { listener.gettaskstate(state); } } @override protected void onpostexecute(void v) { gettaskstate(true); } @override protected void doinbackground(void... arg0) { socket socket = null; try { socket = new socket(dstaddress, dstport); outputstream outputstreamsend = socket.getoutputstream(); bufferedwriter buf = new bufferedwriter(new outputstreamwriter(outputstreamsend)); buf.write(hash); buf.newline(); buf.flush(); log.d("toto", hash); buf.write("end"); buf.newline(); buf.flush(); outputstreamsend.close(); } catch (ioexception e) { e.printstacktrace(); } { try { if (socket != null) { socket.close(); } } catch (ioexception e) { e.printstacktrace(); } } return null; }
how use listener on activity :
hashsender hashsender = new hashsender(dstaddress.get(i), dstport, selecteditempath.get(pos)); hashsender.addlistener(controlsactivity.this); hashsender.execute();
Comments
Post a Comment