udp_listener.cpp

Go to the documentation of this file.
00001 /*
00002  * test_udp_listener.cpp
00003  *
00004  * Copyright (C) 2008  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Program to test a UDP listening process.
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include "netlib/netlib.h"
00012 
00013 #include <iostream>
00014 
00015 
00016 
00017 ////////////////////////////////////////////////////////////////////////////////
00018 //
00019 //      static helper methods
00020 //
00021 ////////////////////////////////////////////////////////////////////////////////
00022 
00023 
00024 
00025 
00026 ////////////////////////////////////////////////////////////////////////////////
00027 //
00028 //      entry point
00029 //
00030 ////////////////////////////////////////////////////////////////////////////////
00031 
00032 int
00033 main
00034 (
00035 IN int argc,
00036 IN const char * argv[]
00037 )
00038 {
00039         ASSERT(2 == argc, "Usage: test_udp_listener <port>");
00040 
00041         int port = atoi(argv[1]);
00042         ASSERT(port > 0, "Bad port: %d", port);
00043 
00044         netlib::address_t address;
00045         address.setlocal(port);
00046         address.dump("local address");
00047         ASSERT(address.isValid(), "invalid local address");
00048 
00049         netlib::conn_id_t conn_id = netlib::createUdpLocal(address);
00050         ASSERT(conn_id, "null");
00051 
00052         for (;;) {
00053                 netlib::envelope_t envelope;
00054                 smart_ptr<netlib::MessageBuffer> msgbuf;
00055                 if (netlib::getNextMessage(1000, envelope, msgbuf)) {
00056                         netlib::dumpMessage(std::cerr,
00057                             "Received message from UDP client",
00058                             envelope, msgbuf);
00059                 }
00060         }
00061 }
00062