I do not know at what address I can get to the server, so I have several sockets in several threads, and they try to reach the server each at the same address.
Bad. BAD solution. Follow the logical step to determine the server address. Or do something to help you find out about the IP address of the server.
Only do so if it is your last hope.
My problem is that I do not want to wait for a timeout, but have no idea how to stop sockets and their flows properly.
You have no other timeout option. Socket.connect () blocks. You cannot do anything but wait.
, . , . ( ).
? Thread.stop() , .
, Thread.stop() Thread.interrupt(). .
, close() .
- . - . - 10 .
,
- , a java.net.SocketTimeoutException, Socket . - > 0. - -.
, try-catch-finally . .
, , :
try{
socket = new Socket();
socket.connect(endpoint,timeout);
OutputStream out = socket.getOutputStream();
}
catch(Exception e){
e.printStackTrace();
}
finally{
socket.close();
}