title: how to pass audio between sockets in java
body: <p>Im try to pass audio between sockets but it pass one time in client than clients stops working.
  I think the way im writing and sending may be wrong please help me regarding this code
  Following is my server code :</p>
  
  <pre><code>  public void run(){
      try{
              SetInitialSettings();
              int bufferSize = (int) format.getSampleRate() * (int) format.getFrameSize();
              System.out.println("length :" +bufferSize);
              out = new ByteArrayOutputStream();
              readagain=true;
              while(record){
              int count=0;
  
              for(int i=0;i&lt;1000;i++)
              {
  
                         if(buffer==null){
                         buffer = new byte[bufferSize];
                         }
                         count = line.read(buffer, 0, buffer.length);
                         if (count &gt; 0) {
                         //out.write(buffer, 0, count);
                         //readagain=false;
                             for (int j=0;i&lt;ser.clientReceiver.audioSender.size();j++){
                              ser.clientReceiver.audioSender.get(i).cliOut.write("Audio".getBytes());
                              ser.clientReceiver.audioSender.get(i).cliOut.write('\n');
                              ser.clientReceiver.audioSender.get(i).cliOut.flush();
                              ser.clientReceiver.audioSender.get(i).cliOut.write(buffer,0,count);
                              ser.clientReceiver.audioSender.get(i).cliOut.flush();
                              Thread.sleep(2000);
  
                                 }
                         }
  
              }            
  
  
  
  
      }
      }
      catch(Exception ex)
      {
          System.out.println(ex.getMessage());
  
      }
  
  
  
  
      }
  </code></pre>
  
  <p>and following is my client code :</p>
  
  <p>public void run(){</p>
  
  <pre><code>    try{
          //audioReceiver = new TAudioReceiver(cli);
          //audioReceiver.start();
          while(true){
  
              //Thread.sleep(1000);    
              System.out.println("reading token");
              cmd= cli.BufferedReader().readLine();
              System.out.println("token :" +cmd);
              switch (cmd) {
                  case "Msg":
                      message = cli.BufferedReader().readLine();
                      cmd="None";
                      break;
              case "File":
                GetFile();
                  break;
              case "Audio":
                  PlayAudio();
                  break;
  
  
          }
  
  
          }
  
          //reading msgs here
      }
      catch(Exception ex)
      {
          //throw new InterruptedException(ex.getMessage());
      }
  }
  </code></pre>
  
  <p>this is the function need to run when "audio" msg received from server to client</p>
  
  <pre><code> public void PlayAudio()throws Exception{
  
       try{
  
                      cli.ServerInputStream().read(buffer);
  
  
                      audio = buffer;
                      audioIn = new ByteArrayInputStream(audio);
                      AudioFormat f = new AudioFormat(8000, 8, 1,true, true);
                      ain= new AudioInputStream(audioIn,f, audio.length/f.getFrameSize());
                      DataLine.Info info = new DataLine.Info(SourceDataLine.class, f);
                      try (SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info)) {
                          int bufferSize = (int) f.getSampleRate()* f.getFrameSize();
                          buffer= new byte[bufferSize];
                          int count;
                          while ((count = ain.read(buffer, 0, buffer.length)) != -1) {
                          if (count &gt; 0) {
                            line.write(buffer, 0, count);
                          }
                          }
                          line.drain();
                      }
  
       }
       catch(Exception ex)
       {
           System.out.println("file" + ex.getMessage());
       }
  
  
   }
  </code></pre>