title: Java - Address already in use (net bind)
body: <p>I know this question has been asked a million times but I haven't found a situation like mine.  I have a simple chat application in java that was working a few months ago and I've come back to it and now I'm having problems.  When I run my server.java in eclipse, everything works fine.  When I run the client.java file afterwards, I get the following: </p>
  
  <pre><code>Exception in thread "main" java.net.BindException: Address already in use: NET_Bind
      at java.base/java.net.DualStackPlainSocketImpl.bind0(Native Method)
      at java.base/java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
      at java.base/java.net.AbstractPlainSocketImpl.bind(Unknown Source)
      at java.base/java.net.PlainSocketImpl.bind(Unknown Source)
      at java.base/java.net.ServerSocket.bind(Unknown Source)
      at java.base/java.net.ServerSocket.&lt;init&gt;(Unknown Source)
      at java.base/java.net.ServerSocket.&lt;init&gt;(Unknown Source)
      at chat.Server.main(Server.java:18)
  </code></pre>
  
  <p>I've terminated all launches in eclipse, found the PID in command prompt and killed it, tried changing the port numbers, and I still get the address still in use every time I try to run my programs. I've also tried running the netstat -a command in command prompt to find the port numbers in use, none of which have been ones I've tried.</p>
  
  <p>Server.java(contains server code as well as a client handler class):</p>
  
  <pre><code>public class Server 
  {
      //vector to store active clients, static so threads share the vector of clients
      static Vector&lt;ClientHandler&gt; activeUsers = new Vector&lt;&gt;();
  
      public static void main(String[] args) throws IOException 
      {
          ServerSocket serverSocket = new ServerSocket(1234); //server is listening on port 1234
          Socket s;   
          while (true) //infinite loop for getting client request
          {
              s = serverSocket.accept(); //accept the incoming request
  
              //obtain input and output streams
              DataInputStream dis = new DataInputStream(s.getInputStream());
              DataOutputStream dos = new DataOutputStream(s.getOutputStream());
  
              String username = dis.readUTF(); //username is first thing sent from the client for naming
              ClientHandler newClient = new ClientHandler(s,username , dis, dos); //create a new handler object for handling this client
  
              //notify currently connected users that a new user has joined
              newClient.notifyUsers(activeUsers, " has joined the server!");
  
              Thread t = new Thread(newClient); //create a new Thread with this object.
              activeUsers.add(newClient); //add this client to active clients list
              t.start(); //start the thread.
          }//end while
  
  
      }//end main
  }//end class Server
  
  class ClientHandler implements Runnable 
  {
      private String name;
      DataInputStream dis;
      DataOutputStream dos;
      Socket socket;
      boolean isloggedin;
  
  
      public ClientHandler(Socket socket, String name, DataInputStream dis, DataOutputStream dos) 
      {
          this.dis = dis; //data input stream
          this.dos = dos; //data output stream
          this.name = name; //client name
          this.socket = socket; //socket
          this.isloggedin=true; //handler is being created so user is logged in
      }
  }
  </code></pre>
  
  <p>Client.java:</p>
  
  <pre><code>public class Client extends JFrame implements ActionListener
  {
      DataInputStream dis = null;
      DataOutputStream dos = null;
      final static int ServerPort = 1234;
      JButton     sendMessage;
      JTextField  messageBox;
      JTextArea   chatBox;
      JButton     logoutButton;
      JButton     activeUsersButton;
      JFrame      newFrame    = new JFrame(".Chat");
      String username;
      Socket s = null;
  
  
      public Client(String user)
      {
  
          this.username = user;
      }
  
      public void start() throws UnknownHostException, IOException 
      {
          newFrame.setTitle("chat - " + username);
  
          InetAddress ip = InetAddress.getByName("localhost");
  
          //establish the connection to the server
          s = new Socket(ip, ServerPort);
  
          //obtaining input and out streams
          dis = new DataInputStream(s.getInputStream());
          dos =  new DataOutputStream(s.getOutputStream());
  
          dos.writeUTF(username);
  
          //readMessage thread
          Thread readMessage = new Thread(new Runnable() 
          {
              @Override
              public void run() 
              {
                  while (dos != null &amp;&amp; dis != null) {
                      try 
                      {
                          //read the message sent to this client
                          String msg = dis.readUTF();
                          System.out.println(msg);
                          chatBox.append(msg);
                      } //end try
                      catch (IOException e) 
                      {
  
                          e.printStackTrace();
                      }//end catch
                  }//end while
              }//end run
          });
          display();
          readMessage.start();
      }
  
  
      //not sure what this needs to do? just to satisfy an error
      @Override
      public void actionPerformed(ActionEvent e) {
          // TODO Auto-generated method stub
           Object obj = e.getSource();
             if(obj == sendMessage){ 
                  String msg = messageBox.getText();
                  try {
                      dos.writeUTF(msg);
                      chatBox.append(username + ": " + msg + "\n"); //place what the user sent in the text box
                      messageBox.setText(""); 
                  } catch (IOException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
             } 
             else{}
      }
  }
  </code></pre>
  
  <p>I've left out the button code as its only sending/receiving messages on the sockets as well as the UI code.</p>