title: URLConnection class java.net.ConnectException: Connection refused: connect
body: <p>I am programming on a system with client running Java Applet on one end and server on the other. Now I have the client and server on the same computer. But as a client, I cannot see the applet and I get error like below:</p>
  
  <pre><code>        java.net.ConnectException: Connection refused: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(Unknown Source)
      at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at sun.net.NetworkClient.doConnect(Unknown Source)
      at sun.net.www.http.HttpClient.openServer(Unknown Source)
      at sun.net.www.http.HttpClient.openServer(Unknown Source)
      at sun.net.www.http.HttpClient.&lt;init&gt;(Unknown Source)
      at sun.net.www.http.HttpClient.New(Unknown Source)
      at sun.net.www.http.HttpClient.New(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
      at MainJApplet.init(MainJApplet.java:58)
      at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  java.net.ConnectException: Connection refused: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(Unknown Source)
      at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at sun.net.NetworkClient.doConnect(Unknown Source)
      at sun.net.www.http.HttpClient.openServer(Unknown Source)
      at sun.net.www.http.HttpClient.openServer(Unknown Source)
      at sun.net.www.http.HttpClient.&lt;init&gt;(Unknown Source)
      at sun.net.www.http.HttpClient.New(Unknown Source)
      at sun.net.www.http.HttpClient.New(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
      at RequestServer.sendRequest(RequestServer.java:25)
      at createGUI.createEditingBar(createGUI.java:1313)
      at GUI.createAndShowGUI(GUI.java:813)
      at MainJApplet.init(MainJApplet.java:137)
      at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  java.net.ConnectException: Connection refused: connect
  java.lang.NullPointerException
      at createGUI.createEditingBar(createGUI.java:1315)
      at GUI.createAndShowGUI(GUI.java:813)
      at MainJApplet.init(MainJApplet.java:137)
      at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  Exception: java.lang.NullPointerException
  </code></pre>
  
  <p>Below is where the problem traced back:</p>
  
  <pre><code>URL url = new URL(ipAddr);
  URLConnection connection = url.openConnection();
  
  connection.setDoOutput(true);
  OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); // exception happens
  </code></pre>
  
  <p>I checked <a href="http://localhost:8080/" rel="nofollow">http://localhost:8080/</a> and port conflict. I have turn off windows firewall and yet the problem exists. Can someone kindly point me some direction? Any advice would be great! Thanks ahead!</p>
  
  <p>I apologize for the long code. The first is for client.</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.net.*;
  import java.io.*;
  
  public class MainJApplet extends JApplet {
  
      GUI gui = new GUI();
      Thread renewThread;
  
      public void init() {
          //pass parameters from url to applet
          String filePath = this.getParameter("FilePath");
          //temporary solution to bypass the new extention
          String userName = this.getParameter("UserName");
          String ipAddr = this.getParameter("IpAddress");
          String userLevel = this.getParameter("UserLevel");
          String ticket = this.getParameter("Ticket");
          String accessMode = this.getParameter("AccessMode"); //1-normal 2-shared 3-email
  
          //sharedTo will be get from the file path infor
          String sharedTo = this.getParameter("ShareTo");
          String type = this.getParameter("Type");
  
          gui.shareTo = sharedTo;
          gui.ticket = ticket; //this gui.ticket is used to varify email access, will be replaced
  
          String relativePath = "";
          try {
              //connect to servlet
  
              URL url = new URL(ipAddr);
              URLConnection connection = url.openConnection();
  
              connection.setDoOutput(true);
              OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
              //Passing NAME+FILE+TICKET+TYPE to server at the initialization stage
              out.write("IN");
              out.write(userName + "#");
              out.write(filePath + "#");
              out.write(ticket + "#");
              out.write(type + "#");
              out.close();
  
              //Wait for server response
              BufferedReader in = new BufferedReader(
                      new InputStreamReader(
                      connection.getInputStream()));
  
              String dcodedStr;
              StringBuffer decodedStringBuffer = new StringBuffer("");
              while ((dcodedStr = in.readLine()) != null) {
                  decodedStringBuffer.append(dcodedStr);
              }
  
              String dStr = decodedStringBuffer.toString();
              in.close();
  
              if (dStr.equals("NOT_AUTHORIZED")) { //not passing authentication check
                  JOptionPane.showMessageDialog(new JFrame(),
                          "Access Denied",
                          "Warning!",
                          JOptionPane.ERROR_MESSAGE);
                  return;
              } else {//pass authentication check
                  String[] initResp = dStr.split("#");
                  gui.FID = initResp[0]; //file ID
                  gui.EditMode = Boolean.parseBoolean(initResp[1]);
                  gui.userLevel = 1; //temporary set all as normal user
                  boolean firstOpen = Boolean.parseBoolean(initResp[2]);
                  relativePath = initResp[3];
                  if (initResp[4].trim().equals("normal")) {//owner
                      gui.accessMode = 1;
                  } else {//shared user
                      gui.accessMode = 2;
                  }
              }
          }catch(UnknownServiceException exp){
              exp.printStackTrace();
          }catch(IOException ex){
              ex.printStackTrace();
          }catch (Exception e) {
              e.printStackTrace();
          }
  }
  
      public void destroy() {
          gui.check_upon_window_close();
  
          if (gui.is_new_drawing == 0) {
              //2010.9.29 workDir -&gt; FID
              if (gui.EditMode) {
                  RequestServer.canClose(gui.IpAddress, gui.user, gui.FID);
              } else {
                  RequestServer.canCloseNoEdit(gui.IpAddress, gui.user, gui.FID);
              }
          }
          renewThread.stop();
      }
  }
  </code></pre>