我写了 2 个应用程序:一个用于 android,使用 ServerSocket:
try {
ServerSocket serverSocket = new ServerSocket(3215);
while (true) {
try {
mConnected = false;
if (mAllowed) {
sendStatusReport(STATUS_IS_ALLOWED);
} else {
sendStatusReport(STATUS_IS_DENIED);
}
Socket socket = serverSocket.accept();
wakeLock.acquire();
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
if (mAllowed) {
mConnected = true;
sendStatusReport(STATUS_IS_CONNECTED);
while (mAllowed) {
byte[] png = takeScreenshot();
outputStream.writeInt(png.length);
outputStream.write(png);
}
outputStream.writeLong(0);
} else {
outputStream.writeLong(0);
}
wakeLock.release();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
另一个使用 C++ 的 Windows:
int recv_count = 0, data_length = 0;
const int block_size = 1024*8;
unsigned char * data;
unsigned char * ptr = data;
int received_total = 0;
int bytes_to_receive = 0;
while (received_total < data_length)
{
if (data_length - received_total < block_size)
{
bytes_to_receive = data_length - received_total;
log::obj()(this, "Left to receive %u bytes of %u, total: %u...", bytes_to_receive, data_length, received_total);
}
else
{
bytes_to_receive = block_size;
}
#define MSG_WAITALL 0x8
recv_count = recv(client_socket, (char*)ptr, bytes_to_receive, MSG_WAITALL);
if (recv_count != bytes_to_receive)
{
log::obj()(this, log::warning, "Received %u bytes instead of %u!", recv_count, bytes_to_receive);
}
ptr += recv_count;
received_total += recv_count;
}
问题是客户端通过 WiFi 以 < 2FPS 的速度接收帧(~1MB),并且当屏幕关闭时,FPS 会低至 0.2FPS(有和没有 PARTIAL_WAKE_LOCK)。服务在前台运行。
log::obj()(this, log::warning, "Received %u bytes instead of %u!", recv_count, bytes_to_receive);
表示没有 MSG_WAITALL 客户端每次接收调用接收 ~500-1000 字节而不是“block_size”。
我是初学者,不明白表现不佳的原因。