Java - How to measure a time out -
i making ping program using java sockets. 1 bug in program not connect , sit there ever. trying add timeout (after twenty seconds) , ping fail. have no idea how to.
here part of ping program:
boolean result = false; long before1 = system.nanotime(); out.println(new byte[64]); system.out.println("(1) sent 64 bytes of data " + address + "..."); try { if ((in.readline()) != null) { int size = in.readline().tostring().getbytes().length; long after = system.nanotime(); long s = ((after - before1) / 1000000l) / 1000; system.out.println("(1) recieved reply " + address + " (" + size + " bytes), time = " + s + " seconds..."); result = true; } else if ((in.readline()) == null) { long after = system.nanotime(); long s = ((after - before1) / 1000000l) / 1000; system.out.println("(1) failed recieve reply " + address + ", time = " + s + " seconds..."); result = false; } } catch (ioexception exc) { long after = system.nanotime(); long s = ((after - before1) / 1000000l) / 1000; system.err.println("(1) failed recieve reply " + address + ", time = " + s + " seconds...\nreason: " + exc); result = false; }
but measure time elapsed in code, instead of:
long time = system.nanotime();
if 1 part of code stuck doing time out after 20 seconds.
suggestions on how measure if twenty seconds has passed @ start of try/catch block or anywhere else in code doesn't stuck during ping?
as "jsn" , "jahory" said need threads. here's 2 useful links, can check them ;)
Comments
Post a Comment