【问题标题】:download multiple subsequent files from one server with one TCP handshake通过一次 TCP 握手从一台服务器下载多个后续文件
【发布时间】:2013-05-29 12:12:56
【问题描述】:

我尝试创建一个可以从服务器下载文件的应用程序,以测量我在特定服务器上可以获得的速度。我通过使用 Asynctask 类来做到这一点。我要下载的所有文件都位于同一目录中。我的问题是,如何通过保持连接而不是每次创建新文件来下载后续文件?我知道对于 TCP 连接,在下载文件之前必须建立 3 次握手。我想连接到服务器,然后保持连接并执行下载。

我的代码是这样的

@Override
    protected Integer doInBackground(String... sUrl) {

        try {
            speed=0; //initial value 
            int i=0;
            while ((i<sUrl.length)) {



            URL url = new URL(sUrl[i]); //sUrl[] contains the links that i want 
// for example http://myserver.net/file1.jpg, http://myserver.net/file2.jpg ... etc
            URLConnection connection = url.openConnection();
            connection.connect(); //connection to be established 3WAY HANDSHAKE

            int fileLength = connection.getContentLength();

            // download the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(file);

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            long start = System.currentTimeMillis();
            while ((count = input.read(data)) != -1) {
                total += count;

                publishProgress((int) (total * 100 / fileLength));
                output.write(data, 0, count);
            }
           long finish = System.currentTimeMillis();
            long tempSpeed= (fileLength *8)/(finish-start);
            if (tempSpeed>speed) {
                speed=tempSpeed;
            }


            output.flush();
            output.close();
            input.close(); // connection is closed
            i++;
            }    
        }catch(Exception e) {
            exceptions.add(e);
        } 

        return 1;
}

由于 3way handsharke ,通过创建新连接我节省了时间(下载速度)。此外,当在 TCP 中传输文件时,有一个称为 tcp 窗口的东西(当您下载数据时,最初您会以低速传输开始,如果连接良好,则此速率会增加)。 如何在不为每个文件创建和拆除连接的情况下执行上述操作?

【问题讨论】:

  • 是否可以连接到域,例如mydomain.com,连接后请求下载文件?我可以用一个 TCP 连接做到这一点吗?还是文件请求 (mydomai.com/myfile.txt) 会导致新的连接和新的 3way-handshake?

标签: android tcp download android-asynctask


【解决方案1】:

查看您的代码,您一直在接收,直到另一端的套接字关闭,因此无法使用相同的套接字,因为它已关闭。

如果您可以同时编程服务器和客户端,那么,我会建议一种可能的方法,那就是使用协议,而不是直接接收文件,您得到的第一个数据包是一个整数表示您将要接收的文件的大小。如果该长度为 0 (cero),则表示不再有文件,应关闭连接。

在服务器上:

While (the_are_files_to_send) 
{
     Socket.write((int) FileSize);
     Socket.write(file's content);
}
Socket.Write(0); // No more files;
Socket.Close();

在客户端:

While ((size = Socket.read(buffer, 0, 4)) != -1) 
{
   int FileLength = convert_to _int(buffer);
   if (FileLength==0) break;
   Socket.read(FileLength bytes);
}
Socket.Close();

【讨论】:

  • Write((int)FileSize) 只写入一个字节。您也不能假设读取四个字节实际上也读取了四个字节。 dataInputStream.readInt()/readLong() 是这里正确的解决方案。
  • 不幸的是,我无法对服务器进行编程。我只能对客户端进行编程。
  • 我还尝试在实际检索数据之前实例化 URLconnections。因此,当我执行 while 循环时,我已经打开了连接。然而,这并没有奏效。无法下载文件(例外)
  • 我说写一个整数,我不知道使用什么语言:C#、Java、C、Python 等。所以,我说写一个整数,无论​​他/她需要发送一个整数通过一个插座。关于 readInt() 或 readLong() 它取决于数字是否在正确的字节顺序中。我更喜欢读取 4 个字节并自己进行转换。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 2010-11-06
相关资源
最近更新 更多