Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Oct 17, 2017 10:44 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: TCPSocket.connect doesn't connect in code
PostPosted: Mon Jan 21, 2013 2:58 pm 
Offline

Joined: Thu Dec 16, 2010 1:22 pm
Posts: 188
If I drag a tcpsocket to a window and call
tcpsocket1.address="www.google.com"
tcpsocket1.port=80
tcpsocket1.connect

the connected event fires. cool.

If I put in code
dim mytcpsocket = new tcpsocket
mytcpsocket.address="www.google.com"
mytcpsocket.port=80
mytcpsocket.connect
if not mytcpsocket.isconnected then
msgbox "error"
end

guess what? Error. Not cool! (even though lasterrorcode returns 0)

but if I add a handler to the above before connecting
addhandler mytcpsocket.connect, addressof mytcpisconnected


and then create a method

Protected Sub mytcpisconnected(mytcp as TCPSocket)
MsgBox "connected"
End Sub

It connects! Cool!

So, while I did eventually solve my problem, I would like this to be a learning experience and take some knowledge away from it that I can possibly use in the future.
Can someone explain why the simple in-code example doesn't work?
Thanks! :o


Top
 Profile  
Reply with quote  
 Post subject: Re: TCPSocket.connect doesn't connect in code
PostPosted: Mon Jan 21, 2013 3:38 pm 
Offline
User avatar

Joined: Wed May 10, 2006 2:42 pm
Posts: 2985
Location: Germany
Connect is asyncronously.

you would need to make a loop calling poll function to see isconnected getting true sometime later if you need it synchronous.

Greetings
Christian

_________________
See you in Orlando, Florida for Real World 2013
More details and registration here:
http://www.realsoftware.com/community/realworld.php


Top
 Profile  
Reply with quote  
 Post subject: Re: TCPSocket.connect doesn't connect in code
PostPosted: Mon Jan 21, 2013 4:37 pm 
Offline

Joined: Thu Dec 16, 2010 1:22 pm
Posts: 188
ahh! That did it.
dim m as double=Microseconds

do until mytcp.IsConnected
mytcp.Poll
loop

MsgBox str((Microseconds-m)/1000)


connected on average in about 15 milliseconds. Not instantaneous, but fast enough :)
Thanks a bunch!

For the curious, is it documented anywhere that it's asynchronous? I couldn't find anything.
Thanks again. 8)


Top
 Profile  
Reply with quote  
 Post subject: Re: TCPSocket.connect doesn't connect in code
PostPosted: Mon Jan 21, 2013 7:41 pm 
Offline
User avatar

Joined: Fri Oct 28, 2005 7:05 am
Posts: 565
Location: Emsworth, UK
const time_out = 2000000
dim mStart as double = Microseconds

do
mytcp.Poll
loop until (mytcp.IsConnected) or (Microseconds - mStart > time_out)

if mytcp.IsConnected then
MsgBox "Connected in " + str((Microseconds-mStart)/1000) + " ms"
else
MsgBox "Time out. Error " + str(mytcp.LastErrorCode)
end if

Hope you don't mind the additions. Hopefully you can work out why.

Quote:
For the curious, is it documented anywhere that it's asynchronous?

Does it need to be documented? Sockets are asynchronous.
.Poll is the documented way to make a socket behave as if it were synchronous, within RS.

_________________
Yes it's me in the avatar


Top
 Profile  
Reply with quote  
 Post subject: Re: TCPSocket.connect doesn't connect in code
PostPosted: Tue Jan 22, 2013 9:44 am 
Offline

Joined: Thu Dec 16, 2010 1:22 pm
Posts: 188
I did have a time out in my original code as well - just removed it for simplicity. 8)

Anything that isn't obvious should be documented. There is no such thing as too much information :wink:


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group