/*
 * Takes on the command line a host and port, then more words.  Sends as a 
 * UDP message, merging words with a space separator.  An isolated : causes
 * a message break.
 */
        
/*
 * This file is part of the Cleansocks library.  It is released under the
 * Lesser GNU GPL.  See the accompanying files COPYING and COPYING.LESSER.
 */
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include "cleansocks.h"
#include "cleanip.h"
using namespace std;
using namespace cleansocks;
void dosend(UDPsocket &sender, string &tosend, const IPendpoint &dest)
{
        cout << "Sending: " << tosend << endl;
        int m = sendto(sender, tosend, dest);
        cout << m << " bytes sent." << endl;
        tosend = "";
}
void doit(const IPaddress &a, const IPport &p, int argc, char **argv)
{
        UDPsocket sender;
        IPendpoint dest(a,p);
        string tosend("");
        for(; argc > 0; --argc, ++argv) {
                // Check for a break;
                if(string(*argv) == ":") {
                        dosend(sender, tosend, dest);
                        continue;
                }
                // Add to the message.
                if(tosend != "") tosend += " ";
                tosend += *argv;
        }
        dosend(sender, tosend, dest);
        close(sender);
}
int main(int argc, char **argv)
{
        if(argc < 3) {
                cerr << argv[0] << " [host] [port] ..." << endl;
                exit(2);
        }
        IPaddress h = lookup_host(argv[1]);
        IPport p = atoi(argv[2]);
        try {
                doit(h,p,argc-3,argv+3);
        } catch(socket_db_error e) {
                cout << "Socket db error: " << e.what() << endl;
        }
        catch(socket_error e) {
                cout << "Socket error: " << e.what() << endl;
        }
        catch(runtime_error e) {
                cout << "Runtime error: " << e.what() << endl;
        }
        
}