title: Connection Refused Error when attempting to connect to socket on localhost
body: <p>I'm trying to set up a simple server-client application using a Java server on my PC, and an android application as the client. They are both connected to the same wi-fi network. I'm running into problems trying to get the client to connect to the server socket. I'm using "localhost" as the IP of the server, and have made sure that the server is running and waiting for a connection on port 60101. The port is not in use by any other applications, and is listening.
  The client is throwing the connection refused error when I attempt to connect on this line:</p>
  
  <pre><code>connection = new Socket(serverAddress,serverPort);// ("localhost", 60101)
  </code></pre>
  
  <p>Here is my server code:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class Main {
  
      private static final int portNumber = 60101;
  
      public static void main(String[] args) {
  
          ServerSocket serverSocket = null;
  
          try {
              //starting server
              System.out.println("Server starting at port number: "+portNumber);
              serverSocket = new ServerSocket(portNumber);
  
              //client connecting
              System.out.println("Waiting for clients to connect");
              Socket socket = serverSocket.accept();
              System.out.println("Client has connected.");
  
              //Send message to the client
              BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
              bw.write("this is a message from the server");
              bw.newLine();
              bw.flush();
  
              //Receive message from client
              BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              String data;
              while((data = br.readLine()) != null ){
                  System.out.println("Message from the client: " + data);
  
              }
              System.out.println("Server has ended");
  
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Android MainActivity.java:</p>
  
  <pre><code>package johnedchristensen.androidclienttestapplication;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  
  public class MainActivity extends AppCompatActivity {
      TextView showMessageText;
      Button buttonConnect;
      String serverAddress = "localhost";
      int portNumber = 60101;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          showMessageText = (TextView) findViewById(R.id.textView);
          buttonConnect = (Button) findViewById(R.id.button);
  
          buttonConnect.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
                  Client myClient = new Client(serverAddress, portNumber, showMessageText);
                  myClient.execute();
              }
          });
      }
  }
  </code></pre>
  
  <p>Android Client.java:</p>
  
  <pre><code>package johnedchristensen.androidclienttestapplication;
  
  import android.os.AsyncTask;
  import android.widget.TextView;
  import java.io.EOFException;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.net.Socket;
  
  
  
  public class Client extends AsyncTask&lt;Void,Void,Void&gt;{
      private String serverAddress;
      private int serverPort;
      private TextView messageDisplay;
      private Socket connection;
      private String statusMessage = "";
  
      Client(String address, int port, TextView message){
          serverAddress = address;
          serverPort = port;
          this.messageDisplay = message;
  
      }
  
      @Override
      protected Void doInBackground(Void... params) {
  
  
          try{
              //connection
              statusMessage += "Attempting to connect... \n";
              connection = new Socket(serverAddress,serverPort);
              statusMessage += "Connected to: " + connection.getInetAddress().getHostName();
  
          }catch(EOFException eofException){
              statusMessage +="\n Error Connecting";
          }catch(IOException ioException){
              statusMessage +="\n Error Connecting";
              ioException.printStackTrace();
          }
          return null;
      }
      @Override
      protected void onPostExecute(Void result) {
          messageDisplay.setText(statusMessage);
          super.onPostExecute(result);
      }
  
  }
  </code></pre>
  
  <p>And the Connection refused Error:</p>
  
  <pre><code> W/System.err: java.net.ConnectException: Connection refused
   W/System.err:     at java.net.PlainSocketImpl.socketConnect(Native Method)
   W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
   W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
   W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
   W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
   W/System.err:     at java.net.Socket.connect(Socket.java:586)
   W/System.err:     at java.net.Socket.connect(Socket.java:535)
   W/System.err:     at java.net.Socket.&lt;init&gt;(Socket.java:427)
   W/System.err:     at java.net.Socket.&lt;init&gt;(Socket.java:210)
   W/System.err:     at johnedchristensen.androidclienttestapplication.Client.doInBackground(Client.java:43)
   W/System.err:     at johnedchristensen.androidclienttestapplication.Client.doInBackground(Client.java:19)
   W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:304)
   W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
   W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
   W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
   W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
   W/System.err:     at java.lang.Thread.run(Thread.java:761)
  </code></pre>
  
  <p>The android application has internet and access network state permissions, so I'm at a loss for why it won't connect to the server.</p>