295
TIDE and Tibbo BASIC User Manual
©2000-2008 Tibbo Technology Inc.
'Correct code -- on startup we order the connection to be established and
in the on_sock_event event handler we record the MAC address of the
'other side'
Sub
On
_sys_init
...
sock.targetip="192.168.100.40"
'we prepare for the connection
sock.targetport=2000
sock.connect
'and now we connect
End
Sub
'-------------------------------------------------------------------------
-----------
Sub
On
_sock_event(newstate
As
pl_sock_state,newstatesimple
As
pl_sock_state_simple)
Dim
s
As
String
If
newstatesimple=PL_SSTS_EST
Then
'OK, so connection is established, let's get this MAC!
s=sock.remotemac
End
If
End
Sub
The above is a good example of event-driven programming. Sometimes, however,
you need to establish a connection and "follow-up" on it in the same event
handler. So, how do we do this? Here is a simple, and WRONG code:
'!!! BAD EXAMPLE !!!
Dim
s
As
String
...
...
sock.targetip="192.168.100.40"
'we prepare for the connection
sock.targetport=2000
sock.connect
'and now we connect
s=sock.remotemac
'and now we try to check the MAC. WRONG!
Connection may not be established yet!
...
And here is the correct way to handle this. For clarity, this example assumes that
connection will definitely be established.
'Correct, but simplified example (we do not handle possible connection
failure).
Dim
s
As
String
...
...
sock.targetip="192.168.100.40"
'we prepare for the connection
sock.targetport=2000
sock.connect
'and now we connect
While
sock.statesimple<>PL_SSTS_EST
'we wait here until the connection is
actually established
Wend
s=sock.remotemac
'Get the MAC!
...
Here is even more interesting example. Supposing, you want to close and