title: java.net.ConnectException: Connection refused
body: <p>I'm trying to implement a TCP connection, everything works fine from the server's side but when I run the client program (from client computer) I get the following error:</p>
  
  <pre><code>java.net.ConnectException: Connection refused
          at java.net.PlainSocketImpl.socketConnect(Native Method)
          at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
          at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
          at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
          at java.net.Socket.connect(Socket.java:529)
          at java.net.Socket.connect(Socket.java:478)
          at java.net.Socket.&lt;init&gt;(Socket.java:375)
          at java.net.Socket.&lt;init&gt;(Socket.java:189)
          at TCPClient.main(TCPClient.java:13)
  </code></pre>
  
  <p>I tried changing the socket number in case it was in use but to no avail, does anyone know what is causing this error &amp; how to fix it.</p>
  
  <p>The Server Code:</p>
  
  <pre><code>//TCPServer.java
  
  import java.io.*;
  import java.net.*;
  
  class TCPServer {
      public static void main(String argv[]) throws Exception {
          String fromclient;
          String toclient;
  
          ServerSocket Server = new ServerSocket(5000);
  
          System.out.println("TCPServer Waiting for client on port 5000");
  
          while (true) {
              Socket connected = Server.accept();
              System.out.println(" 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>The Client Code:</p>
  
  <pre><code>//TCPClient.java
  
  import java.io.*;
  import java.net.*;
  
  class TCPClient {
      public static void main(String argv[]) throws Exception {
          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>