tags:

views:

53

answers:

2

Hi, I'm developping a streaming application with tcl. I have a vlc sever that broadcast a flow in http mode. what I'm trying to do is to develop a client who will try to connect to server with a particular ip adress and port number, and then try to save the flow in a file. the code that i'm using is simple:

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

the problem when i try to test my script, I see that I realise the connection, but I can't read the flow! the 'puts' doesn't show anything in the console...

Have you any ideas! thank you..

A: 

Since you are working with HTTP, I would suggest looking at libcurl bindings for TCL.

Nikolai N Fetissov
+2  A: 

If you're just wanting to save the contents of a URL to a file, the standard http package has a -channel option which lets you dump directly. For example:

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

Edit: Doing it asynchronously (requires the event loop going, e.g. via 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...

Note that the code's recognizably similar, but now in a slightly different order! If you've got Tcl 8.5 — if not, why not? — then you can use a lambda application instead to make the apparent order of the code even more similar:

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...
Donal Fellows
Thank you it's working very well...But when the script is downloading the file, I haven't access to the same file! In fact, I need to stream a file in http mode and forward for someone else! here I have to wait the end of streaming. What I need is to read the stream by bytes and forward it in the same time!any other ideas please!
geniecom
You should have said so before. If you don't want to keep the bytes yourself but can't wait, pass in a `-command` to `geturl` and you'll get an asynchronous callback when it's all done. In the meantime, you can do other things. I'll update the answer with a demonstration.
Donal Fellows