title: How to fix: Error connecting to localhost: Connection Refused in Java
body: <p>I am new to java and I am working my way through a few tutorials. The tutorials don't do much for troubleshooting and I am having troulbe getting one of them to work. I am attempting to connect to my localhost server on windows 8. Here is my code:</p>
  
  <pre><code>import java.net.*;
  public class Connect {
     public static void main(String[] args) {
        String server_name = "localhost";
  
        try {
           Socket sock = new Socket(server_name, 80);
  
           System.out.println("***Connect to " + server_name + " ***");
  
           sock.close();
  
        }
  
        catch (java.io.IOException e ) {
  
           System.out.println("Error connect to " + server_name + 
           ": " + e);
  
           return;
        }
     }
  }
  </code></pre>
  
  <p>and here is my error message when i run the code:</p>
  
  <p>Error connect to localhost: java.net.ConnectException: Connection refused: connect</p>