安卓开发中,测试LAN,WLAN功能,拷贝是否正常,速率是否正常的具体代码
时间: 2024-03-11 08:48:11 浏览: 18
下面是一个测试LAN和WLAN功能,拷贝是否正常,速率是否正常的示例代码:
```
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String TEST_FILE_NAME = "test.txt";
private static final int TEST_FILE_SIZE = 1024 * 1024; // 1MB
private static final int TEST_COUNT = 10;
private TextView mStatusTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStatusTextView = findViewById(R.id.status_text);
// 开始测试
testLanAndWlan();
}
/**
* 测试LAN和WLAN功能
*/
private void testLanAndWlan() {
// 获取本地IP地址
String localIp = getLocalIpAddress();
if (localIp == null) {
showStatus("获取本地IP地址失败");
return;
}
showStatus("本地IP地址:" + localIp);
// 创建测试文件
File testFile = createTestFile();
if (testFile == null) {
showStatus("创建测试文件失败");
return;
}
showStatus("测试文件已创建:" + testFile.getAbsolutePath());
// 测试LAN和WLAN
testLanAndWlan(testFile);
}
/**
* 获取本地IP地址
*/
private String getLocalIpAddress() {
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
return address.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
/**
* 创建测试文件
*/
private File createTestFile() {
File dir = getExternalFilesDir(null);
if (dir == null) {
return null;
}
File testFile = new File(dir, TEST_FILE_NAME);
try {
OutputStream outputStream = new FileOutputStream(testFile);
byte[] buffer = new byte[1024];
for (int i = 0; i < TEST_FILE_SIZE; i += buffer.length) {
outputStream.write(buffer);
}
outputStream.flush();
outputStream.close();
return testFile;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 测试LAN和WLAN
*/
private void testLanAndWlan(File testFile) {
// 获取局域网和无线局域网的IP地址
String[] ips = {getLanIpAddress(), getWlanIpAddress()};
// 测试拷贝速度
for (String ip : ips) {
if (ip != null) {
float totalTime = 0;
for (int i = 0; i < TEST_COUNT; i++) {
float time = testCopySpeed(ip, testFile);
showStatus("拷贝时间:" + time + "s");
totalTime += time;
}
float averageTime = totalTime / TEST_COUNT;
float speed = TEST_FILE_SIZE / averageTime / 1024 / 1024;
showStatus("平均速度:" + speed + "MB/s");
}
}
}
/**
* 获取局域网IP地址
*/
private String getLanIpAddress() {
try {
DhcpInfo dhcpInfo = ((WifiManager) getSystemService(Context.WIFI_SERVICE)).getDhcpInfo();
int ipInt = dhcpInfo.ipAddress;
InetAddress inetAddress = InetAddress.getByAddress(BigInteger.valueOf(ipInt).toByteArray());
return inetAddress.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取无线局域网IP地址
*/
private String getWlanIpAddress() {
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (!address.isLinkLocalAddress() && !address.isLoopbackAddress()
&& address instanceof Inet4Address) {
String hostAddress = address.getHostAddress();
if (hostAddress.startsWith("192.168.")) {
return hostAddress;
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
/**
* 测试拷贝速度
*/
private float testCopySpeed(String ipAddress, File testFile) {
Socket socket = null;
OutputStream outputStream = null;
InputStream inputStream = null;
try {
// 连接TCP服务器
socket = new Socket(ipAddress, 8888);
// 发送测试文件
outputStream = socket.getOutputStream();
byte[] buffer = new byte[1024];
FileInputStream fileInputStream = new FileInputStream(testFile);
int length;
while ((length = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
// 接收测试文件
inputStream = socket.getInputStream();
long startTime = System.currentTimeMillis();
while ((length = inputStream.read(buffer)) != -1) {
}
long endTime = System.currentTimeMillis();
return (endTime - startTime) / 1000f;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return -1;
}
/**
* 显示状态信息
*/
private void showStatus(String status) {
Log.d(TAG, status);
mStatusTextView.append(status + "\n");
}
}
```
在该代码中,首先使用getLocalIpAddress()方法获取本地的IP地址。然后,使用createTestFile()方法创建一个大小为1MB的测试文件。接着,使用getLanIpAddress()和getWlanIpAddress()方法获取局域网和无线局域网的IP地址。最后,使用testCopySpeed()方法测试拷贝速度,并计算出平均速度。