/* DESC: This script implements an UDP server which is able to receive messages. * Copyright (C) 2012-2016 NetModule AG, Switzerland */ void usage() { printf("usage: udpserver.are <port>\n"); exit(1); } if (argc < 2) { usage(); } PORT = (int) argv[1]; sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock < 0) { nb_syslog("Unable to open socket"); exit(1); } /* bind socket to 1st LAN interface */ addr = nb_ifc_address("LAN1"); ret = bind(sock, PORT, addr); if(ret == -1) { nb_syslog("Unable to bind to port %d", PORT); close(sock); exit(1); } nb_syslog("Listening for messages on port %d", PORT); while (1) { msg = recvmsg(sock, 3); if (is_void(msg)) continue; data = struct_get(msg, "data"); from = struct_get(msg, "from"); if (left(data, 4) == "quit") { nb_syslog("Received quit, terminating"); break; } else if (data != "") { nb_syslog("Received message '%s' from %s", data, from); } } close(sock); exit(0);