title: Java chat - thread listening from server
body: <p>I'm trying to create a simple chat that uses thread to listen for incoming messages. I have a main class that opens up a socket to a server that at the moment just sends one message. I'm having problem listening for the incoming message though, and I cannot figure out what I am doing wrong.</p>
  
  <p>This is the main class, sorry for all the GUI code, it makes it long.</p>
  
  <pre><code>package def;
  
  import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.EventQueue;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  import javax.swing.JFrame;
  import javax.swing.JMenu;
  import javax.swing.JMenuBar;
  import javax.swing.JMenuItem;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.JTextArea;
  import javax.swing.JTextField;
  import javax.swing.UIManager;
  import javax.swing.border.EmptyBorder;
  import java.awt.event.ActionListener;
  import java.awt.event.ActionEvent;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  
  
  public class GUI extends JFrame {
  
      private JPanel contentPane;
      private JTextField textField;
  
      private static JMenuBar menuBar;
      private JMenu fileMenu;
      private JMenuItem openMenu;
      private JMenuItem exitMenu;
  
      private Socket socket;
      private ObjectOutputStream oos;
      private ObjectInputStream ois;
  
      private String ipAddress;
  
      private JTextArea textArea;
      private String fromServer = "";
  
  
      public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
              public void run() {
                  try {
                      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
                  try {
                      GUI frame = new GUI();
                      frame.setVisible(true);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          });
      }
  
      public GUI() throws IOException {
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setBounds(100, 100, 450, 325);
          contentPane = new JPanel();
          contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
          contentPane.setLayout(new BorderLayout(0, 0));
          setContentPane(contentPane);
  
          textArea = new JTextArea();
          contentPane.add(textArea, BorderLayout.NORTH);
          textArea.setPreferredSize(new Dimension(0, 230));
          textArea.setEditable(false);
  
          textField = new JTextField();
          textField.addKeyListener(new KeyAdapter() {
              @Override
              public void keyPressed(KeyEvent arg0) {
                  if(arg0.getKeyCode() ==java.awt.event.KeyEvent.VK_ENTER){
                      // TODO
                  }
              }
          });
          contentPane.add(textField, BorderLayout.SOUTH);
          textField.setColumns(10);
  
  
          // Create menu item
          menuBar = new JMenuBar();
          fileMenu = new JMenu("File");
          openMenu = new JMenuItem("Open chat");
          openMenu.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {
                  ipAddress = JOptionPane.showInputDialog("Ange IP-address: ");
                  try {
                      openSocket(ipAddress);
                  } catch (UnknownHostException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          });
  
          exitMenu = new JMenuItem("Exit");
          exitMenu.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  System.exit(0);
              }
          });
          fileMenu.add(openMenu);
          fileMenu.add(exitMenu);
          menuBar.add(fileMenu);
  
          setJMenuBar(menuBar); //Add the menubar
          setVisible(true);
      }
  
      //Open a socket to the server
      private void openSocket(String ipAddress) throws UnknownHostException, IOException {
          socket = new Socket(ipAddress, 1337);
          new ListenIncoming(socket, this).start();
          System.out.println("Socket and thread started!");
      }
  
      public void setMessage(String message){
          textArea.setText(message);
      }
  }
  </code></pre>
  
  <p>There is an action listener for the "Open chat" button in the menu, where it runs the method openSocket(). In this method, found at the bottom of the code, I open a socket and starts a new ListenIncoming-thread.</p>
  
  <p>This is the thread code:</p>
  
  <pre><code>    import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.net.Socket;
  
  public class ListenIncoming extends Thread{
  
      private Socket socket;
      private ObjectInputStream ois;
      private GUI gui;
  
  
      public ListenIncoming(Socket socket, GUI gui){
          this.socket = socket;
          this.gui = gui;
      }
  
  
      @Override
      public void run() {
          try {
              ois = new ObjectInputStream(socket.getInputStream());
          } catch (IOException e1) {
              e1.printStackTrace();
          }
  
          //A loop that runs forever
          while(true){
              try {
                  String fromServer = ois.readObject().toString();
                  System.out.println(fromServer);
                  gui.setMessage(fromServer);
              } catch (IOException e) {
                  e.printStackTrace();
              } catch (ClassNotFoundException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>This is the server that sends one message. I expected to receive the message "Connected" but I get nothing, no error nor exception. I don't know where my code is wrong.</p>
  
  <pre><code>public class Server {
  
      private static Scanner in;
      private static String toSend;
  
      public static void main(String[] args) throws IOException {
          toSend = "";
          int port = 1337;
          in = new Scanner(System.in);
  
  
          ServerSocket serverSocket = null;
          try {
              serverSocket = new ServerSocket(port);
          } catch (IOException e) {
              System.err.println("Could not listen on port: " + port);
              System.exit(1);
          }
  
          Socket clientSocket = null;
          try {
              clientSocket = serverSocket.accept();
          } catch (IOException e) {
              System.err.println("Could not connect.");
              System.exit(1);
          }
  
          ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
          ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
          String input, output;
  
          output = "Connected!\n";
          oos.writeObject(output);
          System.out.println("This code is reached?");
  
          oos.close();
          ois.close();
          clientSocket.close();
          serverSocket.close();
      }
  }
  </code></pre>
  
  <p>I get the printout from the GUI class when I run the program, though I don't get the debug out prints from the Server class. I'm using Eclipse, but shouldn't both classes be able to write to the console? How come I never reach that code? The problem seem to be at the server side, but wouldn't it generate some error on the client side when trying to read?</p>
  
  <p>Thanks for all the tips and inputs I can get on this!</p>