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.

Async socket

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:

Async socket

Post by Ofloo »

I am having a hard time understanding async socket when it infact connects and when not ive seen several examples and read about it but can someone tell me what it is what i am doing wrong .. or give me an working example .. one that shows when u connect and when the connected failed cause i tryed this and it seems to be connecting all the time makes no difference wether it connects or not

tnx in advance

Code: Select all

#!/usr/bin/tclsh

proc open_sock {host port} {
  catch {socket -async $host $port} sock
  fileevent writable [list check_sock $sock $host $port]
}

proc check_sock {sock host port} {
  set error [fconfigure $sock -error]
  close $sock
  eval report_sock [list $error] 
}

proc report_sock {error} { 
  if {[string match -nocase {} $error]} { 
    puts "socket connected" 
  } 
}

open_sock [lindex $argv 0] [lindex $argv 1]
Error:
open_sock [lindex $argv 0] [lindex $argv 1]
brain:~# ./sock 127.0.0.1 666
bad event name "check_sock sock3 127.0.0.1 666": must be readable or writable
while executing
"fileevent writable [list check_sock $sock $host $port]"
(procedure "open_sock" line 3)
invoked from within
"open_sock [lindex $argv 0] [lindex $argv 1]
"
(file "./sock" line 20)
socket closes directly before it can check it is writable
listening on [any] 666 ...
connect to [127.0.0.1] from localhost [127.0.0.1] 48096
sent 0, rcvd 0
brain:~#
XplaiN but think of me as stupid
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Re: Async socket

Post by stdragon »

Ofloo wrote: ...
cause i tryed this and it seems to be connecting all the time makes no difference wether it connects or not
...
From what you posted, it seems like it does not show it to be connecting all the time... it instead shows an error all the time. You forgot an argument to fileevent, namely the socket.
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

<seemed to of posted it twice>
Last edited by Ofloo on Wed Apr 14, 2004 5:14 am, edited 1 time in total.
XplaiN but think of me as stupid
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

Ok thats something i could of prevented didn't see but i rewrot that like 5 times so it doesn't do anything why is that ?

ive tryed this not once but like 50 times before and i always end up the same the socket connects closes can't check for writable and nothing happens

Code: Select all

#!/usr/bin/tclsh

proc open_sock {host port} {
  catch {socket -async $host $port} sock
  fileevent $sock writable [list check_sock $sock $host $port]
}

proc check_sock {sock host port} {
  set error [fconfigure $sock -error]
  close $sock
  eval report_sock [list $error]
}

proc report_sock {error} {
  if {[string match -nocase {} $error]} {
    puts "socket connected"
  }
}

open_sock [lindex $argv 0] [lindex $argv 1]
brain:~# ./sock 127.0.0.1 666
brain:~#
listening on [any] 666 ...
connect to [127.0.0.1] from localhost [127.0.0.1] 51639
sent 0, rcvd 0
brain:~#
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 also have to wait for it to connect. Try using vwait or a loop with update.
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

tryed it

when it becomes writable set connect 1

then vwait connect

then if {$connect} {} .. tryed hehe what is it i am doing wrong i know it has to be something .. just can't put my finger on it ..

could maybe someone write a working example ?? doesn't matter how as long as its an async socket that checks if it connects yes or no thats it
XplaiN but think of me as stupid
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Code: Select all

proc open_sock {host port} {
	if {[catch {socket $host $port} sock]} {
		puts "Connection to $host:$port failed: $sock"
	} {
		fileevent $sock writable [list check_sock $sock $host $port]
		vwait ::done
	}
}
proc check_sock {sock host port} {
	if {[fconfigure $sock -error]==""} {
		puts "Connected to $host:$port"
	} {
		puts "Connection to $host:$port failed: [fconfigure $sock -error]"
	}
	close $sock
	set ::done 1
}
Have you ever read "The Manual"?
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

seems to be working tnx verry mutch
XplaiN but think of me as stupid
Locked