Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sdk:scripts:udpserver [2015/05/05 17:04] (current)
Line 1: Line 1:
 +====== SDK Script udpserver.are ======
 +<code c udpserver.are>​
 +/* DESC: This script implements an UDP server which is able to receive messages.
 + * Copyright (C) 2012 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) {
 +    printf("​Unable to open socket\n"​);​
 +    exit(1);
 +}
 +
 +/* bind socket to 1st LAN interface */
 +addr = nb_ifc_address("​LAN1"​);​
 +ret = bind(sock, PORT, addr);
 +if(ret == -1) {
 +    printf("​Unable to bind to port: %d\n", PORT);
 +    close(sock);​
 +    exit(1);
 +}
 +
 +printf("​Listening for messages on port %d\n", PORT);
 +
 +while (1) {
 +    msg = recvfrom(sock);​
 +    if (left(msg, 4) == "​quit"​) {
 +        printf("​Received quit, terminating\n"​);​
 +        break;
 +    } else if (msg != ""​) {
 +        printf("​Received message '​%s'​\n",​ msg);
 +    }
 +}
 +
 +close(sock);​
 +exit(0);
 +
 +</​code>​