title: Streaming MP3 to Android from C# server
body: <p>I have set up a C# server which at present serves up one test mp3 file over TCP. The code to send the file is as follows</p>
  
  <pre><code>    public void RunStreamer()
      {
          log.MakeLog("Starting streamer");
          Run = true;
          //Need to look into how to thread this to not block the main window
          TcpListener listen = new TcpListener(localAddr, _port);
          listen.Start(); //startlistening to client requests
  
              //blocks until a client request comes in
          for (; ; )
          {
              Socket socket = listen.AcceptSocket();
              if (socket.Connected)
              {
  
                  SendFileToClient(socket);
                  socket.Disconnect(false);
              }
          }
  
      }
  
      void SendFileToClient(Socket socket)
      {
          log.MakeLog("Connection made");
          NetworkStream netStream = new NetworkStream(socket);
          StreamWriter writer = new StreamWriter(netStream);
          //Todo - set specfified file - this file just for test
          FileStream filestream = File.Open(@"C:\MusicTest\Test.mp3", FileMode.Open, FileAccess.Read, FileShare.Read);
          filestream.CopyTo(netStream);
          netStream.Flush();
          netStream.Close();
  
  
      }
  </code></pre>
  
  <p>In my test android set up I am making a call to the server on a button click:</p>
  
  <pre><code>    public void btngo_click(View v)
  {
      final TcpClient client = new TcpClient();
  
      new Thread(new Runnable(){
          @Override
          public void run() {
              final MediaPlayer mediaPlayer = new MediaPlayer();
              client.GetStream();
              runOnUiThread(new Runnable(){
                  public void run()
                  {
                       int length = client.GetLength();
                       if(length &gt; 0)
                       {
                          byte[] result = client.GetResult();
                          try {
                              // create temp file that will hold byte array
                              File tempMp3 = File.createTempFile("test", "mp3", getCacheDir());
                              tempMp3.deleteOnExit();
                              FileOutputStream fos = new FileOutputStream(tempMp3);
                              fos.write(result);
                              fos.close();
  
  
                              mediaPlayer.reset();
  
                              FileInputStream fis = new FileInputStream(tempMp3);
                              mediaPlayer.setDataSource(fis.getFD());
  
                              mediaPlayer.prepare();
                              mediaPlayer.start();
                          } catch (IOException ex) {
                              String s = ex.toString();
                              ex.printStackTrace();
                          }
  
                       }
                  }
              });
  
          }
      }).start();
  
  }
  </code></pre>
  
  <p>the stream is received in the TcpClient class which is as follows:</p>
  
  <pre><code>public class TcpClient {
  
  public final static String SERVER_ADDRESS = "127.0.0.1";
  public final static int SERVER_PORT = 65000;
  public String TotalResult;
  public int Length;
  byte[] result = new byte[21000000];
  
  
  public TcpClient()
  {
  
  
  
  }
  
  public int GetLength()
  {
      return Length;  
  
  }
  
  public byte[] GetResult()
  {
  
      return result;
  
  }
  
  public void GetStream()
  {
      try
      {
  
          final Socket socket = new Socket("192.0.0.5", 85000);
          final InputStream input = new BufferedInputStream(socket.getInputStream());
          ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  
          int nread;
  
  
          while((nread = input.read(result, 0, result.length)) != -1)
          {
  
              buffer.write(result, 0, nread);
          }
          buffer.flush();
  
          //input.read(result);
          Length = result.length;
  
  
          input.close();
          socket.close();
  
      } catch (UnknownHostException e) {
          String exc = e.getMessage();
          e.printStackTrace();
      } catch (IOException e) {
          String exc2 = e.getMessage();
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>With apologies for all the code here is my problem.</p>
  
  <p>I am receiving the stream. The temp MP3 file is created and the media player starts.  I then only get a short snippet of the test MP3 file (which is a full song). It also jumps about a bit. The length is not the same and the section of the song played is different each time. </p>
  
  <p>How do I receive the full file in a ordered way such that it will provide full play back of the song. </p>
  
  <p>I have tried to route around for this and have an idea that I need to tell my client what file size it should suspect and then perform some loop until all data is received although I have no idea how to successfully implement this if that is the correct solution.</p>
  
  <p>Any pointers on where I am going wrong or what I can do to rectify would be greatly appreciated!!</p>