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 timeout

Old posts that have not been replied to for several years.
Locked
User avatar
ex
Voice
Posts: 14
Joined: Wed May 18, 2005 11:04 pm
Location: Chicago

socket timeout

Post by ex »

When my eggdrop tries to acces a webserver and this server is currently offline, the following line causes a ping timeout for my eggdrop often because he is totally blocked while trying to connect

Code: Select all

proc get:rss { url port get offset } {
        if { [catch {set sock [socket -async $url $port]}] } {
                putlog "error: Can't reach $url $port"
                return 0
        } else {
                 ...
is there any possibility to set a timeout for socket ? so that he doesn't try to reach for longer than 5 seconds or something like that?
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

no, but the correct way to implement this is either set the socket in non-blocking mode and read from/write to it only when ready (using fconfigure and fileevent) or use eggdrop's built-in socket commands ([connect] and [control])
User avatar
ex
Voice
Posts: 14
Joined: Wed May 18, 2005 11:04 pm
Location: Chicago

Post by ex »

ok tried it

Code: Select all

proc get:rss2 { url port get offst news } {
        set ::sid [socket $url $port]

        fconfigure $::sid -buffering line -blocking 0
        fileevent $::sid readable gotdata:rss
}

proc gotdata:rss { } {
...
}
thats the way someone aut of #tcl channel showed me.

but it also hangs in the line with 'socket' in it.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

did you bother to read through TCL docs, following the links I posted? If not, do so, and pay attention to the examples

from socket manpage:
-async
The -async option will cause the client socket to be connected asynchronously. This means that the socket will be created immediately but may not yet be connected to the server, when the call to socket returns. When a gets or flush is done on the socket before the connection attempt succeeds or fails, if the socket is in blocking mode, the operation will wait until the connection is completed or fails. If the socket is in nonblocking mode and a gets or flush is done on the socket before the connection attempt succeeds or fails, the operation returns immediately and fblocked on the socket returns 1.
User avatar
ex
Voice
Posts: 14
Joined: Wed May 18, 2005 11:04 pm
Location: Chicago

Post by ex »

yes i read it. but the problem is, that the eggdrop doesn't arrive at any following line

even:

Code: Select all

set utimer [utimer 10 [list close:socket [set sock [socket -async $url 80]]]]
does not help

at the moment it crash while trying to connect to eggheads.org
they are obviosly down.
i think it hangs, when trying to resolve the hostname
and no matter what scripting follows. it always hangs on the line with socket.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

if you are certain that your [socket] call blocks because of DNS resolving problem, use eggdrop's asynchronous resolver (the dns module) via [dnslookup] (check out tcl-commands.doc)
User avatar
ex
Voice
Posts: 14
Joined: Wed May 18, 2005 11:04 pm
Location: Chicago

Post by ex »

i am pretty sure because changed the program to fileevents

Code: Select all

proc test:rss { url port get offset } {
        global noff
        set ::sid [socket $url $port]
        set noff($::sid) 1
        fconfigure $::sid -buffering line -blocking 0
        fileevent $::sid readable "gotdata $::sid $offset"
        puts $::sid "GET $get HTTP/1.0"
        puts $::sid "User-Agent: bot by ex"
        puts $::sid "Host: $url"
        puts $::sid "Connection: close"
        puts $::sid ""
        flush $::sid
}

proc gotdata { sid offset } {
        global noff geturl
        if {[gets $sid bl] > 0} {
                putlog "$bl $noff($sid)"
                if {[regexp {^.*<title>(.+?)</title>.*} $bl num title]} {
                        if {$noff($sid) < $offset} {
                                incr noff($sid)
                        } else {
                                set geturl($sid) $title
                        }
                } elseif { [regexp ^.*link>(.+?)</link.* $bl num link] && [info exists geturl($sid)] } {
                        set title [ns:clean:news $geturl($sid)]
                        putlog "\002$title\002 \xbb\xbb $link"
                        unset noff($sid)
                        unset geturl($sid)
                        close $sid
                }
        }
}
and it still crashs while trying to connect to eggheads.org
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

read demonds post again.
All my scripts using real async connection are using IPs for connect and socket.

check as example my google script:

Code: Select all

dnslookup www.google.de getgoogledns:cb
dnslookup www.filemirrors.com getfmdns:cb
proc getgoogledns:cb {ip host status} { set ::googleip $ip }
proc getfmdns:cb {ip host status} { set ::fmip $ip }
This is running when the script is loaded, however, you could use dnslookup everytime, but that would only be necesarry for dynamic IPs, not for static IPs :).
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Locked