java - 从 JAVA 套接字读取比 Python 慢

标签 java android python sockets

我正在尝试使用 TCP 套接字从 Python 服务器读取数据。为此,已经编写了 python 客户端,我应该为此编写 Android 代码。我在 JAVA 中尝试过不同的方法,但我发现我编写的 JAVA 代码中的 Socket 读取速度非常慢。

在某些情况下我会覆盖缓冲区,因为我只关心接收到的字节数而不是实际数据。

以下是Python客户端读取socket的 fragment

        buffer_len = 0
        print str(tcp.response_len) + " start  " + str((time.time()-timeOrigin)*1000)
        while True:
            buffer_len += len(self.sock.recv(4096))
            print str(buffer_len) + " of " + str(tcp.response_len)
            if buffer_len >= tcp.response_len:
                break

以下是我在 JAVA 中尝试过的一些事情:

            while (totalRead < RS.getResponse_len()) {
                                    //dataInputStream is of type DataInputStream
                int bytesRead = dataInputStream.read(buffer, totalRead, RS.getResponse_len() - totalRead);
                Log.d("Response", "Total read : " + totalRead + " of " + RS.getResponse_len());
                if (bytesRead < 0) {
                    throw new IOException("Data stream ended prematurely");
                }
                totalRead += bytesRead;
            }

小缓冲区:

                while (totalRead < RS.getResponse_len()) {
                int bytesRead = dataInputStream.read(buffer, 0, 4096);

                Log.d("Response", "Total read : " + totalRead + " of " + RS.getResponse_len());
                if (bytesRead < 0) {
                    throw new IOException("Data stream ended prematurely");
                }
                totalRead += bytesRead;
            }

我也尝试过使用 readFully 而不是 read,但读取速度仍然很慢。

然后我尝试使用 ReadableByteChannel 读取数据,如下所示:

            ByteBuffer lenBuf = ByteBuffer.allocate(4096);
            //SocketChannel.open();
            ReadableByteChannel channel = Channels.newChannel(client.socket.getInputStream());
            int bytesRead = 0;
            while (totalRead < RS.getResponse_len()) {
                bytesRead = channel.read(lenBuf);
                lenBuf.clear();
                Log.d("Response", "Total read : " + totalRead + " of " + RS.getResponse_len());
                totalRead += bytesRead;
            }

但我没有看到阅读有任何显着改善。

能否请您指出 mu 代码中的错误或建议我一些替代方法以加快阅读速度?

谢谢。

最佳答案

我不知道 RS.getResponse_len() 做了什么。我只能希望它不会循环计算长度。但是你的第二个 fragment 的一个改进是这个。您的原始代码会分配足够的缓冲区大小来容纳整个响应长度。您可以将缓冲区大小用作 Response_len()。你会有类似的东西

byte[] buffer = new byte[RS.getResponse_len()];
int totalRead = 0;
while(totalRead<buffer.length) {
    int bytesRead = dataInputStream.read(buffer,totalRead,buffer.length-totalRead);
    if(bytesRead<0) throw new IOException("Data stream ended prematurely");

    totalRead+=bytesRead;
}

您说您将读取 14MB 的数据。在这种情况下,您将需要读入较小的缓冲区并处理该缓冲区。

如果您只是想阅读内容而什么都不做。试试这个

byte[] buffer = new byte[10240]; // 10Kb buffer
BufferedInputStream bis = new BufferedInputStream(dataInputStream);
int totalBytesToRead = RS.getResponse_len();
while(totalBytesToRead>0) {
    int bytesRead = bis.read(buffer);
    if(bytesRead<0) throw new IOException("Data stream ended prematurely");
    totalBytesToRead -= bytesRead;
}

关于java - 从 JAVA 套接字读取比 Python 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22569516/

相关文章:

python - 将 csv 文件加载到列表中

java - onError java.lang.NullPointerException : Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference 错误

安卓|指定的子项已有父项。您必须先对 child 的 parent 调用 removeView()

android - 在 PreferenceActivity 之外使用 findPreference(key)

android - 修复后台 RAM 使用率高和 Google 服务在我的应用程序中不断运行的问题

Python 'with' 语句 - 如何判断哪个模块/对象/类支持它?

java - 安卓 5 : No more Garbage Collection message?

java - Android Room 插入所有问题

java - 为什么这会返回 false 和 true?

python - 配对颜色名称和值