Skip to main content


Close an open UDP socket






This method closes an open UDP socket. Once closed, the target udpsocket object is unavailable for use and if any of its methods are called, an exception will be thrown. The method will have no effect if it is called on a udpsocket object that has already been closed. It is called implicitly if the udpsocket object goes out of scope.

Once a UDP socket is closed, its port, if it was bound to one, is now free for use by other UDP sockets.


local interface;
local udpsocket;

function interfaceHandler(state) {
    // Called when the local networking interface state changes
    server.log("Connection state: " + state);

    if (state == && udpsocket == null) {
        // We're connected, so initiate UDP
        server.log("Opening UDP on local network");
        udpsocket = interface.openudp(null, 20001);
    } else if ( && udpsocket != null) {
        // The WiFi network has gone, so close the UDP socket and
        // set it to be ready for reconnection
        server.log("Closing UDP");
        udpsocket = null;

// Open the WiFi connection for local use
server.log("Opening WiFi for local networking");
interface ={"interface":"wl0"}, interfaceHandler);