title: Android Wifi Bandwidth
body: <p>Recently I was given a task to develop Android app on Nexus 7 tablet, which would be conneted with pc via tcp sockets using wifi.</p>
  
  <p>Particularly I had to pass stream of images (uncompressed BMP for example) to tablet. So I made simple test to check bandwidth and was dissapointed by results. I'm siting with my tablet just in front of wifi signal sourse, it's written that connection speed is 54Mb per sec, but I get only ~16Mb per sec considering test results.</p>
  
  <p><strong>Test code:</strong></p>
  
  <p>PC:</p>
  
  <pre><code>    public static void main(String[] args) throws Exception 
      {
          Socket socket = new Socket(androidIpAddr, port);
  
          OutputStream output = socket.getOutputStream();
  
          byte[] bytes = new byte[1024 * 100]; // 10K
          for (int i = 0; i &lt; bytes.length; i++) {
              bytes[i] = 12;
          } // fill the bytes
  
          // send them again and again
          while (true) {
              output.write(bytes);
          }
       }
  </code></pre>
  
  <p>Android:</p>
  
  <pre><code>public class MyActivity extends Activity {
  /**
   * Called when the activity is first created.
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);       
  
      new Thread(new Runnable() 
        {
          public void run() 
           {
             ServerSocket server = new ServerSocket(port);
             Socket socket = server.accept();
  
             InputStream input = socket.getInputStream();
             long total = 0;
             long start = System.currentTimeMillis();
  
             byte[] bytes = new byte[1024 * 100]; // 10K
  
             // read the data again and again
             while (true) 
             {
               int read = input.read(bytes);
               total += read;
               long cost = System.currentTimeMillis() - start;
               if (cost &gt; 100) 
               {
                 double megaBytes = (total / (1024.0 * 1024));
                 double seconds = (cost / 1000.0);
  
                 System.out.println("Read " + total + " bytes, speed: " + megaBytes / seconds + " MB/s");
                 start = System.currentTimeMillis();
                 total = 0;
               }
             }                
          }
      }).start();
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>What did I missed?</p>