title: Simple String-sending client server handler Java
body: <p>I would like to programme a very simple Server-Client pair in Java, meaning that I have a server, available under localhost/port showing Strings. So if I open the website, it shows something like:</p>
  <pre><code>Hello World
  Hi
  How are you?
  </code></pre>
  <p>The clients are all using the same programme, but they all see exactly the same page. Every client can append the current page by several lines when they want.</p>
  <p>I tried to research a lot, but haven't found a good example for it.</p>
  <p>At the moment, my programmes look like that:
  Server:</p>
  <pre><code>import java.io.DataInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class DomServer {
      public static void main(String[] args) throws IOException {
          // don't need to specify a hostname, it will be the current machine
          ServerSocket ss = new ServerSocket(7777);
          System.out.println(&quot;ServerSocket awaiting connections...&quot;);
          Socket socket = ss.accept(); // blocking call, this will wait until a connection is attempted on this port.
          System.out.println(&quot;Connection from &quot; + socket + &quot;!&quot;);
  
          // get the input stream from the connected socket
          InputStream inputStream = socket.getInputStream();
          // create a DataInputStream so we can read data from it.
          DataInputStream dataInputStream = new DataInputStream(inputStream);
  
          // read the message from the socket
          String message = dataInputStream.readUTF();
          System.out.println(&quot;The message sent from the socket was: &quot; + message);
  
          System.out.println(&quot;Closing sockets.&quot;);
          //ss.close();
          //socket.close();
      }
  }
  </code></pre>
  <p>Client:</p>
  <pre><code>import java.io.BufferedReader;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.net.Socket;
  
  public class DomClient {
      public static void main(String[] args) throws IOException {
          // need host and port, we want to connect to the ServerSocket at port 7777
          Socket socket = new Socket(&quot;localhost&quot;, 7777);
          System.out.println(&quot;Connected!&quot;);
  
          // get the output stream from the socket.
          OutputStream outputStream = socket.getOutputStream();
          // create a data output stream from the output stream so we can send data
          // through it
          DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
  
          System.out.println(&quot;Sending string to the ServerSocket&quot;);
  
          // write the message we want to send
          boolean terminate = false;
          BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
           
          while(!terminate) {
              String text = reader.readLine();
              if(text.equals(&quot;end&quot;)) {
                  terminate = true;
              }
              dataOutputStream.writeUTF(text);
              dataOutputStream.flush(); // send the message
              
          }
          dataOutputStream.close(); // close the output stream when we're done.
  
          System.out.println(&quot;Closing socket and terminating program.&quot;);
          socket.close();
      }
  }
  </code></pre>
  <p>Its a modified copy of that one: <a href="https://gist.github.com/chatton/8955d2f96f58f6082bde14e7c33f69a6" rel="nofollow noreferrer">https://gist.github.com/chatton/8955d2f96f58f6082bde14e7c33f69a6</a></p>
  <p>At the moment, I am suffering from the problem that the connection closes itself when I type in a third line. I am getting java.net.SocketException: Broken pipe (Write failed).
  Moreover, I do not have an idea on how to solve the problem with the expendable string website.</p>
  <p>Is there somebody having programmed something similar?</p>