从Java套接字读取比Python慢 [英] Reading from JAVA socket slower than Python

查看:236
本文介绍了从Java套接字读取比Python慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取使用TCP套接字从Python的服务器的数据。对于这一点,客户端蟒蛇已经写好,我应该写的Andr​​oid code这一点。我试过在JAVA不同的方法,但我看到,插座,我已经写很慢JAVA code阅读。

I'm trying to read the data from Python server using TCP socket. For this, client in python is already written and I'm supposed to write Android code for this. I've tried different approaches in JAVA, but I'm seeing that Socket reading in JAVA code that I've written is very slow.

我重写缓冲区在某些情况下,因为我只关心,我收到没有实际数据的字节数。

I am overwriting buffer in some cases as I am only concerned with number of bytes that I am receiving not the actual data.

以下是插座阅读Python中的客户端的代码片段

Following is the snippet of socket reading in Python client

        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已经试过几件事情:

Following are the couple of things that I have tried in 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,而不是阅读太多,但读数仍然缓慢也试过。

I also tried using readFully instead of read too but reading was still slow.

然后我试图用读的ReadableByteChannel如下数据:

Then I tried reading data using ReadableByteChannel as follows :

            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;
            }

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

But I am not seeing any significant improvements in reading.

能否请您点中多亩code错误或建议我一些替代品更快的读取?

Can you please point mistakes in mu code or suggest me some alternatives for faster reading?

感谢您。

推荐答案

我不能告诉RS.getResponse_len()做的。我只能希望它不循环计数长度。但是,在你的第二片段的一个改进是这样的。您原来的code将分配到足够的缓冲区大小,以适应整个长度效应初探。你可以利用你的缓冲区大小为Response_len()。你会像

I can't tell what RS.getResponse_len() do. I can only hope that it doesn't loop to count the length. But one improvement in your 2nd snippet is this. Your original code would have allocated enough buffer size to accomodate the entire reponse length. You can make use of your buffer size as Response_len(). You would have something like

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。在这种情况下,你需要读入该缓冲区较小的缓冲区和过程。

You said you will be reading 14MB of data. In that case you will need to read into a smaller buffer and process that buffer.

如果你只是想阅读的内容,什么也不做它。试试这个

If you just want to read the contents and do nothing with it. Try this

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套接字读取比Python慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆