raw-broadcast.cpp

Go to the documentation of this file.
00001 /*
00002  * raw-broadcast.cpp
00003  *
00004  * Basic code to test raw UDP broadcasting
00005  */
00006 
00007 // includes --------------------------------------------------------------------
00008 #include <iostream>
00009 
00010 #include "common/wave_ex.h"
00011 #include "netlib/wavesock.h"
00012 #include "perf/perf.h"
00013 
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////////////////
00017 //
00018 //      static helper methods
00019 //
00020 ////////////////////////////////////////////////////////////////////////////////
00021 
00022 static void
00023 throwError
00024 (
00025 IN const char * msg
00026 )
00027 {
00028         ASSERT(msg, "null");
00029 
00030         const int bufsize = 1024;
00031         char buffer[bufsize];
00032 
00033         netlib::wsGetErrorMessage(buffer, bufsize);
00034 
00035         WAVE_EX(wex);
00036         wex << msg << "\n";
00037         wex << buffer;
00038 }
00039 
00040 
00041 
00042 static void
00043 doBroadcast
00044 (
00045 IN const char * server,
00046 IN int port
00047 )
00048 {
00049         ASSERT2(server, "null");
00050         ASSERT2(port > 0, "Bad port: " << port);
00051 
00052         // look up our own address!
00053         DPRINTF("Looking up server: '%s'", server);
00054         netlib::address_t a;
00055         if (!a.set(server, port)) {
00056                 throwError("Failed to resolve server name");
00057         }
00058         a.dump("broadcast address");
00059 
00060         int s = netlib::wsCreateUdpSocket(true);
00061         if (s < 0) {
00062                 throwError("Failed to create UDP broadcast socket");
00063         }
00064 
00065         const char * msg = "Hello world!";
00066         int len = strlen(msg);
00067         DPRINTF("Message (%d bytes): '%s'", len, msg);
00068 
00069         int nMessages = 100;
00070         for (int i = 0; i < nMessages; ++i) {
00071                 int nBytes = netlib::wsSendTo(s, msg, len, a);
00072                 DPRINTF("Sent %d bytes", nBytes);
00073                 if (nBytes != len) {
00074                         throwError("Sent bad number of bytes!");
00075                 }
00076                 sleep(3);
00077         }
00078 }
00079 
00080 
00081 
00082 ////////////////////////////////////////////////////////////////////////////////
00083 //
00084 //      entry point
00085 //
00086 ////////////////////////////////////////////////////////////////////////////////
00087 
00088 int
00089 main
00090 (
00091 IN int argc,
00092 IN const char * argv[]
00093 )
00094 {
00095         ASSERT(3 == argc, "Usage: netlib-raw-broadcast <server> <port>");
00096 
00097         const char * server = argv[1];
00098         int port = atoi(argv[2]);
00099         ASSERT(port > 0, "bad port: %d", port);
00100         int retval = 0;
00101 
00102         try {
00103                 perf::Timer timer("overall timer");
00104 
00105                 doBroadcast(server, port);
00106         } catch (std::exception& e) {
00107                 DPRINTF("EXCEPTION: %s", e.what());
00108                 retval = 1;
00109         }
00110 
00111         perf::dumpTimingSummary(std::cerr);
00112 
00113         return 0;
00114 }
00115