Android™
Android™
Receive a Block of Bytes to a File
See more Socket/SSL/TLS Examples
Demonstrates Socket.ReceiveBytesToFile, which receives one available block of bytes and appends it to a file.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. Any buffered data is written first; otherwise a single receive operation is performed. The method appends, so repeated calls accumulate received blocks in the file.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
CkSocket socket = new CkSocket();
// Connect to the server using TLS.
boolean bTls = true;
int maxWaitMs = 5000;
success = socket.Connect("example.com",5000,bTls,maxWaitMs);
if (success == false) {
Log.i(TAG, socket.lastErrorText());
return;
}
// The file path is relative to the application's current working directory. An absolute path
// may also be used. Supply the path appropriate to your own environment.
// Receive one available block of bytes and append it to a file. If bytes are already buffered,
// they are used first; otherwise a single receive operation is performed.
success = socket.ReceiveBytesToFile("qa_output/received.dat");
if (success == false) {
Log.i(TAG, socket.lastErrorText());
return;
}
Log.i(TAG, "Received block appended to the file.");
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}