title: How to close a server port after we are done using it?
body: <p>Note: I found a similar question here:</p>
  
  <p><a href="https://stackoverflow.com/questions/6147481/how-to-close-port-after-using-server-sockets">How to close port after using server sockets</a></p>
  
  <p>But did not find any satisfactory answer there.</p>
  
  <p>Here is my code for the client program:</p>
  
  <pre><code>package hf;
  
  import java.io.*;
  import java.net.*;
  
  public class DailyAdviceClient 
  {
      private static final int chatPort = 4242;
      public static void main(String[] args)
      {
          DailyAdviceClient client = new DailyAdviceClient();
          client.go();
      }
  
      private void go()
      {
          try
          {
              Socket socket = new Socket("127.0.0.1",chatPort);
              InputStreamReader inputStream = new InputStreamReader(socket.getInputStream());
              BufferedReader bufferedReader = new BufferedReader(inputStream);
  
              String advice = bufferedReader.readLine();
              System.out.println("Advice received by the client for today is "+advice);
  
              bufferedReader.close();
          }
          catch(Exception e)
          {
              System.out.println("Failed to connect to the server");
          }
      }
  }
  </code></pre>
  
  <p>And here is the code for the server program:</p>
  
  <pre><code>package hf;
  
  import java.io.*;
  import java.net.*;
  
  public class DailyAdviceServer 
  {
      private String[] adviceList = {"Take smaller bites",
              "Go for the tight jeans. No they do NOT make you look fat.",
              "One word: inappropriate", 
              "Just for today, be honest. Tell your boss what you *really* think", 
              "You might want to rethink that haircut."};
      private static final int chatPort = 4242;
  
      public static void main(String[] args)
      {
          DailyAdviceServer server = new DailyAdviceServer();
          server.go();
      }
  
      private void go()
      {
          try
          {
              ServerSocket serverSocket = new ServerSocket(chatPort);
  
              while(true)
              {
                  Socket socket = serverSocket.accept();
  
                  PrintWriter writer = new PrintWriter(socket.getOutputStream());
  
                  String advice = getTodaysAdvice();
  
                  writer.println(advice);
                  writer.close();
              }
          }
          catch(Exception e)
          {
              System.out.println("Error in establishing connection with the client");
          }
      }
  
      private String getTodaysAdvice()
      {
          String advice = null;
  
          int randomIndex = (int) (Math.random()*adviceList.length);
          advice = adviceList[randomIndex];
          return advice;
      }
  }
  </code></pre>
  
  <p>In the application, whenever a client program connects to the server program, it receives a String that contains advice for the day.</p>
  
  <p>When I run </p>
  
  <pre><code>netstat -an
  </code></pre>
  
  <p>In the command prompt of my Windows computer as suggested in one of the answers in the aforementioned link, I get a message that the port 4242 is </p>
  
  <pre><code>LISTENING
  </code></pre>
  
  <p>How do I close the port and make it available for future re-use?</p>