title: client ends connection or server?
body: <p>I'm programming a client server app. I'm wondering to end the connection, which side should decide to end it, client or server?</p>
  <p>Then if for example the client did it, should the server still keep open or not?</p>
  <p>Suppose we have the below codes for each client and server:</p>
  <pre><code>           socket = new Socket(&quot;127.0.0.1&quot;,3000);
  ........
  
              socket.close();
  </code></pre>
  <p>or</p>
  <pre><code>            serverSocket = new ServerSocket(3000);
              socket = serverSocket.accept();
  ......
  
              serverSocket.close();
  </code></pre>
  <p>EDIT:
  according to the matt's answer, i pot my code here and let see why my code doesnt work:</p>
  <p>generally, i have a Jframe program as client which will connect to a server and while its open, i want the connection being alive, since it send info to the server and server should response for the result. but if i dont use closing the socket from server it gives an error and if i use, after once calculation, it closes the connection:</p>
  <h1>Server</h1>
  <pre><code>        private static PrintWriter toClient;
          private static BufferedReader fromClient;
  
  
    public static void run() throws IOException, SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException, SAXException, XPathExpressionException
          {
    
          
          System.out.println(&quot;Server is waiting for connections...&quot;);
     
          while(true)
          {
                   openStreams();
                    processClient();
                    closeStreams();
   }
       
      }
  </code></pre>
  <p>OpenStream:</p>
  <pre><code>  public static void openStreams() throws IOException, SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException, SAXException, XPathExpressionException
              {
              serverSocket = new ServerSocket(3000);
              socket = serverSocket.accept();
                  System.out.println(&quot;Connected&quot;);
  
               toClient = new PrintWriter(socket.getOutputStream(),true);
              
              fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              
  
              }
  </code></pre>
  <p>Closestearms:</p>
  <pre><code>   public static void closeStreams() throws IOException
              {
              fromClient.close();
              toClient.close();
              socket.close();
              serverSocket.close();
                  System.out.println(&quot;Disconnected&quot;);
          }
            
  </code></pre>
  <p>the error i receive if i remove the <code>closestream();</code></p>
  <pre><code>at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:359)
      at java.net.ServerSocket.bind(ServerSocket.java:319)
      at java.net.ServerSocket.&lt;init&gt;(ServerSocket.java:185)
      at java.net.ServerSocket.&lt;init&gt;(ServerSocket.java:97)
  </code></pre>