This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

socket to file

Old posts that have not been replied to for several years.
Locked
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

socket to file

Post by Ofloo »

hey i am trying to understand how to write from a socket to a file i know i am doing something wrong but could someone point me out what it is

yes i know there is a http lib ;)

Code: Select all

#!/usr/bin/tclsh

proc wget {url} {
  set host [lindex [split [string trimleft $url "http://"] \x2F] 0]
  if {[string match -nocase *:* $host]} {
    set port [lindex [split $host \x3A] 1]
  } else {
    set port 80
  }
  set path [string trimleft $url "http://$host"]
  catch {socket $host $port} sock
  if {[string match -nocase sock?? $sock]} {
    fconfigure $sock -blocking 0 -buffering line
    puts $sock "GET $path HTTP/1.1"
    fconfigure $rfile -translation binary
    set wfile [open [file tail $url] w]
    fconfigure $wfile -translation binary
    while {![eof $sock]} {
      gets $sock x
      if {[string match -nocase {} $x]} {
        continue
      }
      puts $wfile "$x"
    }
    close $wfile
  }
  close $sock
}

wget $argv
XplaiN but think of me as stupid
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

You should have been seeing your problem on stderr (or $errorInfo)...

There is no "rfile" defined... Don't think you even want that line in there at all...
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

huh ur right not sur what that is doing there .. must of been real tired last night .. hehe

but still doesn't work, but it doesn't return any errors to stdout
XplaiN but think of me as stupid
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

your string match should probably be "sock*", not "sock??" as the socket descriptor can start at a single digit number and even go past two digits..
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Another thing -- you set the socket to be nonblocking, but then you try to read it in a while loop. Either don't make it nonblocking, or use fileevent to read from it.
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

now i am confused of course i am reading from it ..? the transfer of files has worked from server side like this not sur how client side manages it .. so how do you suggest i send data then retreve data ??
XplaiN but think of me as stupid
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

I already suggested not making it nonblocking... get rid of the -blocking 0 arg. I'm not sure it will work but at least try it and see what happens.

Also add some debugging statements so you can see what's happening.
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

i did thing is it doesn't do anything
XplaiN but think of me as stupid
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

You should have a flush after the puts. Also, you're not sending a valid http request... there should be headers and a blank line after the headers. At the minimum you need another \n. And a flush.
Locked