title: Stopping blocking sockets in java
body: <p>I have a java socket calling a server. However, I do not know at which address I can reach the server, so I put several sockets in several threads and they try to reach the server each on one address. My probem is that I do not want to wait for the timeout but have no idea how to stop the sockets and their threads properly.</p>
  
  <p>Code:</p>
  
  <pre><code>socket = new Socket();
  socket.connect(endpoint, timeout); // **Blocking method**
  OutputStream out = socket.getOutputStream();
  //Write Data here
  </code></pre>
  
  <p>How can I interrupt the operation? I consider <code>Thread.stop()</code> a bad style and it also does not work properly. <em>.NET</em> Tcp Endpoints have a non-blocking <code>pending</code> method that allows uinsg boolean flags but I could not find something similiar</p>