June 20, 2018

UDP programming in Kyu

It is quite simple to set up a UDP server in Kyu. The source code provides servers for DNS, DHCP, and TFTP. These are useful in their own right, but are also worthwhile examples for study.

Setting up a UDP server

This is just a matter of arranging to listen on a certain port, and providing a callback function.
    udp_hookup ( port, rcv_func );
Once this is done, the receive function will be handed each packet received:
    my_rcv_func ( struct netbuf * );
The receive function is called in the context of the network thread, so it is quite possible to cause problems if the handler is poorly written.

In the unlikely event that you no longer want to listen on a given UDP port, call:

    udp_unhook ( port );

Sending a UDP packet

Call one of the following:
    udp_send ( target_ip, my_port, target_port, buf, count );

    udp_broadcast ( my_port, target_port, buf, count );
For more details, study the examples in the source code. In particular, note that the receive callback receives a pointer to a netbuf structure. Data contained in this structure should not be modified, but should either be acted on, or copied into a user buffer.


Have any comments? Questions? Drop me a line!

Kyu / tom@mmto.org