我創建了一個允許執行一些iperf命令的Android應用程序。要做到這一點,我得到了iperf的C++項目的源代碼版本3和I交叉編譯使用這些命令是:從Android 7執行IPerf3不起作用
> make clean
> ./configure --host=arm-linux --prefix=/home/laboPC/Downloads CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++ CFLAGS="-static" CXXFLAGS="-static" LDFLAGS="-pie -fuse-ld=bfd"
> make
的交叉編譯後,我得到了我把一個二進制文件我的android項目中的資產文件夾。
對於使用的iperf於Android,我創建了二進制的這樣一個副本:
private String binariePath = context.getApplicationInfo().dataDir + "/iperf3";
private void setupBinaries(){
InputStream in = context.getResources().openRawResource(R.raw.iperf3);
OutputStream out = new FileOutputStream(binariePath);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.flush();
out.close();
Runtime.getRuntime().exec("chmod 751 " + binariePath);
}
然後,我用一個運行對象來執行這樣的iperf的命令:
public String runClient (String server, String argument) {
try {
setupBinaries();
process = Runtime.getRuntime().exec(binariePath + " -c " + server + " " + argument);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
final StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line + "\n");
}
reader.close();
process.destroy();
return result.toString();
} catch (IOException e) {
Log.d("IPERF", e.getLocalizedMessage());
return e.getLocalizedMessage();
}
}
除了在Android 7.0中,Everythings都能正常工作。當我在Android 7中的Nexus 5X上運行我的應用程序時,iperf命令似乎不會執行,並且我的result
變量爲空。
我檢查了Runtime.exec()在Android 7中正常工作,並且二進制文件正確地複製到應用程序數據目錄中。
是不是每個人都有一個想法我的過程中出了什麼問題?我的命令是否正確編譯IPerf項目?
感謝您的幫助。
編輯
我在以下發現線程在Android 6.0及以上可以執行與-fPIC選項編譯的二進制文件:
android ndk: are -fPIC and -pie mututally exclusive?
Position Independent Executables and Android Lollipop
所以我試圖通過使用這個命令行來編譯我的C項目:
./configure --host = arm-linux --prefix =/home/laboPC /下載CC = arm-linux-androideabi-gcc CXX = arm-linux-androideabi -g ++ CFLAGS =「 - static -fPIC」CXXFLAGS = 「-static」LDFLAGS =「 - pie -fuse-ld = bfd」
我認爲我的命令行有些問題,但我不知道是什麼。有人可以幫助我確定我在命令行中的錯誤嗎?
直接從設備shell檢查你的二進制文件:'$ adb push iperf3/data/local/tmp; adb shell「chmod + x/data/local/tmp/iperf3」; adb shell「/ data/local/tmp/iperf3 -c」'。 –
Sergio
感謝您的回答。我這樣做,我得到一個'CAN NOT LINK EXECUTABLE「/ data/local/tmp/iperf3」:/ data/local/tmp/iperf3:有文本重定位。如果我在Nexus 5X以外的設備上執行此操作,則會出現'iperf3:錯誤 - 無法創建新流:Permission denied' – Red
嘗試將'-fpie'添加到'CFLAGS',因爲'-pie'鏈接器選項只有在鏈接代碼與位置無關時纔會執行該操作。同時仔細檢查這些標誌是否傳遞給編譯器和鏈接器。 – Sergio