Search code examples
javaftpsolarisftp-client

Solaris FTP client implementation


I have FTP server written in java. No implementation of calculating speed.

But when I use Solaris FTP client or winSCP and connect to my ftp server and transfer file, it shows speed at the end like :

380352 bytes sent in 0.72 seconds (51562.34 Kbytes/s)

Can anybody help to let me know how this speed calculation is implemented. Or even guide where should I start looking.


Solution

  • Here's a simplified version of the code in netkit-ftp that transfers a file, showing how it calculates the transfer rate.

    recvrequest(...)
    {
        volatile off_t bytes = 0;
        struct timeval start, stop;
    
        gettimeofday(&start, (struct timezone *)0);
        while ((c = read(fileno(din), buf, bufsize)) > 0) {
            if ((d = write(fileno(fout), buf, c)) != c)
                break;      
            bytes += c;
        }
        gettimeofday(&stop, (struct timezone *)0);
        ptransfer("received", bytes, &start, &stop); 
    }
    
    ptransfer(const char *direction, off_t bytes,
        const struct timeval *t0,
        const struct timeval *t1)
    {
        struct timeval td;
        float s, bs;
    
        tvsub(&td, t1, t0);
        s = td.tv_sec + (td.tv_usec / 1000000.);
    #define nz(x)   ((x) == 0 ? 1 : (x))
        bs = bytes / nz(s);
        printf("%jd bytes %s in %.2f secs (%.1f kB/s)\n",
            (intmax_t) bytes, direction, s, bs / 1024.0);
    }
    

    The steps are:

    • store current time in start
    • read from the net, write to the file, tallying up the byte count in bytes
    • store current time in stop
    • call ptransfer, which finds the difference between start and stop, converts it to a floating-point number, makes sure the time difference is not zero, then divides bytes by the time difference and prints the result in kilobytes per second.