title: Client Socket cannot find Server Socket. Throws UnknownHostException despite having the same port number
body: <p>I'm following an online tutorial to get used to clients&amp;servers in Java before I really start working with them.</p>
  
  <p>All I am trying to do right now is start the server and then connect the client. It's only a couple lines of code for the client and the server and it all seemed very straightforward to me, so I'm not sure what the problem is.</p>
  
  <p>I run my server program, then run my client program, but the client socket cannot find my server socket for some reason even though they have the same port number. </p>
  
  <p>Here is the code.</p>
  
  <p>Server:</p>
  
  <pre><code>public class Server {
  
  
       private ServerSocket serverSocket;
       private int portNumber;
  
       public Server(int portNumber){
          this.portNumber=portNumber;
      }
  
      public void run() throws IOException {
          serverSocket = new ServerSocket(portNumber);
          System.out.println("server at port "+portNumber);
          System.out.println("Waiting for client.");
          Socket clientSocket = serverSocket.accept();
          System.out.println("Connected.");
      }
  
       public static void main(String[] args) {
               int portNumber = 9999;
               try {
                  Server serverSocket = new Server(portNumber);
                   serverSocket.run();
               }
               catch(IOException e){
                   e.printStackTrace();
               }
           }
      }
  </code></pre>
  
  <p>And then my client program:</p>
  
  <pre><code>public class Client {
      private int portNumber;
      private String host;
      Socket clientSocket;
  
  
      public Client(int portNumber,String host){
          this.portNumber=portNumber;
          this.host=host;
      }
  
      public void connect() throws IOException, UnknownHostException {
          System.out.println("Connecting to port "+portNumber);
          clientSocket = new Socket(host,portNumber);
          System.out.println("Connected");
      }
  public static void main(String arg[]){
              Client clientSocket = new Client(9999,"host");
              try {
                  clientSocket.connect();
              }
              catch(UnknownHostException e){
                  System.err.println("Can't connect to host");
              }
              catch (IOException e) {
                  System.err.println("Can't connet. "+e.getMessage());
  
              }
  
          }
      }
  </code></pre>
  
  <p><code>UnknownHostException</code> is thrown when I run the client program. </p>
  
  <p>I thought I followed the tutorial pretty closely, but I don't know what the problem with my code is. </p>
  
  <p>I would guess it had something to do with my <code>connect()</code> method because that's where the exception is thrown, but I can't say for sure.</p>
  
  <p>Any ideas? </p>