title: Make server keep listening for inputstreams using DataInputStream
body: <p>I created a java class TCPClient which sends Message objects to the TCPServer. I use DataOutputStream and DataInputstream (This is a task we were told to do so, we have to use both of them in order to send an Object from client to server).
  This is my TCPClient class</p>
  
  <pre><code>public class TCPClient {
  public static final int PORT = 4000;
  public static void main(String[] args){
      String hostname = "localhost";
      try(Socket client = new Socket(hostname,PORT);
          //BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
          BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
  
          Message message;
          String input;
          String the_message;
          double random;
          int id;
          while(!(input = stdIn.readLine()).equalsIgnoreCase("end")){
              random =  Math.random()*100;
              message = new Message();
              the_message = input;
              id = (int)random;
              message.setId(id);
              message.setMessage(the_message);
              encode(message,client);
              //System.out.println(in.readLine());
          }
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  private static void encode(Message message, Socket client) {
      try(DataOutputStream out = new DataOutputStream(client.getOutputStream())){
          out.writeUTF(message.getMessage());
          out.writeInt(message.getId());
      } catch (IOException e) {
          e.printStackTrace();
      }
  }}
  </code></pre>
  
  <p>TCPServer class</p>
  
  <pre><code>public class TCPServer {
  public static final int PORT = 4000;
  
  public static void main(String[] args) {
      try (ServerSocket ss = new ServerSocket(PORT);
           Socket cs = ss.accept();
           //PrintWriter out = new PrintWriter(cs.getOutputStream(), true);
      ) {
          Message message = decode(cs);
          System.out.print("Message received: " + message.getMessage() + " id: " + message.getId());
  
  
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  
  }
  private static Message decode(Socket client){
      Message message = new Message();
      try(DataInputStream in = new DataInputStream(client.getInputStream())){
         message.setMessage(in.readUTF());
          message.setId(in.readInt());
          return message;
      } catch (IOException e) {
          System.err.print("Error happened while decoding object");
          return null;
      }
  }}
  </code></pre>
  
  <p>What I DONT want to do is that when I send a new message from Client the Server closes. I want the server to keep listening for Messages. How do I accomplish that?</p>
  
  <p>Tried something like this</p>
  
  <pre><code>while(cs.getInputStream().available()==0){
              Message message = decode(cs);
              System.out.println("Message received = " + message.getMessage() + " id " + message.getId());
          }
  </code></pre>
  
  <p>But didnt work</p>
  
  <p>EDIT: I dont want to build a multi-threaded server to host several clients. I want a single server to receive several messages from a unique client and close the socket whenever the client sends "end", I dont know if thats possible.</p>