title: Java Client and Server connected but not communicating
body: <p>so I'm trying to create a Client - Server chat application and I've got the program already written. The client and server connect through a common port(5000) on localhost. The server and client connect (the console confirms this with a system.out) however when I prompt the server to send a println out to the client to prompt the user to enter a username nothing happens. Please help.</p>
  
  <p>Client:</p>
  
  <pre><code>//ChatClient.java
  
  import javax.swing.*;
  
  import java.awt.FlowLayout;
  
  import java.awt.event.ActionEvent;
  
  import java.awt.event.ActionListener;
  
  import java.io.*;
  
  import java.net.Socket;
  
  public class ChatClient {
  
      static JFrame chatWindow = new JFrame("OneOh");
  
      static JTextArea chatArea = new JTextArea(20, 50);
  
      static JTextField textField = new JTextField(40);
  
      static JLabel blankLabel = new JLabel("           ");
  
      static JButton sendButton = new JButton("Send");
  
      static BufferedReader in;
  
      static PrintWriter out;
  
      static JLabel nameLabel = new JLabel("         ");
  
      private Socket soc;
  
  
  
      ChatClient()
  
      {
  
  
  
          chatWindow.setLayout(new FlowLayout());
  
  
  
          chatWindow.add(nameLabel);
  
          chatWindow.add(new JScrollPane(chatArea));
  
          chatWindow.add(blankLabel);
  
          chatWindow.add(textField);
  
          chatWindow.add(sendButton);
  
          chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          chatWindow.setSize(475, 500);
  
          chatWindow.setVisible(true);
  
  
  
          textField.setEditable(false);
  
          chatArea.setEditable(false);
  
  
  
          sendButton.addActionListener(new Listener());
  
          textField.addActionListener(new Listener());
  
      }
  
  
  
  
  
      void startChat() throws Exception
  
      {
  
         String ipAddress = JOptionPane.showInputDialog(
  
                  chatWindow,
  
                  "Enter IP Address:",
  
                  "IP Address Required",
  
                  JOptionPane.PLAIN_MESSAGE);    
  
  
  
         soc = new Socket(ipAddress, 5000);
  
         in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
  
         out = new PrintWriter(soc.getOutputStream(), true);
  
         while (true)
  
         {
  
           String str = in.readLine();
  
             if (str.equals("NAMEREQUIRED"))
  
             {
  
             String name = JOptionPane.showInputDialog(
  
                         chatWindow,
  
                         "Enter a unique name:",
  
                         "Name Required!!",
  
                         JOptionPane.PLAIN_MESSAGE);
  
  
  
                 out.println(name);
  
  
  
             }
  
             else if(str.equals("NAMEALREADYEXISTS"))
  
             {
  
             String name = JOptionPane.showInputDialog(
  
                         chatWindow,
  
                         "Enter another name:",
  
                         "Name Already Exits!!",
  
                         JOptionPane.WARNING_MESSAGE);
  
  
  
                 out.println(name);
  
             }
  
             else if (str.startsWith("NAMEACCEPTED"))
  
             {
  
                 textField.setEditable(true);
  
                 nameLabel.setText("You are logged in as: "+str.substring(12));
  
  
  
             }
  
             else
  
             {
  
                 chatArea.append(str + "\n");
  
             }
  
         }
  
     }
  
  
  
        public static void main(String[] args) throws Exception {
  
              // TODO Auto-generated method stub
  
              ChatClient client = new ChatClient();
  
              client.startChat();
  
        }
  
  }
  
  class Listener implements ActionListener
  
  {
  
        @Override
  
        public void actionPerformed(ActionEvent e) {
  
              // TODO Auto-generated method stub
  
              ChatClient.out.println(ChatClient.textField.getText());
  
              ChatClient.textField.setText("");
  
        }
  
  
  
  }
  </code></pre>
  
  <p>server:</p>
  
  <pre><code>//ChatServer.java
  
  import java.io.*;
  
  import java.net.*;
  
  import java.util.ArrayList;
  
  public class ChatServer {
  
  
  
        static ArrayList&lt;String&gt; userNames = new ArrayList&lt;String&gt;();
  
        static ArrayList&lt;PrintWriter&gt; printWriters = new ArrayList&lt;PrintWriter&gt;();
  
        public static void main(String[] args) throws Exception{
  
             System.out.println("Waiting for clients..."); 
  
            @SuppressWarnings("resource")
          ServerSocket ss = new ServerSocket(5000);
  
             while (true)
  
             {
  
               Socket soc = ss.accept();
  
                 System.out.println("Connection established");
  
               ConversationHandler handler = new ConversationHandler(soc);
  
               handler.start();
  
             }
  
  
  
        }
  
  }
  
  class ConversationHandler extends Thread
  
  {
  
      Socket socket;
  
      BufferedReader in;
  
      PrintWriter out;
  
      String name;
  
      PrintWriter pw;
  
      static FileWriter fw;
  
      static BufferedWriter bw;
  
  
  
     public ConversationHandler(Socket socket) throws IOException {
  
          this.socket = socket;
  
          fw = new FileWriter("C:\\Users\\Edoardo Sella\\Desktop\\ChatServer-Logs.txt",true);
  
          bw = new BufferedWriter(fw);
  
          pw = new PrintWriter(bw);
  
      }
  
     public void run()
  
     {
  
          try
  
          {
  
              in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
              out = new PrintWriter(socket.getOutputStream());
  
              while (true)
  
              {
  
                 int _count = 0;
  
                 if(_count &gt; 0)
  
                 {
                      out.println("NAMEALREADYEXISTS");
  
                 }
  
                 else
  
                 {
  
                      out.println("NAMEREQUIRED");
  
                 }
  
  
  
                 name = in.readLine();
  
  
  
                 if (name == null)
  
                 {
  
                     return;
  
                 }
  
  
  
                 if (!ChatServer.userNames.contains(name))
  
                 {
  
                    ChatServer.userNames.add(name);
  
                    break;
  
                 }
  
               _count++;
  
             }
  
  
  
              out.println("NAMEACCEPTED"+name);
  
              ChatServer.printWriters.add(out);
  
  
  
              while (true)
  
              {
  
                  String message = in.readLine();
  
  
  
                  if (message == null)
  
                  {
  
                      return;
  
                  }
  
  
  
                  pw.println(name + ": " + message);
  
                  for (PrintWriter writer : ChatServer.printWriters) {
  
                      writer.println(name + ": " + message);
  
                  }
  
              }
  
  
  
          }
  
          catch (Exception e)
  
          {
  
              System.out.println(e);
  
          }
  
     }
  
  }
  </code></pre>
  
  <p>I'm really confused as to why the Client doesn't receive the out.println command any help is appreciated thanks!</p>