title: Java Client-Server: Stream audio files in real time
body: <p>Im making a client-server based music applicationn like spotify.The following is a only simple part of code that sends an <code>mp3 audio file</code> to my client. What i want is to <strong>play this .mp3 audio file to my client in real time</strong>
  What to change in order to achieve this?</p>
  <p>Edit : i have watched a lot of similar post both here on stackoverflow and GitHub but i cant find any solution</p>
  <p>Server</p>
  <pre><code>public void send_file_to_client(String requested_file) throws IOException {
          FileInputStream fis = null;
          BufferedInputStream bis = null;
  
          File FILE_TO_SEND = new File(&quot;C:\\ServerMusicStorage\\&quot; + requested_file + &quot;.mp3&quot;);
          byte[] mybytearray = new byte[(int) FILE_TO_SEND.length()];
          try {
              fis = new FileInputStream(FILE_TO_SEND);
              bis = new BufferedInputStream(fis);
  
          } catch (FileNotFoundException ex) {
              Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
          }
  
          OutputStream os = null;
          bis.read(mybytearray, 0, mybytearray.length);
          os = connsock.getOutputStream();
          System.out.println(&quot;Sending &quot; + FILE_TO_SEND + &quot;(&quot; + mybytearray.length + &quot; bytes)&quot;);
          os.write(mybytearray, 0, mybytearray.length);
          os.flush();
      }
  </code></pre>
  <p>Client</p>
  <pre><code>public static void receive_file_from_server(String requested_file) throws IOException {
  
          File file_to_save = new File(&quot;C:\\ClientMusicStorage\\&quot; + requested_file + &quot;.mp3&quot;);
          int bytesRead;
          int current = 0;
          FileOutputStream fos = null;
          BufferedOutputStream bos = null;
  
          byte[] mybytearray = new byte[FILE_SIZE];
          InputStream is = clientSocket.getInputStream();
          fos = new FileOutputStream(file_to_save);
          bos = new BufferedOutputStream(fos);
          bytesRead = is.read(mybytearray, 0, mybytearray.length);
          current = bytesRead;
  
          do {
              bytesRead
                      = is.read(mybytearray, current, (mybytearray.length - current));
              if (bytesRead &gt;= 0) {
                  current += bytesRead;
              }
          } while (bytesRead &gt; -1);
  
          bos.write(mybytearray, 0, current);
          bos.flush();
          System.out.println(&quot;File &quot; + requested_file + &quot;.mp3&quot;
                  + &quot; downloaded (&quot; + current + &quot; bytes read)&quot;);
  
      }
  </code></pre>