0

我正在用 tcl 开发一个流媒体应用程序。我有一个以 http 模式广播流的 vlc 服务器。我要做的是开发一个客户端,该客户端将尝试使用特定的 IP 地址和端口号连接到服务器,然后尝试将流保存在文件中。我使用的代码很简单:

set server localhost
set sockChan [socket $server 1234]
set line [read $sockChan 1000]
puts " vidéo: $line"
close $sockChan

当我尝试测试我的脚本时的问题,我看到我实现了连接,但我无法阅读流程!'puts' 在控制台中没有显示任何内容......

你有什么想法!谢谢你..

4

2 回答 2

2

如果您只是想将 URL 的内容保存到文件中,标准http包有一个-channel选项可以让您直接转储。例如:

package require http
set f [open video.dump w]
fconfigure $f -translation binary
set tok [http::geturl "http://server:port/url" -channel $f]
close $f
if {[http::ncode $tok] != 200} {
    # failed somehow...
} else {
    # succeeded
}
http::cleanup $tok

编辑:异步执行(需要事件循环,例如通过vwait forever):

package require http
set f [open video.dump w]
fconfigure $f -translation binary
proc done {f tok} {
    close $f
    if {[http::ncode $tok] != 200} {
        # failed somehow...
    } else {
        # succeeded
    }
    http::cleanup $tok
}    
http::geturl "http://server:port/url" -channel $f -command "done $f"
# Your code runs here straight away...

请注意,代码明显相似,但现在的顺序略有不同!如果你有 Tcl 8.5——如果没有,为什么不呢?— 然后您可以使用 lambda 应用程序来使代码的明显顺序更加相似:

package require http
set f [open video.dump w]
fconfigure $f -translation binary
http::geturl "http://server:port/url" -channel $f -command [list apply {{f tok} {
    close $f
    if {[http::ncode $tok] != 200} {
        # failed somehow...
    } else {
        # succeeded
    }
    http::cleanup $tok
}} $f]
# Your code runs here straight away...
于 2010-07-21T15:30:51.747 回答
0

由于您使用的是 HTTP,我建议您查看libcurlTCL 的绑定。

于 2010-07-21T15:23:32.447 回答