Android Send/Receive UDP every 5 sec -
Android Send/Receive UDP every 5 sec -
this first post @ stackoverflow... dealing android app few weeks ago knoledge limited.
i want send , receive udp pakets enery 5 secs.i found illustration code utilize button that.
that code:class client implements runnable { public client() { }
@override public void run() { seek { string outmessage = txtoutmessage.gettext().tostring(); string outip = txtoutip.gettext().tostring(); int outport = integer.parseint(txtoutport.gettext().tostring()); inetaddress serveraddr = inetaddress.getbyname(outip); datagramsocket socket = new datagramsocket(); byte[] buf = outmessage.getbytes(); datagrampacket packet = new datagrampacket(buf, buf.length, serveraddr, outport); socket.send(packet); byte[] receivedata = new byte[1024]; datagrampacket receivepacket = new datagrampacket(receivedata, receivedata.length); socket.receive(receivepacket); final string recvmsg = new string(receivepacket.getdata()).trim(); runonuithread(new runnable() { @override public void run() { txtinmessage.settext(recvmsg); } }); } catch(exception e) { e.printstacktrace(); } } }
}
please advice in advanced nikos
why not seek using thread? work:
//create thread thread t = new thread() { @override public void run() { while (true) { seek { //do stuff thread.sleep(5000); //thread sleep 5 seconds (5000 milliseconds) } grab (interruptedexception ie) { ie.printstacktrace(); //catch exception thread.sleep } } } }; t.start(); //start thread
i have similar mine checks incoming info ever 100 milliseconds , checks outgoing info every 250 milliseconds, have 1 thread each of those, have separate thread runs every 20 seconds check socket create sure live , socket validation, , create new socket if failed.
android udp
Comments
Post a Comment