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.
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.
Test code:
PC:
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 < bytes.length; i++) {
bytes[i] = 12;
} // fill the bytes
// send them again and again
while (true) {
output.write(bytes);
}
}
Android:
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 > 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();
}
}
What did I missed?