303
TIDE and Tibbo BASIC User Manual
©2000-2008 Tibbo Technology Inc.
Receiving Data in UDP Mode
we have already explained how to handle data reception
when the socket is in the TCP mode. This section provides explanation for receiving
data with UDP.
Using on_sock_data_arrival event
With UDP, you still (typically) process incoming data basing on the
event. Two differences apply:
Each time the on_sock_data_arrival event handler is entered you only get to
process a single UDP datagram waiting in the RX buffer. Unless you use a special
method (see below), you won't be able to get the data from the next datagram,
even if this datagram is already available in the RX buffer.
If, while within the on_sock_data_arrival event handler, you don't read out entire
contents of the "current" datagram and exit the event handler, then the unread
portion of the datagram is discarded.
Here is an example: supposing, two datagrams are waiting in the RX buffer and
their contents are "ABC" and "123". The following code will then execute twice:
sub
on_sock_data_arrival
dim s as string(2)
s = sock.getdata(255)
' will get 2 bytes as this is the capacity of s
end
sub
Since s has a maximum capacity of 2 the first time s=sock.getdata(255) executes
you will get "AB". When the event handler is exited the rest of the first UDP
datagram will be discarded, so next time you will get "12"!
Using sock.nextpacket method
Using on_sock_data_arrival event is a preferred method of handling an incoming
data stream. Still, in selected cases you may need to process RX data in a loop,
without leaving the event handler.
method exists for just such a case. The result of this
method execution is equivalent to exiting/(re)entering the on_sock_data_arrival
event handler: the unread portion of the previous UDP datagram is discarded and
we move to the next UDP datagram (if any).
Here is a code example where we handle all incoming UDP data without exiting
event handler:
301
342
339