Using Buffers in UDP Mode

Top  Previous  Next

UDP is a packet-based protocol (as opposed to TCP, which is stream-based). In UDP you usually care what data belongs to what datagram (packet). The sockets object gives you complete control over the individual datagrams you receive and transmit.

Sending and receiving UDP data is still effected through the TX and RX buffers. The difference is that the subdivision into datagrams is preserved within the buffers.

Each datagram in the buffer has its own header:

For the TX buffer, headers contain datagram length as well as the destination MAC, IP, port, and broadcast flag (indicating whether to send the datagram as a broadcast).
For the RX buffer, headers contain datagram length plus the sender's MAC, IP, port, and broadcast flag (indicating whether the datagram is a broadcast).

Sending Data

Just like with TCP, sending data through the TX buffer in UDP mode is a two-step process; first you put the data in the buffer using the sock.setdata method, and then you close a datagram and perform the actual sending (commit the data) using the sock.send method.

The datagrams will never be mixed with one another. Once you invoke sock.send, the datagram is closed and sent (as soon as possible). Any new data added to the TX buffer will belong to a new datagram. For example:

 

 

sock.setdata ("Foo") ' Placed our data in the tx buffer - not being sent out yet.

 

' ... more code...

 

sock.setdata ("Bar") ' Added even more data to the same datagram, waiting to be sent.

sock.send ' A datagram containing 'FooBar' will now be closed and committed for sending.

sock.setdata ("Baf") ' This new data will go into a new datagram.

sock.send ' Closes the datagram with only 'Baf' in it and commits it for sending.

sock.send ' sends an empty UDP datagram!

 

 

Notice that in the example above we were able to send out an empty datagram by using sock.send without sock.setdata!

Keep in mind that there is a limitation for the maximum length of data in the UDP datagram- 1536 bytes.

Receiving Data

Receiving data in UDP mode requires you to be within an event handler for incoming socket data, or to explicitly move to the next UDP datagram in the buffer.

To extract the data from a buffer, use the sock.getdata method. This method only accesses a single datagram on the buffer, unless you use the sock.nextpacket method. If you have several incoming datagrams waiting, you will have to process them one by one, moving from one to the next. This is good because this way you know where one datagram ends and another one begins.

Here is an example:

 

 

sub on_sock_data_arrival

 dim whatigot as string

 whatigot = sock.getdata(255) 'will only extract the contents of a single datagram. Reenter the on_sock_data_arrival to get the next datagram

end sub

 

 

Data may only be extracted once from a buffer. Once extracted, it is no longer in the buffer.

You can also preview the data in the RX buffer without actually extracting it. Use sock.peekdata to achieve this.

Discussion of UDP data RXing continues in Receiving Data in UDP mode.