title: write from server to multiple outputstreams in java
body: <p>I am trying to write a simple client-server programme. The server will be having 4 separate clients, which means 4 different inputstreams - outputstreams (I am not supposed to use threads). The only thing server is supposed to do, is write a message to each of the clients (one at a time). The problem i am facing, is that the last of the clients cannot get the message from server(The first 3 get the message right). I get the error : "Exception in thread "main" java.util.NoSuchElementException: No line found". </p>
  
  <p>I think i should mention that i run the code many times and some of them the message sent from the server is delivered properly. </p>
  
  <p>Any ideas where the problem is?</p>
  
  <p>Sorry for the bad english
  here is my code:
  For the server::</p>
  
  <hr>
  
  <p>public class SServer {</p>
  
  <pre><code>/**
   * @param args the command line arguments
   */
  private ArrayList&lt;Socket&gt; clients = new ArrayList&lt;Socket&gt;();
  private ArrayList&lt;PrintWriter&gt; pw = new ArrayList&lt;PrintWriter&gt;();
  int port;
  public SServer(int port)
  {
      this.port = port;
  }
  
  public void start() throws InterruptedException
  {
      Socket tmps ;
      try {
          System.out.println("ready to accept connections!");
          ServerSocket serversocket = new ServerSocket(port);
          for(int i=0;i&lt;4;i++)
          {
              tmps = serversocket.accept();
              clients.add(tmps);
              PrintWriter toPlayer1 = new PrintWriter(tmps.getOutputStream(),true);
              pw.add(toPlayer1);
  
          }
          for(int i=0;i&lt;4;i++)
          {
              System.out.println("sending message to client " +i);
              System.out.println(pw.get(i).toString());
              pw.get(i).println("hello client "+i);
              pw.get(i).flush();
          }
  
  
  
      } catch (IOException ex) {
          Logger.getLogger(SServer.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  
  public static void main(String[] args)
  {
      SServer myserver = new SServer(7777);
      try {
          myserver.start();
      } catch (InterruptedException ex) {
          Logger.getLogger(SServer.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And for the client::</p>
  
  <hr>
  
  <p>public class SClient {</p>
  
  <pre><code>/**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
      // TODO code application logic here
       try (Socket echoSocket = new Socket("localhost", 7777);
  
          PrintWriter toServer = new PrintWriter(echoSocket.getOutputStream(), true);
          Scanner fromServer = new Scanner(echoSocket.getInputStream()) )  {
          Scanner keyboard = new Scanner(System.in);
  
  
           String response = fromServer.nextLine();
           System.out.println("Response: " + response);
  
          fromServer.close();
          toServer.close();
      }
      catch (UnknownHostException ex) {
          System.err.println(ex);
      }
      catch (IOException ex)  {
          System.err.println(ex);
      }
  }
  </code></pre>
  
  <p>}</p>