title: Java Socket Connection initiated through web application, reopens when server restarts
body: <p>I have a web application running on a Tomcat 6, installed on a Windows Server 2003. This application opens socket connections with at different server to retrieve some data. This application has been working for months. But in two occurrences for some reason the web application stopped working. In both occurrences running the netstat command (with -ano) showed  about 4.000 tcp connections (on TIME_WAIT state) on the port that the web app is connecting through the socket connection.</p>
  
  <p>The strange thing starts here: I stop tomcat, after a while those connections drop (running netstat again). I restart tomcat and these connections are all back on TIME_WAIT state!!!</p>
  
  <p>I have no clue why these connections are reopened. It might be something very obvious that I am missing. Any ideas?</p>
  
  <p>Thank you.</p>
  
  <p>EDIT:</p>
  
  <p>Here is the code that does the socket management:</p>
  
  <pre><code>    public void openSocketConnection() throws Exception {
      socket = createSingleSocket();
      socket.setSoTimeout(5000);
      out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
      in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
  }
  
  /**
   * Closes socket and output input streams.
   */
  public void closeSocketConnection() throws IOException {
      socket.close();
      out.close();
      in.close();
  }
  
  /**
   * Writes the input to the socket and returns the output response
   * 
   * @param input
   *            a String to write on the socket
   * @return output a String that the socket returns
   * @throws Exception
   */
  public String writeReadSocket(String input) throws Exception {
      openSocketConnection();
      out.println(input);
      logger.debug("Socket Input:" + input);
      String output = in.readLine();
      logger.debug("Socket output:" + output);
      closeSocketConnection();
      return output;
  }
  
  /**
   * Method overiden by Spring. We use the method injection technique, so that we have a new socket instance in every call to openSocketConnection()
   * method
   */
  public abstract Socket createSingleSocket();
  </code></pre>
  
  <p>The method that is called is writeReadSocket.</p>
  
  <p>The last method (createSingleSocket) is used by spring, so I can have a new Socket instance for every call to openConnection() method (by the way, I am not sure if this is correct to have a new Socket for every request). In a Spring configuration file I have this:</p>
  
  <pre><code>&lt;!--  prototype scope is used to instantiate a new socket instance in every createSingleSocket() method call. --&gt;
  &lt;bean id="socket" class="java.net.Socket"  scope="prototype"&gt;
      &lt;constructor-arg type="String" value="${socket.url}"/&gt;
      &lt;constructor-arg type="int" value="${socket.port}"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="socketConnector" class="gr.diassa.dsjsonrpcsuite.socket.SocketConnector"&gt;
      &lt;lookup-method name="createSingleSocket" bean="socket"/&gt;
  &lt;/bean&gt;
  </code></pre>