How to send a file through a socket connection

[Explain why ftp is probably better choice, or tls, or an exec of ssh, or...]

Here's one demonstration:

    # This is server.tcl.

    # This is a model implementation of file-copying through Tcl's [socket]
    #    facilities.  What follows is the server process, which receives
    #    files through network connections, and deposits them in
    #    $destination_directory.

    # A production version of this model would probably expose parameters,
    #    handle exceptions more gracefully, consider security, and so on.
    #    The purpose of this code is primarily pedagogic.

        # You might want this to come from a command-line argument,
        #    or environment variable, or even let the clients pass
        #    it in.
    set destination_directory /tmp

        # A command-line argument is probably a good way to specify this.
    set service_port  3456

    proc receive_file {channel_name client_address client_port} {
        fconfigure $channel_name -translation binary
        gets $channel_name line
        foreach {name size} $line {}

        set fully_qualified_filename [file join $::destination_directory $name]
        set fp [open $fully_qualified_filename w]
        fconfigure $fp -translation binary

        fcopy $channel_name $fp -size $size

        close $channel_name
        close $fp

    socket -server receive_file $service_port

    vwait forever

And here's the second half:

    # This is client.tcl.

    # Invoke as "client.tcl <filename>" (for a local file).

    set service_port 3456
    set service_host ;# Old web site - need to change!

    proc send_one_file name {
        set size [file size $name]
        set fp [open $name]
        fconfigure $fp -translation binary

        set channel [socket $::service_host $::service_port]
        fconfigure $channel -translation binary
        puts $channel [list $name $size]

        fcopy $fp $channel -size $size

        close $fp
        close $channel

    send_one_file $argv

[other uses of socket.]

[Make link to cool Expect example.]

[ Steve Ball's book has chapter on client-server programming.]

Dennis LaBelle has an example [L1 ].

TV Probably the procedures in Pcom make sense at least to look at. They also deal with possible through-firewall file transfering by allowing the connection to be set up from either side for both sending or receiving files. There is a small security issue once the control connection sets up the file transfer connection, where during a small interval rendez-vous could take place with another unintended party, and especially when for some reason the connection doesn't get made, than the server socket remains open, waiting for a connection. The use of the special copy command probably makes connections relatively fast in spite of the interpreted language use. I'll soon make a page testing this, and maybe some benchmarks on linux/windows, both IPC on the same machine and across a lan.

escargo 20 Oct 2003 - There also seems to be to be some interesting issues with the acceptable forms of file names. If the client and server are running the same operating system, then things are not too bad (aside from major security issues). If the client and server have dissimilar file name semantics, what may be acceptable on one system may be bad on another. (Sending two files with names that differ only in capitalization could be a problem on a receiving system that ignored case.)