Network programming 文件复制完成后关闭tcl服务器

Network programming 文件复制完成后关闭tcl服务器,network-programming,client-server,tcl,Network Programming,Client Server,Tcl,我在VM中运行一个tcl服务器,在windows机器中运行一个客户端,以便将文件从客户端复制到VM。问题是我希望服务器在任务完成后自动关闭。目前它没有这样做,因为“vwait”设置为“永远”。我对tcl网络编程没有太多知识,所以我不知道如何实现它 我的服务器代码 ##server side set destination_directory /home/media set service_port 9900 proc receive_file {channel_name client

我在VM中运行一个tcl服务器,在windows机器中运行一个客户端,以便将文件从客户端复制到VM。问题是我希望服务器在任务完成后自动关闭。目前它没有这样做,因为“vwait”设置为“永远”。我对tcl网络编程没有太多知识,所以我不知道如何实现它

我的服务器代码

##server side    

set destination_directory /home/media
set service_port  9900
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
和我的客户代码:

##client side
set service_port 9900
set service_host 192.168.164.161

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 "sample.pdf"

请引导。

关闭手柄后,在
接收\u文件中再添加一行:

set ::forever true   ;# or any other value at all

如果希望Tcl脚本在传输完成后终止,可以在
关闭后退出

set destination_directory /home/media
set service_port  9900
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
    exit ;     ####  All done with this process
}

socket -server receive_file $service_port
vwait forever
但是,最好使用异步形式的
fcopy
(我在这里也添加了一些其他Tcl 8.5-ISM):


生产版本中可能会有代码来防止错误的文件名,进行日志记录,并且可能允许建立多个连接,并且这些连接在服务器脚本完成之前全部完成。所有这些使得代码变得更长了……

+1:
永远
只是一个全局变量名。我们习惯于永远使用
,因为我们永远都不会设定,但你可以自由选择任何东西。
set destination_directory /home/media
set service_port  9900
proc receive_file {channel client_address client_port} {
    global destination_directory
    lassign [gets $channel] name size
    fconfigure $channel -translation binary
    if {[catch {
        set fp [open [file join $destination_directory $name] "wb"]
        fcopy $channel $fp -size $size -command [list done_transfer $channel $fp]
    }]} exit
}
proc done_transfer {from_channel to_channel bytes {errorMessage ""}} {
    close $from_channel
    close $to_channel
    exit
}

socket -server receive_file $service_port
vwait forever