296
Platforms
©2000-2008 Tibbo Technology Inc.
reestablish a TCP connection right within the same event handler. Here is a wrong
way of doing this:
'!!! BAD EXAMPLE !!! -- this just won't work!
...
...
sock.close
sock.connect
...
You see, executing sock.close doesn't really close the connection -- it only issues
the instruction (to the Master Process) to close the connection. So, by the time
program execution gets to the sock.connect method your previous connection is
still on!
Correct way is to wait for the connection to actually be closed before executing
sock.connect. Here is another example -- not quite correct either -- but closer to
the truth.
'!!! 'BETTER' CODING !!! -- still not totally OK!
...
...
sock.close
While
sock.statesimple<>PL_SSTS_CLOSED
'here we wait for the connection
to be closed
Wend
sock.connect
...
OK, this is better. One final correction and the code is complete. In the
topic ("Socket re-use after connection closing" section) we have
already explained that the OS makes sure that the on_sock_event has a chance to
execute after the old connection is closed and before the new one is established. In
the above example both sock.close and sock.connect are in the same event
handler -- the on_sock_event won't squeeze in between them unless you use the
statement! Here is the correct code:
'100% CORRECT!
...
...
sock.close
While
sock.statesimple<>PL_SSTS_CLOSED
'here we wait for the connection
to be closed
Wend
doevents
' Absolutely essential for this particular case!
sock.connect
...
Notice how doevents is placed after the while-wend loop. It is absolutely essential
that you do it this way! Of course, now that you have at least one doevents in the
event handler you might as well add doevents in all "places of waiting" -- just to
let other events execute sooner.
290
68