Real Software Forums
http://forums.realsoftware.com/

TCPSocket.connect doesn't connect in code
http://forums.realsoftware.com/viewtopic.php?f=2&t=46638
Page 1 of 1

Author:  jlawrence [ Mon Jan 21, 2013 2:58 pm ]
Post subject:  TCPSocket.connect doesn't connect in code

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

Author:  MonkeybreadSoftware [ Mon Jan 21, 2013 3:38 pm ]
Post subject:  Re: TCPSocket.connect doesn't connect in code

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

Author:  jlawrence [ Mon Jan 21, 2013 4:37 pm ]
Post subject:  Re: TCPSocket.connect doesn't connect in code

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)

Author:  msssltd [ Mon Jan 21, 2013 7:41 pm ]
Post subject:  Re: TCPSocket.connect doesn't connect in code

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.

Author:  jlawrence [ Tue Jan 22, 2013 9:44 am ]
Post subject:  Re: TCPSocket.connect doesn't connect in code

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:

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/