title: java.net.ConnectException: Connection refused TCP
body: <blockquote>
    <p><strong>Objective</strong> - I want to send  the entered text in the java (PC) project to the android app which displays this text.The PC is connected to wifi
    hotspot created by the android mobile.</p>
  </blockquote>
  
  <p><strong>The PC/client java project code:</strong></p>
  
  <pre><code>public class EcsDemo {
  
      public static void main(String[] args) {
  
          System.out.println("Enter SSID to connect :");
          Scanner in = new Scanner(System.in);
          String ssid = in.nextLine();
          System.out.println("You entered ssid " + ssid);
          System.out.println("Connecting to ssid ..");
          DosCommand.runCmd(DosCommand.connectToProfile(ssid));
          // netsh wlan connect name=
          System.out.println("initializing tcp client ..");
  
              try {
                  TCPClient.startTCpClient();
              } catch (UnknownHostException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
      }
  }
  
  public class TCPClient {
    public static void startTCpClient() throws UnknownHostException, IOException{
        String FromServer;
        String ToServer;
  
        Socket clientSocket = new Socket("localhost", 5000);
  
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
                System.in));
  
        PrintWriter outToServer = new PrintWriter(
                clientSocket.getOutputStream(), true);
  
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));
  
        while (true) {
  
            FromServer = inFromServer.readLine();
  
            if (FromServer.equals("q") || FromServer.equals("Q")) {
                clientSocket.close();
                break;
            } else {
                System.out.println("RECIEVED:" + FromServer);
                System.out.println("SEND(Type Q or q to Quit):");
  
                ToServer = inFromUser.readLine();
  
                if (ToServer.equals("Q") || ToServer.equals("q")) {
                    outToServer.println(ToServer);
                    clientSocket.close();
                    break;
                } else {
                    outToServer.println(ToServer);
                }
            }
        }
    }
    }
  </code></pre>
  
  <p><strong>Android app/Server code:</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private String TAG = "MainActivity";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          Log.i(TAG, "starting server");
  
          new ServerAsyncTask().execute();
      }
  }
  
  public class ServerAsyncTask extends AsyncTask&lt;Void, Void, Void&gt; {
  
      @Override
      protected Void doInBackground(Void... params) {
          try {
              TCPServer.startTCPServer();// initTCPserver();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return null;
      }
  
  }
  
  
  public static void startTCPServer() throws IOException{
      String fromclient;
      String toclient;
  
      ServerSocket Server = new ServerSocket(5000);
  
      System.out.println("TCPServer Waiting for client on port 5000");
      Log.i("startTCPServer","TCPServer Waiting for client on port 5000");
  
      while (true) {
          Socket connected = Server.accept();
          System.out.println(" THE CLIENT" + " " + connected.getInetAddress()
                  + ":" + connected.getPort() + " IS CONNECTED ");
          Log.i("startTCPServer"," THE CLIENT" + " " + connected.getInetAddress()
                  + ":" + connected.getPort() + " IS CONNECTED ");
  
          BufferedReader inFromUser = new BufferedReader(
                  new InputStreamReader(System.in));
  
          BufferedReader inFromClient = new BufferedReader(
                  new InputStreamReader(connected.getInputStream()));
  
          PrintWriter outToClient = new PrintWriter(
                  connected.getOutputStream(), true);
  
          while (true) {
  
  //            System.out.println("SEND(Type Q or q to Quit):");
  //            toclient = inFromUser.readLine();
  //
  //            if (toclient.equals("q") || toclient.equals("Q")) {
  //                outToClient.println(toclient);
  //                connected.close();
  //                break;
  //            } else {
  //                outToClient.println(toclient);
  //            }
  
  
              fromclient = inFromClient.readLine();
  
              if (fromclient.equals("q") || fromclient.equals("Q")) {
                  connected.close();
                  break;
              } else {
                  System.out.println("RECIEVED:" + fromclient);
              }
  
          }
  
      }
  }
  }
  </code></pre>
  
  <p>After running the android app and then when I run the java project I get the following exception:</p>
  
  <pre><code>java.net.ConnectException: Connection refused: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at java.net.Socket.&lt;init&gt;(Unknown Source)
      at java.net.Socket.&lt;init&gt;(Unknown Source)
      at com.expressecs.javademo.TCPClient.startTCpClient(TCPClient.java:15)
      at com.expressecs.javademo.EcsDemo.main(EcsDemo.java:41)
  </code></pre>
  
  <p>I have referred to the following  links:</p>
  
  <p><a href="https://stackoverflow.com/questions/6876266/java-net-connectexception-connection-refused">java.net.ConnectException: Connection refused</a></p>
  
  <p>Thanks!</p>