Sample code for GUPnP

This sample code shows how to use GUPnP from Vala, and more specifically, how to get the external IP address for your network when you are using NAT in your router.

This code will register itself to receive any change in the external IP address, and will call the service_available_cb callback every time it is renewed.

using GUPnP;

public void service_available_cb(GUPnP.ServiceProxy sp) {

    string value = "";
    sp.send_action("GetExternalIPAddress", null, "NewExternalIPAddress", typeof(string), out value);
    print("External IP is %s\n".printf(value));

}

int main() {

    var ctx = new GUPnP.Context(null, null, 0);
    var cp = new GUPnP.ControlPoint(ctx, "urn:schemas-upnp-org:service:WANIPConnection:1");
    cp.service_proxy_available.connect(service_available_cb);
    cp.set_active(true);
        
    var mloop = new GLib.MainLoop();
    mloop.run();

    return 0;
}

Compile with:

$ valac --pkg=gupnp-1.0 gupnp_demo.vala
$ ./gupnp_demo

Projects/Vala/GUPnP_sample (last edited 2015-09-11 10:31:17 by SergioCostas)