开发者

vc++ - Ftp client code - uploading a file is very slow

开发者 https://www.devze.com 2023-02-20 18:18 出处:网络
I am writing an ftp client to upload file. The pseudo code is something like below { command_Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

I am writing an ftp client to upload file. The pseudo code is something like below

{

    command_Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    login...//

    pass.. // passive mode

    get the address from the reply ... // 

    data_Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

    connect the data_Socket to that address ..//

    open the file using createFile(....) //

    loop
    {
        char buf[1000];

        readFile and fill the buf //

        send(dataSocket,buf,..);
    }

}

Now, the problem is the uploading speed is 20 kB/s even though I am uploading a file on the same machine . But when a person uploaded one file using ftp, through the lan(His machine was ubuntu), the upload speed was 10MB/s. I can't get the logic behind this.

Note: I am开发者_Python百科 using FileZilla FTP server, it has the ability to show the uploading/arrival speed of client's file .


Posting pseudo code hides probably important performance issues. So it's not clear how do you fill the the variable buf.

But you should not send chunks of 1000 bytes. This can TCP cause to send ACKs more often than necessary at the server side.


Your pseudo-code leaves out a few important details, but I'll take a few guesses to compensate :-)

  • try making your socket non-blocking, use select() to know when you can write to it and write chunks of just under 64 K
  • while your socket is sending (which is asynchronous now) you can read the next chunk of the file

your pseudo-code now becomes:

{
   command_Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    login...//
    pass.. // passive mode
    get the address from the reply ... // 
    data_Socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    /* set data socket to non-blocking  */
    connect the data_Socket to that address ..//
    open the file using createFile(....) //
    /* read a chunk */
    loop
    {
        select
        send data
        readFile and fill the buf //
    }
}

making the socket non-blocking allows your code to return as soon as the socket impl. knows what to do, so you don't have to wait around while it does what you need it to do - meaning you can read from the file while sending the data.


  1. I suggest using memory-mapped I/O
  2. If you want to use ReadFile API function, don't read by 1000 bytes.

    2.1 Read 2^k bytes (1024, 2048, 4096, etc...)

    2.2 Read more than 1024 bytes per iteration (for big file it is too small). Try to increase it to 4MB or something like that. You can do this constant file-size dependent.

    2.3 You can use async I/O to read next chunk from file while previous chunk is sending through send.

Hmm... may be others can suggest more.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号