title: how to tell server has sent data successfully in socket programming?
body: <p>i am working on a problem on socket programming in JAVA.
  There is a server and a client.<br><br>
  1) server is connected to client<br>
  2) server sends N no of Strings which are stored in an array on server side(obviously ;)).<br>
  3)Client doesn't know the size of array<br>
  4)Server receives Strings from server one by one.<br>
  5)When Client reads all the Strings it sends one msg to server<br>
  6)Server receives the msg.<br>
  7)This process goes on(step 2-step 6) for multiple times.<br></p>
  
  <p>The problem i am facing is, Client does not know when server sends the last String and it is waiting for its turn.<br>
  I have solved this problem using:<br>
  a)Multi threading.<br>
  b)Telling size of array to client at the beginning of the first msg    </p>
  
  <p>I want to know is there any in-built function which indicates if the server has stoped sending data?</p>
  
  <p>here is the code for 1 iteration(step 1-step 6)</p>
  
  <p><strong>Server code</strong>:</p>
  
  <pre><code>public class server {
  static String[] a;
  static DataOutputStream dos;
  static DataInputStream dis;
  static ServerSocket server;
  static Socket socket;
  
  public static void main(String[] args)
  {
  
      a=new String[]{"String1","String2","String3"};
  
  
       try {
  
          server=new ServerSocket(8080);
  
          socket=server.accept();
          dos=new DataOutputStream(socket.getOutputStream());
          dis=new DataInputStream(socket.getInputStream());
  
          ///sending array values
  
          //String temp=null;
  
          for(int i=0;i&lt;a.length;i++)
          {
              dos.writeUTF(a[i]);
          }
  
          String msg_from_client=dis.readUTF();
          System.out.println(msg_from_client);
  
  
  
      } catch (IOException e) {
  
          e.printStackTrace();
      }
  
  }
  
  
  }
  </code></pre>
  
  <p><strong>Client code</strong>:</p>
  
  <pre><code>public class client {
  
  
  static String[] a;
  static DataOutputStream dos;
  static DataInputStream dis;
  static Socket socket;
  static Scanner sc;
  
  public static void main(String[] args)
  {
      try {
          socket=new Socket("127.0.0.1",8080);
          System.out.println("connected");
          dos=new DataOutputStream(socket.getOutputStream());
          dis=new DataInputStream(socket.getInputStream());
          sc=new Scanner(System.in);
  
          //reading from server i dont know what is the size of array at server side
          String temp=null;
          while((temp=dis.readUTF())!=null)
          {
              System.out.println(temp);
          }
          System.out.println("out of the loop");
  
          ////now client sends the msg;
  
          String msg=sc.nextLine();
          dos.writeUTF(msg);
  
          System.out.println("sent");
  
  
  
  
      } catch (UnknownHostException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
  
  }
  }
  </code></pre>
  
  <p>output at client side:</p>
  
  <blockquote>
    <p>connected<br>     String1<br>      String2<br>      String3</p>
  </blockquote>