I am developing wifi direct based file transfer application. I can see that the transfer speed using TCP socket is not satisfactory. The speed is normally 1Mbps. Whenever I analyze the data transfer graph, I can see a lot of spikes and many seconds are with no data transfer at all. I know the transfer speed should be much higher (may be 20-30Mbps) Please help me in increasing the transfer speed. The serversocket that accepts the connection is
private void serverTask() {
Log.v(TAG, "server task");
try {
serverRunning = true;
ServerSocket serverSocket = new ServerSocket(
DeviceDetailFragment.PORT);
serverSocket.setReceiveBufferSize(TCP_BUFFER_SIZE);
Socket client = serverSocket.accept();
BufferedInputStream inputstream = new BufferedInputStream(
client.getInputStream());
// new BufferedInputStream(client.getInputStream(), 8 * 1024);
BufferedReader bufferedStream = new BufferedReader(
new InputStreamReader(inputstream));
fileName = bufferedStream.readLine();
fileSizeInBytes = bufferedStream.readLine();
fileMime = bufferedStream.readLine();
f = new File(Globals.fileSavingLocation + fileName);
File dirs = new File(f.getParent());
if (!dirs.exists())
dirs.mkdirs();
if (f.exists()) {
f.delete();
}
f.createNewFile();
IOUtils.copy(inputstream, new FileOutputStream(f));
serverSocket.close();
}
isSuccessful = true;
} catch (IOException e) {
isSuccessful = false;
Log.e(TAG, e.getMessage());
}
serverRunning = false;
}
And the client that sends the data has following code:
private void clientTask(Intent intent) {
String fileUri = intent.getExtras().getString(EXTRAS_FILE_PATH);
String host = intent.getExtras().getString(EXTRAS_ADDRESS);
String fileName = intent.getExtras().getString(FILE_NAME);
String fileMimeType = intent.getExtras().getString(FILE_MIME_TYPE);
final long sizeInBytes = intent.getExtras().getLong(FILE_SIZE);
Socket socket = new Socket();
int port = intent.getExtras().getInt(EXTRAS_PORT);
try {
socket.setSendBufferSize(TCP_BUFFER_SIZE);
socket.bind(null);
socket.connect((new InetSocketAddress(host, port)),
SOCKET_TIMEOUT);
BufferedOutputStream stream = new BufferedOutputStream(
socket.getOutputStream());
ContentResolver cr = FileTransferService.this
.getApplicationContext().getContentResolver();
InputStream is = null;
BufferedWriter bufferStream = new BufferedWriter(
new OutputStreamWriter(stream));
bufferStream.write(fileName);
bufferStream.newLine();
bufferStream.flush();
bufferStream.write(String.valueOf(sizeInBytes));
bufferStream.newLine();
bufferStream.flush();
bufferStream.write(fileMimeType);
bufferStream.newLine();
bufferStream.flush();
try {
is = new BufferedInputStream(cr.openInputStream(Uri
.parse(fileUri)));
} catch (FileNotFoundException e) {
isSuccessful = false;
}
IOUtils.copy(is, stream);
isSuccessful = true;
} catch (IOException e) {
Log.e(TAG, e.getMessage());
isSuccessful = false;
} finally {
if (socket != null) {
if (socket.isConnected()) {
try {
socket.close();
} catch (IOException e) {
// Give up
e.printStackTrace();
}
}
}
}
The value of TCP_BUFFER_SIZE
is set to 1024*512
.
I have tried a lot altering the value of TCP_BUFFER_SIZE but with no luck. I replaced my implementation of copying stream by Apache Commons IOUtils. Help me out
Update: Please see the following transfer graph