java - How to make client socket wait for server socket -
if client socket opens before server socket, java generate connectionexception. have check whether server available , keep waiting before executing
socketchannel.open(hostname, port)
in client thread. i've found related api:
inetaddress.getbyname(hostname).isreachable()
however, still can't tell whether socket on specific port open. think problem should common didn't useful information google , other places.
boolean scanning=true; while(scanning) { try { socketchannel.open(hostname, port); scanning=false; } catch(connectionexception e) { system.out.println("connect failed, waiting , trying again"); try { thread.sleep(2000);//2 seconds } catch(interruptedexception ie){ ie.printstacktrace(); } } } this code sonics comment
Comments
Post a Comment