Android / Windows TCP客户端 - 服务器性能问题

时间:2012-09-17 13:30:03

标签: android tcp client-server

我写了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;
}

问题是客户端在&lt;处理器上接收了frmaes(~1MB)。通过WiFi 2FPS,当屏幕关闭时,FPS低至0.2FPS(有和没有PARTIAL_WAKE_LOCK)。服务在前台运行。

log::obj()(this, log::warning, "Received %u bytes instead of %u!", recv_count, bytes_to_receive);

说没有MSG_WAITALL客户端每次recv调用接收~500-1000字节而不是`block_size'。

我是初学者,并不理解这种糟糕表现的原因。

1 个答案:

答案 0 :(得分:0)

尝试缓冲输出流,否则,每次写入都可以发送数据包。

        DataOutputStream outputStream = new DataOutputStream( new BufferedOutputStream(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);
        }
        outputStream.flush();

唯一改变的行是第一行和最后一行。