title: Java Server Socket Response
body: <p>I'm trying to create a simple client/server socket communication application (chat client). I've spent countless hours looking on how to fix this with still no luck, I can send the message to the server but I'm stuck with sending the message back from the server to the client.</p>
  
  <p>I believe the issue is how I'm getting the message from the server after it's sent back, I deleted what I had which was an InputStreamReader which I couldn't get to work.</p>
  
  <p><em>(I apologize in advance for the sloppy code)</em></p>
  
  <p><strong>Server.java</strong></p>
  
  <pre><code>public class Server extends Thread {
  @SuppressWarnings("unused")
  private static Socket socket;
  static int port = 1337;
  static ObjectOutputStream output;
  
  @SuppressWarnings("resource")
  public static void main(String[] args) throws IOException{  
      ServerSocket ss = new ServerSocket(port);
      System.out.println("Server started on port: " + port);
      while(!Thread.interrupted()){
          try {  
              Socket clientSocket = ss.accept();
              DataInputStream dis = new DataInputStream(clientSocket.getInputStream()); 
              PrintStream output = new PrintStream(clientSocket.getOutputStream());
              String str = (String)dis.readUTF();
              String[] split = str.split("-");
              String subStringUsername = split[0];
              String subStringMessage = split[1];
              Date date = new Date();
              SimpleDateFormat sdf = new SimpleDateFormat("h:mm:ss a");
              String formattedTimestamp = sdf.format(date);
              System.out.println("Message from " + subStringUsername + ": " + subStringMessage + " - at " + formattedTimestamp);
              output.flush();
  
              output.println("Message received! Hello client!");
              System.out.println("Reply sent");
              output.flush();
  
              //TODO create new thread handle new users instead of Thread sleep
              //TODO chat commands and user ID / can't be existing user
  
              Thread.sleep(500);
          }
          catch(Exception e){
              System.out.println(e);
          } 
      }
  }
  </code></pre>
  
  <p><strong>getMessage.java</strong></p>
  
  <pre><code>public class GetMessage extends Thread {    
      public void run(){
          while(true) {
              InputStreamReader be = new InputStreamReader();
          }
      }
  }
  </code></pre>
  
  <p>This is what I have left of the getMessage class as I deleted everything in frustration, I'm running getMessage as a thread which I don't know is the best way or not. I've tried 10's of methods to get the message from the server with still not luck, if someone could point me in the right direction I would be greatly appreciative.</p>