Differences

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

Link to this comparison view

sdk:scripts:udpclient [2015/05/05 17:04] (current)
Line 1: Line 1:
 +====== SDK Script udpclient.are ======
 +<code c udpclient.are>​
 +/* DESC: This script sends a message to a remote UDP server.
 + * Copyright (C) 2012 NetModule AG, Switzerland
 + */
 +
 +void usage() ​
 +{
 +    printf("​usage:​ udpclient.are <​server>​ <​port>​ <​msg>​\n"​);​
 +    exit(1);
 +}
 +
 +if (argc < 4) {
 +    usage();
 +}
 +
 +SERVER = argv[1];
 +PORT = (int) argv[2];
 +MSG = "";​
 +
 +for (i = 3; i < argc; i++)  {
 +    MSG = strcat(MSG, " ", argv[i]);
 +}
 +
 +sock = socket(AF_INET,​ SOCK_DGRAM, IPPROTO_UDP);​
 +if (sock < 0) {
 +    printf("​Unable to open socket\n"​);​
 +    exit(1);
 +}
 +
 +sent = sendto(sock,​ MSG, SERVER, PORT);
 +
 +if (sent == strlen(MSG)) {
 +    printf("​Successfully sent message to %s:​%d\n",​ SERVER, PORT);
 +} else {
 +    printf("​ERROR:​ Sending failed"​);​
 +}
 +
 +exit(0);
 +
 +</​code>​